A Walkthrough on Conditional Tags in WordPress: Introduction

One of the most important strengths of WordPress is the extensibility of the core. With plugins and themes, WordPress users have been able to mold their websites for almost a decade. (WordPress was first released in 2003, but plugins were introduced in 2004 and themes were introduced in 2005.) And to create such a solid infrastructure, WordPress includes lots of handy sub-systems (functions, classes or whole APIs). One of them is "Conditional Tags", which allow our code to function differently in particular situations.

In this series, we're going to learn about these Conditional Tags. We're going to start off with the definition and the importance of Conditional Tags in this post. And in the next parts, we're going to go through Conditional Tags with a description and some examples.

Let's begin!

What Are Conditional Tags?

In the Codex, Conditional Tags are described like this:

The Conditional Tags can be used in your Template files to change what content is displayed and how that content is displayed on a particular page depending on what conditions that page matches.

You get the idea: In order to make your code use and/or alter the content, you use Conditional Tags and tell your code the type, state and place of the content. Imagine your code and WordPress having a conversation:

  • Your Code: Hey man, I need some help.
  • WordPress: Sure, I'm all ears. What do you need?
  • Your Code: I'm going to wrap these post titles with some DIVs, but I need to know if these are on a category archive page. Are these on a category archive page?
  • WordPress: TRUE
  • Your Code: Um... What?
  • WordPress: I mean yes.
  • Your Code: That's great, thank you!
  • WordPress: Bye!

So, in short, Conditional Tags are boolean statements that steer your code to understand where it is, when used inside an if/else statement. They only return TRUE or FALSE, and your code only needs these two boolean values.

How to Use Conditional Tags

While Conditional Tags are a pretty important part of WordPress development, it's amazingly simple to use them. Since they only return TRUE or FALSE, you can use them inside if statements without any hassle. (Actually, there are three exceptional Conditional Tags that return FALSE or a value, and we'll get to them in the next parts, but you can use them in if statements, too.)

Let's have a quick example of how a Conditional Tag works:

Get it? We used the Conditional Tag for the if statement and we told WordPress that if it's the homepage, this piece of code will echo a—somewhat dull—welcome text. It's really not that big of a deal.

Let's have another example, with some "cleaner" code:

See what we did? We created a variable and defined the Conditional Tag in it; so we were able to use the variable for the if statement. Piece of cake!

Example Scenarios for Using Conditional Tags

Believe me when I say there's an infinite number of cases for using Conditional Tags. Off the top of my head, I can give you five scenarios in which you can make use of Conditional Tags:

  1. Imagine that you're developing a social sharing plugin for WordPress and you want to give your users to option to show and hide the widget under posts and pages. With a combination of is_single(), is_page() and is_singular(), you can create a function that checks the user's plugin settings and, say, hides the widget on pages but shows them under each post.
  2. Let's say that you're developing a theme for a small company. You're working on the "News" page (the "blog" part of the theme) and you designed a slick post listing with thumbnails... but you know that they will forget or choose to not use a thumbnail for some posts. That's where has_post_thumbnail() comes in handy: Use it and your theme will check if the post does not have a thumbnail and display a default image.
  3. Suppose you're creating an add-on plugin for a popular WordPress plugin. You need to detect that the main plugin is installed and being used because your plugin may cause problems if a novice user installs it without using the main plugin. The solution is simple: Using is_plugin_active(), you can disable your plugin's functionality, and using is_plugin_inactive(), you can display a warning in the admin area.
  4. You created a theme for another client and they want to upload images, PDF documents and ZIP archives to their posts—but they also want to display all images under each post. Simply using the Conditional Tag wp_attachment_is_image() will let you pick over the images and show them under the posts.
  5. Say you're making a plugin for multi-author blogs and you want to detect if the user's website has more than one author. The Conditional Tag is_multi_author() gives you the answer.

Conclusion

As you can see, Conditional Tags are one of the easiest features of WordPress to use, but also one of the most important parts of theme and plugin development.

The aim of this series is to introduce Conditional Tags and we're just getting started. In the next five articles, we're going to go through 65 different Conditional Tags with descriptions, cases of use, and examples for some of them.

See you in the next part!

Tags:

Comments

Related Articles