How to Use Hooks in WordPress?

In our previous tutorial, we have published PHP Code Snippets. In this post, we have published about Hooks in WordPress.

If your are in WordPress development, then you can not ignore the importance of hooks. Suppose you want to add some functionality to your WordPress project, then you will need to edit some core functionality of WordPress. But WordPress doesn’t allow you to do this. In that case you need to use Hooks to add functionality to WordPress.

So now question arises what’s Hooks in WordPress. The hooks are actions and filers which is used to add or modify  functionalities in WordPress. So here in this post you will learn about WordPress Hooks and its types.

Types Of Hooks in WordPress

There are two types of Hooks in WordPress; actions and filters


About WordPress Actions:

Actions are the hooks that the WordPress core launches at specific points during execution, or when specific events occur. Your plugin can specify that one or more of its PHP functions are executed at these points, using the Action API.

Actions are triggered by specific events that take place in WordPress, such as publishing a post, changing themes, or displaying a page of the admin panel. Your plugin can respond to the event by executing a PHP function, which might do one or more of the following:

  • Modify database data
  • Send an email message
  • Modify what is displayed in the browser screen.

Now we will create a function to know actions in real scenario

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:
<?php
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://phpzag.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:
<?php
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.

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:
<?php
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’)

Here you I have given general description to create hooks in WordPress. you can also go for details in WordPress Actions list where you find a lot of actions hooks. you can also create your own here.

You may also like: