The Ultimate Guide About the WordPress Hooks

WordPress Hooks are API (Application Programming Interface) which are provided by WordPress to allow your plugin to “hook-in” to your site; to call functions in your plugin when required and thus kick off the plugin.

Tech-babble aside, this is one of the reasons WordPress gives a lot of free reign to even the novice developers. Hooks allow you to change the behavior of code that isn’t yours without making any changes to it. This is how plugins work.

There are two types of wordpress hooks: Actions and Filters.

Actions are functions triggered by specific events. Filters are API though which WordPress passes data before it is supposed to do something else. Think of Action function as ‘doing’ or ‘executing’, and Filter function as ‘last minute editing before printing’.

Also Read:

WordPress typically does some filtering of its own. For instance, ‘the_title’ filter does its work every time WordPress outputs a post’s title. Now any plugin or theme that works on making that title look different can hook that filter and perform as their code instructs.

Where Does this Code Go?

For the kind of code we’re talking about here, there are two choices; You can add the code to your child theme functions.php file, or to a custom plugin (which goes to wp-content/plugins directory) you can create. Note that this does NOT include your parent theme (or any other theme) functions.php file. The whole point of wordpress hooks is to modify the behavior of code with changing it directly.

There are several advantages to adding this code to your custom plugin instead of child theme functions.php.

  • You can create small plugins for specific goals, which makes it easier to keep track of them.
  • You can use them on multiple sites.
  • Testing becomes easier when you have to enable/disable individual plugins instead of modifying the entire code.
  • The plugins stay with you even if you change a theme.

But it’s entirely up to you which method you use. Let’s begin with using Actions and Filters.

Note: In the following code examples, I have chosen the prefix ‘slug’. Make sure you always change it to unique slug for the plugin/theme you are working with.

Use of WordPress Hooks: Actions and Filters

Using Actions:

Think of Actions as the point where a piece of code takes a breather while another one runs.

Here’s what an action function hooked to WordPress looks like:

add_action( ‘wp_head’, ‘slug_change_h3_color’ );
function slug_change_h3_color() {
if ( is_category( ‘news’ ) || is_tag( ‘important-updates’) ) {
echo ‘<style>h3, h3 a { color: red;}</style>’;
}
}

The first line uses add_action() to hook/register the function  slug_change_h3_color()with WordPress. add_action() is a WordPress action function, and has two required parameters which tell the code WHEN to run (Example: ‘wp_head’) and WHAT to do (Example: ‘slug_change_h3_color’). The custom function slug_change_h3_color is the callback.

It’s not necessary to write the callback function immediately after your action hook. As long as it’s somewhere it can be executed, it’s good to go. I added it here because it makes for slightly-more-easily-digestible code.

The hook (first line of code) and the custom function slug_change_h3_color()go in the same .php file if you’re writing the code as part of a plugin. Make sure you install it correctly so it can be activated on admin section of WordPress.

There are loads of actions in WordPress core and even more are added by additional themes and plugins. Most commonly used in customized themes are wp_head and wp_footer. wp_enqueue_scripts is great for adding CSS and JavaScript files to your site.

Using Filters:

Filters are like actions, but they sit around waiting until they are called at a specific time to alter a data before it reaches its destination (read: completes execution).

Here’s what a hooked filter function looks like:

add_filter( ‘the_content’, ‘slug_add_copyright’ );
function slug_add_copyright( $content ) {
//get current post
global $post;

//create copyright notice using post date and site name
$date = get_the_date( ‘y’, $post->ID );
$copyright = ‘<p>Copyright ‘.$date.’ by ‘.get_bloginfo( ‘name’ );’.</p>’;

//append copyright notice to post content and return
$content = $content.$copyright;
return $content;

}

In the above example, add_filter() registers/hooks the function to WordPress and generates copyright before publishing the post on the browser. It’s similar to action function above, the only difference being that it provides us data for modifications. The function uses the data as a parameter. In the last line of this callback we return the variable. If we don’t, there will be no post –copyrighted or otherwise.

WordPress filters, like actions, are pretty popular. the_content allows you to modify your content before it is output to the browser and published on your site for the general audience. A lot of social sharing plugins use this filter for great effect.

WordPress Plugin API (Actions and Filters) make for a vast topic that requires comprehensive study and experimentation. I barely scratched the surface with this post. So check out WordPress codex for Actions and Filters, look up some more sources and tutorials about wordpress hooks, and start coding. It’s the only way to get the most out of one of WordPress’s more robust tools.

Author Bio: 

This is a guest post witten by Tracey Jones. She is a WordPress developer by profession and works with HireWPGeeks Ltd. She handles a team of experienced WordPress developers who are expert in providing WordPress customization service at very reasonable cost with 100% client satisfaction guarantee.

Muhammad Imran
 

I am Muhammad Imran (Engineering graduate), a professional blogger, SEO analyst, and webmaster with over 9 years of blogging experience, managing multiple blogs in different niches related to blogging, affiliate marketing, SEO, and product reviews.

Click Here to Leave a Comment Below 5 comments