What is the difference between WordPress Actions and Filters

Home \ Coding \ What is the difference between WordPress Actions and Filters
WordPress Actions and Filters

What makes WordPress stands out as one of the most popular CMS ecosystems is the extendibility of its core code. Any WordPress developer can modify the behaviour of WordPress or even add as many features as she can, without touching the core code of WordPress. This is thanks to WordPress hooks.

WordPress hooks are pre-defined points in the WordPress core code where developers can use them to inject an additional code or modify a variable. WordPress hooks have two types of hooks, Actions and Filters.

WordPress Actions

WordPress actions are benchmarks where the developer need to execute a specific code at a specific event. For instance, if a developer wants to make WordPress publish the post to social networks at the same moment the administrator publish the post. He only needs to add the publishing code to the pre-defined action save_post. Luckily, WordPress has a very comprehensive documentation. Hence, most of the hooks are well documented with usage examples.

To add a piece of code to execute or to “hook” when WordPress publish a post. All what you have to do is this:

<?php
add_action("save_post", "publish_to_facebook");
function publish_to_facebook( $post_id ){
  // You can add any code here and it will be executed when the user save s a post.
}

This code can be added in a plugin or in the functions.php file of the theme.

add_action is a WordPress function used to register another function to be executed at the save_post hook which is the first parameter and the function name to be executed as the second parameter.

You can also add your own action in your plugin or theme, this will give the ability to other developers to extend your plugin or theme easily without touching the plugin’s code.

You may add your own action like this:

<?php
do_action('my_own_action');

Simple and clean, right? You can add that line at a place of the code where do you think that it would be useful. Such as updating the plugin settings, displaying a shortcode  … etc.

WordPress Filters

WordPress filters have the same idea as actions, but the main difference is that filters are used to modify variables. Unlike actions, filters code must return a value, which is the modified copy of the original value. For example, when WordPress display the title of each post. We might need to modify the title by capitalizing it or any other modifications we want.

The filter which we should use in this case is the_title. You can find a list of the pre-defined filters hooks in the WordPress codex. All relevant links are listed below.

<?php
add_filter("the_title" , "capitalize_post_titles" );
function capitalize_post_titles( $post_title ){
   $title = ucwords(  $post_title  );
   return $title; 
}

add_filter is a WordPress function used to register a filter to be executed on the_title hook attached to the $post_title variable, and the function name to be executed as the second parameter.

You can also add your own filter in your plugin or theme, this will give the ability to other developers to modify the plugin’s variables without editing the plugin’s code.

<?php
$posts_count = 5;
$posts_count = apply_filters( "the_posts_count", $posts_count );

apply_filters will execute any filter registered by the filter name the_posts_count and pass the variable $posts_count to it. The result will be the return of the registered filters in other activated plugins and theme. If there are no filters registered, the $posts_count value won’t change.

Conclusion

To summarize, WordPress hooks are a great way to extend the WordPress functionality efficiently without worrying about WordPress upgrades and not to scratch your head around the core code. I always recommend placing hooks into Plugins and Themes, As WordPress developers will be very grateful if they find that your plugin is already shipped with actions and filters. This will give your WordPress product more scalability and publicity.

ABOUT THE AUTHOR: Waseem

Founder and Lead Developer of WPRuby.

2 thoughts on “What is the difference between WordPress Actions and Filters

LEAVE YOUR COMMENT

Your email address will not be published.

Hurry!! Save 30% OFF any plugin license. Claim Our Black Friday Sale Now by using our time limited coupon: BLACKRUBY GET 30% OFF
Hurry!! Save 30% OFF Any Plugin License. Claim Our Black Friday Sale Now By Using Our Time Limited Coupon: BLACKRUBY GET 30% OFF