Revision 506 is a pre-publication revision. (Viewing current revision instead.)

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… [php]if ( is_single() ) { // Do Something }[/php] 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… [php]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; } } } }[/php] 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. [php]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; }[/php] 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. [php]if ( prefix_is_extension() ) { // Do something }[/php] 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

  • July 12, 2013 @ 02:10:03 [Current Revision] by PeterLugg
  • July 12, 2013 @ 02:07:10 by PeterLugg

Revision Differences

July 12, 2013 @ 02:07:10Current Revision
Content
 Added: As teaken from: <a href="http:// wpninjas.com/ create-your- own-conditional-tag/" target="_blank">WP Ninjas</a>.
 Added: Have you ever wanted to do something based on a specific set of conditions. Sure you have and you’ve probably used  the <a title="Conditional Tags" href="http:// codex.wordpress.org/ Conditional_ Tags">WordPress Conditional Tags</a> at least once or twice. Conditional Tags are extremely useful functions that check a set of conditions for you.
 Added: They work like this…
 Added: [php]if ( is_single() ) {
 Added: // Do Something
 Added: }[/php]
 Added: 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.
 Added: Here is an example of something we use on this site. We use <a title="Easy Digital Downloads" href="http:// wpninjas.com/ a/easy-digital- downloads/">Easy Digital Downloads</a> to sell our products. All of these products, at least at the moment, are <a title="Ninja Forms Extensions" href="http:// wpninjas.com/ downloads/category/ ninja-forms/">extensions of Ninja Forms</a>. 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…
 Added: [php]global $post
 Added: if( 'download' == get_post_type() ){
 Added: $terms = get_the_terms( $post-&gt;ID, 'download_category' );
 Added: if( $terms ) {
 Added: foreach( $terms as $term ) {
 Added: if( 'extension' == $term-&gt;slug ) {
 Added: // Do something
 Added: break;
 Added: }
 Added: }
 Added: }
 Added: }[/php]
 Added: 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.
 Added: 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.
 Added: [php]function prefix_is_extension() {
 Added: global $post;
 Added: $response = false;
 Added: if( 'download' == get_post_type() ){
 Added: $terms = get_the_terms( $post-&gt;ID, 'download_category' );
 Added: if( $terms ) {
 Added: foreach( $terms as $term ) {
 Added: if( 'theme' != $term-&gt;slug ) {
 Added: $response = true;
 Added: break;
 Added: }
 Added: }
Deleted: Added: }
 Added: }
 Added: return $response;
 Added: }[/php]
 Added: 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.
 Added: [php]if ( prefix_is_extension() ) {
 Added: // Do something
 Added: }[/php]
 Added: 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.
 Added: 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.

Note: Spaces may be added to comparison text to allow better line wrapping.

Tags:

No comments yet.

Leave a Reply