In this series, we're going through one of the fundamental features of WordPress: Conditional Tags. In this fourth part, we'll continue introducing and reviewing the Conditional Tags. Be sure to check out the previous parts if you haven't yet.
Let's begin!
27. Checking Whether the Page Is a "Monthly Archives" Page: is_month()
In WordPress, the most commonly used date-based archive type is probably the monthly archives. When you need to detect whether your code is running in a monthly archive page, the Conditional Tag is_month()
will help you.
Accepted Parameters
This Conditional Tag doesn't accept any parameters.
28. Checking the Current Theme's Features: current_theme_supports()
While developing plugins, you might need to check whether the active theme allows thumbnails, post formats, custom headers or even widgets. With the help of current_theme_supports()
, you can check whether the theme in use supports the feature you specify.
Accepted Parameters
This Conditional Tag has only one parameter:
$feature
(string, optional): Name of the features. (Default: None)
Feature names:
- 'post-thumbnails'
- 'post-formats'
- 'custom-header'
- 'custom-background'
- 'menus'
- 'automatic-feed-links'
- 'editor-style'
- 'widgets'
- 'html5'
Usage Example for current_theme_supports()
Let's say you're developing a plugin specifically for WordPress themes with the "custom backgrounds" feature. Your plugin will be useless for themes which don't support custom backgrounds, so you need to display an error message in the administration panel to inform the user. Here's what you do:
<?php
add_action( 'admin_notices', 'custom_bg_feature_warning' );
function custom_bg_feature_warning() {
if ( ! current_theme_supports( 'custom-background' ) ) {
echo '<div class="error">
<p>' . __( 'This plugin requires a theme with the "custom backgrounds" feature enabled.', 'translation-domain' ) . '</p>
</div>';
}
}
?>
29. Checking Whether the Specified Plugin Is Active: is_plugin_active()
Working in the administration panel only, the Conditional Tag is_plugin_active()
helps your code know whether the given plugin is active. This Conditional Tag can be useful if you're writing "addon plugins" for popular plugins (like WooCommerce) or base frameworks installed as plugins (like the Redux Framework).
Accepted Parameters
This Conditional Tag has only one parameter:
$plugin
(string, required): Plugin's or sub-directory's name. (Default: None)
30. Checking Whether the URL Is a Local Attachment: is_local_attachment()
WordPress attachments are a blessing when used properly, but how do you find out whether a URL is actually an attachment? If you're working with URLs while developing, the Conditional Tag is_local_attachment()
might be very useful for you: It checks whether the given URL is uploaded to the same WordPress installation as an attachment.
Accepted Parameters
This Conditional Tag has only one parameter:
$url
(string, required): URL to check. (Default: None)
Usage Example for is_local_attachment()
Since this conditional tag is pretty straightforward, the example will be straightforward, too. Imagine you're developing a plugin and you need URLs (generated by another function you created) to be checked to see whether they're local attachments or not:
<?php
$url = some_url_generator_function();
if ( is_local_attachment( $url ) ) {
_e( 'Yep, this is a local attachment!', 'translation-domain' );
} else {
_e( 'Nope, this is not a local attachment.', 'translation-domain' );
}
?>
31. Checking Whether the Page Is a Time-Based Archive Page: is_time()
Did you know that WordPress has hour-based, minute-based and even second-based archives? (To be honest, I didn't know that you could create these kinds of archives before writing this series. Writing at Tuts+ can be an educational journey, too!)
It doesn't come out of the box, which means you can't type in http://ift.tt/1RBdtbi
and get archives for 18:30 on February 22, 2014; but you can create one with the help of the WP_Query
class. Anyway: If you want to detect these kinds of time-based archives, you can use the is_time()
Conditional Tag.
Accepted Parameters
This Conditional Tag doesn't accept any parameters.
32. Checking Whether the Current Locale Is RTL: is_rtl()
In some languages, mostly in the countries of the Middle East, text is written from right to left instead of left to right. As a WordPress developer, you must consider all WordPress users, and if you want your theme or plugin to work nicely with WordPress websites with RTL text, you should use is_rtl()
to determine whether the locale is RTL.
Accepted Parameters
This Conditional Tag doesn't accept any parameters.
Usage Example for is_rtl()
Most web designers create separate stylesheets for RTL locales—it's a common practice. And if you want to load your RTL stylesheet conditionally, here's what you do:
<?php
add_action( 'wp_enqueue_scripts', 'mytheme_enqueue_styles' );
function mytheme_enqueue_styles() {
if ( is_rtl() ) {
wp_enqueue_style( 'style-rtl', plugins_url( '/css/style-rtl.css', __FILE__ ) );
}
}
?>
33. Checking Whether the Page Is a Custom Taxonomy's "Archives" Page: is_tax()
The Conditional Tag is_tag()
can detect tag archives, but it can't work with custom taxonomies. To determine whether a taxonomy archive page is being displayed, you can use is_tax()
. By default, it includes all taxonomy archive pages, including regular tags. But it has two parameters, so you can target specific taxonomy archive pages, or even specific terms.
Accepted Parameters
This Conditional Tag has only one parameter:
$taxonomy
(array/string, optional): Taxonomy slug or an array of slugs. (Default: None)
$term
(array/string/integer, optional): Term's ID, name, slug or an array of those. (Default: None)
34. Checking Whether the Page Is an Attachment Page: is_attachment()
While creating a plugin (or theme), you might want to know when an attachment page is being displayed. The Conditional Tag is_attachment()
is the one that will help you: It returns TRUE
in an attachment page and FALSE
anywhere else.
Accepted Parameters
This Conditional Tag doesn't accept any parameters.
Usage Example for is_attachment()
Let's say you want to display a "Back to the post" link above the attachment. Here's what you do:
<?php
if ( is_attachment() ) {
// Get attachment's parent.
$parent = get_post_field( 'post_parent', $id );
// Get permalink of the parent post.
$parent_link = get_permalink( $parent );
// Echo the link.
echo '<a href="' . $parent_link . '" class="parent-post-link">' . __( 'Back to the post', 'translation-domain' ) . '</a>';
}
?>
35. Checking Whether the Given Term Exists: term_exists()
This is the second Conditional Tag that doesn't return TRUE
and FALSE
, but more useful information: The Conditional Tag term_exists()
checks whether a term of a taxonomy exists, and returns the term ID (if no taxonomy is specified) or an array of the taxonomy and term pairing instead of TRUE
. If the specified term doesn't exist, it returns 0
or NULL
.
Accepted Parameters
This Conditional Tag has three parameters:
$term
(string/integer, required): Term's name or ID. (Default: None)
$taxonomy
(string, optional): Taxonomy's name. (Default: Empty)
$parent
(integer, optional): Parent term's ID (to confine the search under it). (Default: 0)
36. Checking Whether the Post Has the Given Term: has_term()
We saw that we could use has_tag()
if we wanted to find a tag in a post. What if we wanted to do the same thing, but with a term of a custom taxonomy? Easy: The Conditional Tag has_term()
has a third parameter (second in order) that allows us to specify a custom taxonomy.
Fun fact: Even the Conditional Tag has_tag()
uses has_term()
to return TRUE
or FALSE
! And as with the has_tag()
function, has_term()
also allows you to specify more than one term (but in only one taxonomy).
Accepted Parameters
This Conditional Tag has three parameters:
$term
(array/string/integer, optional): Term's name, ID, slug, or an array of those. (Default: Empty)
$taxonomy
(string, required): Taxonomy's name. (Default: Empty)
$post
(object/integer, optional): Post to check. (Default: NULL
)
Usage Example for has_term()
Let's say you run an e-commerce website and you're going to change your supplier for Dell laptops, so you want to inform your users that shipments will be delayed until next week. Here's what you do:
<?php
if ( has_term( 'dell', 'laptops' ) ) {
echo '<div class="above-product-warning"><strong>' . __( 'Warning', 'translation-domain' ) . ':</strong> ' . __( 'We\'re switching suppliers for Dell laptops, so the shipments will be delayed until next week. Sorry about that!', 'translation-domain' ) . '</div>';
}
?>
37. Checking Whether It's a Trackback (and We're in 2007): is_trackback()
Greetings, old-timer! You want to find out if a comment is a "trackback"? The is_trackback()
Conditional Tag will help you with that!
Accepted Parameters
This Conditional Tag doesn't accept any parameters.
38. Checking Whether "WordPress Multisite" Is in Use: is_multisite()
While being a not-so-popular feature of WordPress, Multisite comes in very handy when necessary. If you want your code to run in normal WordPress installations and behave differently in Multisite installations, the Conditional Tag is_multisite()
can help you detect whether Multisite support is enabled.
Accepted Parameters
This Conditional Tag doesn't accept any parameters.
Usage Example for is_multisite()
Let's say you're developing a plugin, and if Multisite is enabled, the plugin requires another class from your plugin. Here's what you do:
<?php
if ( is_multisite() ) {
require_once( 'path/to/multisite-class.php' );
}
?>
39. Checking Whether the Page Is a Post Type(s) Archive: is_post_type_archive()
Sometimes, you might want your code to know whether an archive page for a custom post type (or a couple of custom post types, or any custom post type) is being displayed. The Conditional Tag is_post_type_archive()
does exactly that: It checks whether the query is for an archive page for a custom post type.
Accepted Parameters
This Conditional Tag has only one parameter:
$post_types
(array/string, optional): Name of the post type, or an array of post type names. (Default: None)
Conclusion
In this part, we reviewed another batch of the 65 documented Conditional Tags in WordPress. In the next parts, we're going to go through the remaining 26. If you have any questions or comments, shoot them below—and if you liked this article, don't forget to share it!
See you in the next part!
by Barış Ünver via Tuts+ Code