Quick Tip: Checking for Filters and Actions

VN:F [1.9.22_1171]
Rating: 0 (from 0 votes)

Within WordPress, every once in a while you’ll need to see if a specific callback has been added to a specific action or filter. For instance, in my case, I needed to temporarily remove the callback that inserted the “Sharing” buttons via JetPack on some content I was re-purposing. In order to do that, I first had to check to see if that callback had been added to the appropriate filter, then remove it if it had, then put it back after I was done (so as not to mess up any other content on the page).

The nice thing is, much like the wp_script_is() function that I mentioned in some of my recent WordPress Themes presentations at various conferences, WordPress also provides two functions to help you determine whether a callback has been registered on an action or filter.

To determine whether a callback has been added to a filter, use the has_filter() function. To determine whether a callback has been added to an action, use the has_action() function.

Following is a brief example of how you might use one of these. In this example, I am outputting the featured image, along with its “caption” (post_excerpt) and “description” (post_content), and applying the “the_excerpt” and “the_content” filters to them, respectively. However, I don’t want the Sharing buttons from JetPack to appear below my image description, so I am removing it before applying any filters, then putting it back afterwards. It’s a quick & dirty example (parts of my original function have been stripped out for simplicity, etc.), but it should give you a rough idea of how to go about this.

VN:F [1.9.22_1171]
Rating: 0.0/5 (0 votes cast)

Leave a Reply