Function Documentation

WordPress Function Documentation Progress

Writing Plugins: Does the filter or action exist?

This WordPress Plugin API feature is new to WordPress 2.5 and is quite interesting. Its application in plugins will be interesting to see when WordPress 2.5 is released.

The has_action() and has_filter() checks for the existent of hooks and whether a specific callback exists within the filter. This is useful, since it allows you to check whether anyone has added a callback to a specific hook. It also can prevent you from removing a callback that had already been removed.

Testing the Existence of a Hook

Hooks are only created when callbacks are added and in WordPress 2.5, when there remains at least one callback in the hook. If no callbacks are added to a hook or all callbacks are removed then both has_action($hook_name); and has_filter($hook_name); will return false.

This can be useful for creating your own filters for your plugin to see if anyone had hooked into your actions and filters. You can halt running the action or filter if there is nothing attached to the hook.

Removing Callbacks from Hooks

It used to be quite the process to test whether a callback existed and get the priority of the callback in order to remove it. These processes have been simplified in WordPress 2.5 and will make both finding and removing a known callback easier.

The key is that the callback has to be known. With classes, in 2.3.0 and the upcoming 2.5 release, there is a format for how classes are added as a callback. Fortunately, at least in 2.5 you don’t need to worry about the format. I’ll detail the format in a later post for those wanting to know in 2.3.x.

In WordPress 2.5 however, you only need to pass the same reference to the class and the method name. The way to get the reference, if you are removing the callback from another plugin’s class is to get the known global and use that reference.

If the plugin is known to cause problems with yours, then you should be able to get that information easily. It is easier if the offending plugin uses functions or static method calls as the original class reference variable is not needed.

Most of the hooks plugin authors will be removing will be WordPress internal ones, which don’t use classes. If this is the case, then the format is easier.

function myplugin_remove_offending_hook_after_test() {
    if( false !== ($priority = has_filter( 'pre_term_name', 'wp_filter_kses')) ) {
        remove_filter( 'pre_term_name', 'wp_filter_kses', $priority );
        // Hook another filter in its place.
    }
}

myplugin_remove_offending_hook_after_test();

When the second parameter is used, if the callback is found then the priority is returned. You can use this in the method above to remove the hook. As before, you must know the callback before hand.

The has_action() is used in the same way and as with add_filter() and add_action() it is always better to use the correct function for the hook you are referencing, even through they both use the same function.

Advertisements

January 12, 2008 - Posted by | Writing Plugins Series

2 Comments »

  1. Sadly this function breaks my admin completely.

    Comment by sum1 | March 31, 2008 | Reply

  2. Which function?

    Comment by Jacob Santos | May 9, 2008 | Reply


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: