Uses Of Hooks In WordPress

Jun 20, 2013, by admin

What is hooks ?

WordPress hooks are actions and filers which is used to add or modify WordPress functionalities. So here in this post we will discucss WordPress Hooks and its types that’s used during WordPress Plugin development.

Types of wordpress hooks

Filters and actions are two types of hooks. As you saw in the previous section, a filter modifies the value of something. An action, rather than modifying something, calls another function to run beside it.

Create an Action Function

The first step in creating an action in your plugin is to create a PHP function with the action functionality of your plugin, and put it in your plugin file. For example, if you want your friends to get an email message whenever you create a new post, you might define the following function:

function email_to_friends($post_ID)  {
    $friends = ‘dev@example.org,dev2@example.org’;
    mail($friends, “your blog updated”,
    ‘I want to something on my blog: http://Bugtreat.com’);
    return $post_ID;
    }

Hook Action to WordPress

After your function is defined, the next step is to “hook” it with WordPress. In the example above, call add_action() in the global execution space of your plugin file and put the following line in the plugin file:

add_action ( ‘publish_post’, ‘email_to_friends’ );

Install and Activate

The last step in getting your action hook to work is to install the file and activate the plugin. The PHP function you wrote and the add_action call must go into a PHP file together, and the PHP file must be installed in the wp-content/plugins directory. Once it is installed, you will need to visit the admin section of WordPress and activate your plugin.
About WordPress Filters:

Filters are functions that WordPress passes data through, at certain points in execution, just before taking some action with the data . Filters sit between the database and the browser, and between the browser and the database. most input and output in WordPress passes through at least one filter. WordPress does some filtering by default, and your plugin can add its own filtering.

The basic steps to adding your own filters to WordPress:

Create a Filter Function

A filter function takes as input the unmodified data, and returns modified data. If the data is not modified by your filter, then the original data must be returned so that subsequent plugins can continue to modify the value if necessary.

For example, if you want to make sure that your posts and comments contain no profanity, you might define a variable with a list of forbidden words, and then create the following PHP function:

function do_filter_profanity($content) {
    $profanities = array(‘badword’,‘alsobad’,‘…’);
    $content=str_ireplace($profanities,‘{censored}’,$content);
    return $content;
    }

Hook in your Filter

After your function is defined, the next step is to “hook” or register it with WordPress. To do this, call add_filter() in the global execution space of your plugin file:

In the example above, we would put the following in the main executing section of the plugin file, to tell WordPress to filter comments for profanity:

add_filter(‘comment_text’,‘do_filter_profanity’);

Install and Activate Filters

The last step in getting your filter hook to work is to install the file and activate the plugin. The PHP function you wrote and the add_filter() call must go into a PHP file together, and the PHP file must be installed in the wp-content/plugins directory. Once it is installed, you will need to visit the admin section of WordPress and activate your plugin; see Managing Plugins for more details.

Removing Actions and Filters

In some cases, you may find that you want your plugin to disable one of the actions or filters built into WordPress, or added by another plugin. You can do that by calling

remove_filter(‘filter_hook’,‘filter_function’)

or

remove_action(‘action_hook’,‘action_function’)