Create your own conditional tag

Have you ever wanted to do something based on a specific set of conditions. Sure you have and you’ve probably used ┬áthe WordPress Conditional Tags at least once or twice. Conditional Tags are extremely useful functions that check a set of conditions for you.

They work like this…

The is_single function returns true if what you are interacting with is a single post. It reads nicely. If is single then so something. But is_single, while useful, isn’t always the thing you want to check. Sometimes what you are checking for is much more complicated.

Here is an example of something we use on this site. We use Easy Digital Downloads to sell our products. All of these products, at least at the moment, are extensions of Ninja Forms. I wanted a way to conditionally target all downloads that had the category of “extension”. This way I could include or exclude certain functionality from just these types of products. I can check this condition like this…

This first checks if what I an interacting with is a download and then loops through the terms to see if one of them is “extension”. If it is then it will run some code. If not it does nothing. Here’s the problem though, I have to run through this same bit of code every single time I want to check this condition. Worse yet, if I want to expand or change my condition I have to go back and edit every place I’ve used this code. What a pain in the neck.

This is where creating your own conditional tag comes in to save the day. All we really have to do is wrap the code above into a function and add a few lines of code.

See what we’ve added. A variable set to false at the beginning, change the variable to true if the condition is met, and return that variable at the end. Now we can use this function anytime we want to check this condition.

See how much simpler that is? The best part is if you want to tweak the condition at some point down the road you only have to change one function and not a dozen places where you’ve copied that same code.

Of course this is a very specific example that we are currently using but you can apply this to almost anything you might want to check regularly and save your project possible hundreds of lines of code.

What conditional tags do you find useful or wish were available?


  1. Peter says

    The one *really* useful tag I would like to see is one that allows control over author, date/time of posting and commenting. It is a mess, doubly so because posts and pages do not share any sort of mechanism for that.

    Given that WP is used for all sorts of things I am astonished that full control over author identification is still hard work. The options none, account name or any kind of alias would be welcome – after all, publicly showing an account name is flat out stupid from a security perspective. Next is the format of time, but that has at least some definition – until a theme chooses to override it. I know some people would like even hour and millisecond on a post, but at most a date is in my case more than enough.

    Next is commenting – not all blogs need comments, but some themes insist on telling you that “comments are disabled”. Well, duh – it’s evident if there aren’t any boxes around to enter a comment, no? “You can comment if you are logged in”, fine, that’s OK because it indicates a possible cause of action, but a sign that says “you cannot do this” is IMHO unhelpful.

    So there, this is what I would like to see as a conditional. If not wanted, nuke author name, date, comments on comment availability..

Leave a Reply

Your email address will not be published. Required fields are marked *