Actions and Filters in WordPress

In WordPress there are two types of hooks can be used: filters and actions. By events in WordPress the action hooks are triggered. For example, When a new post is published an Action hook is triggered. To modify the WordPress content before saving it into the database or displaying it to the screen the Filter hooks are used. Let’s suppose a Filter hook is available for the content of the page or post. It means that you can alter that content after it is retrieved from the database but before it is displayed in your browser.

Remember that Filter hooks modify content so look at an example of a Filter hook. In this example you will modifies the post content in Wordpreass:

<?php add_filter( ‘the_content’, ‘prowp_function’ ); ?>

For execution the Filter action the add_filter() function is used. For your post content here you are using the filter called the_content. So when every time the content is displayed, here will needs to pass through your custom function and this called my_cutom_function().

There are four parameters accept by this add_filter() function:

1.  filter_action (string): It is for the filter to use

2. custom_filter_function (string): It is for the custom function to pass the filter through

3.  priority (integer): It is for the priority in which this filter should run. While multiple callback functions are attached to the same hook, then the priority parameter determines the execution order

4.  accepted args (integer): It is for the number of arguments the function accepts Below is an example of the_content filter in action:

add_filter( ‘the_content’, ‘prowp_profanity_filter’ );
function prowp_profanity_filter( $content ) {
$profanities = array( ‘Web’, ‘design’ );
$content= str_ireplace( $profanities, ‘[Web Agency London]’, $content );
return $content;

Here the prowp_profanity_filter() function will replace the words “web” and “design” with [Web Agency London] automatically on all the pages and post on your website. The PHP function str_ireplace() you are using to handle the replacement. So this function will replace the some characters in a string with other characters in a string. It is case-insensitive also. A Filter hook you using, so the content isn’t actually modified or update in the database; so it’s modified during processing of the_post() function, and before it being the displayed, when this filter is create. In the database the content is not affected so the words “web” and “design” will still save in your content, and if you ever disable or change the plugin, all those words will display in the displayed text.

The data always receive the Filter hooks; in this case, the variable $content is passed to your function and carry your post content. The last line of the example is returns the $content variable. So you should to remember that you must always return the content when you are updating or else it returns empty and therefore displays nothing.

So in the above example you have seen the Filter hook. No let’s see at the Action hook and what it can do. In WordPress the Action hook is triggered by events. In the Action hook function there is no return value is required in WordPress. The structured of the Action hook is exactly like a Filter hook which you have read, as you can see in the following code:

<?php add_action( ‘hook_name’, ‘prowp_function’ ); ?>

In this code function is used add_action the add_action() function accepts four parameters it same like the add_filter() function. So here you can set the hook name that you want to hook into and the custom function name which you will execute that when the event is triggered, and the number of accepted args and the priority. Below is a real example it using an Action hook:

add_action( ‘comment_post’, ‘prowp_email_new_comment’ );
function prowp_email_new_comment() {
wp_mail( ‘’, ‘New blog comment’,
‘There is a new comment on your website Web Design Agency:’ );

So in this example the in the add_action the first parameter is the comment_post Action hook. What this mean it will triggered this action whenever a new comment is posted in WordPress. And the custom function you can see the prowp_email_new_comment() function, it will send an e-mail any time a new comment is created in the post or page. In the Action hook there is no require to sending any variables to your function or returning any values out of your function. If needed to return any value, you can pass values into your function.