WPRuby Blog

Explore our blog for expert tips on WordPress plugins, development, and eCommerce solutions to grow your online business.

What is the difference between WordPress Actions and Filters

Blog Image

What makes WordPress stand 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 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 needs 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 publishes the post. He only needs to add the publishing code to the pre-defined action save_post. Luckily, WordPress has 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 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 saves 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 to 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 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, filter 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 filter 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 to 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 WordPress functionality efficiently without worrying about WordPress upgrades and not scratching 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.

 

6 comments

Leave a Reply

Your email address will not be published. Required fields are marked *