Create your own conditional tag in WordPress

As teaken from: WP Ninjas.

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…

if ( is_single() ) {
    // Do Something
}

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…

global $post
if( 'download' == get_post_type() ){
    $terms = get_the_terms( $post->ID, 'download_category' );
    if( $terms ) {
        foreach( $terms as $term ) {
            if( 'extension' == $term->slug ) {
                // Do something
                break;
            }
        }
    }
}

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.

function prefix_is_extension() {
    global $post;
    $response = false;
    if( 'download' == get_post_type() ){
        $terms = get_the_terms( $post->ID, 'download_category' );
        if( $terms ) {
            foreach( $terms as $term ) {
                if( 'theme' != $term->slug ) {
                    $response = true;
                    break;
                }
            }
        }
    }
    return $response;
}

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.

if ( prefix_is_extension() ) {
    // Do something
}

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.

Revisions

Tags:

No comments yet.

Leave a Reply