WordPress Actions, Filters, and Hooks : A guide for non-developers

Hooks are provided by WordPress to allow your plugin to ‘hook into’ the rest of WordPress; that is, to call functions in your plugin at specific times, and thereby set your plugin in motion.In WordPress code there are numerous pre-defined actions or hooks that allow developers to add their own code at these points.

There are two kinds of hooks:

  1. Actions
  2.  Fitters

WordPress Actions:-

  • Actions allow you to add extra functionality at a specific point in the processing of the page—for example, you might want to add extra widgets or menus, or add a promotional message to your page.
  • The first step in creating an action in your plugin is to create a PHP function with the action functionality of your plugin,
  • You can then add this function to an action that is in the spot where you would like your copyright code to be executed.
function copyright_notice() {
   echo "Copyright All Rights Reserved";
}
add_action('wp_footer','copyright_notice');
In this example, copyright_notice is an action hooked into wp_footer hook. The function copyright_notice will be executed whenever the wp_footer() hook appears in a WordPress theme code.

WordPress offers a list of available actions in their Plugin API.

Filter hooks, or filters, control how something happens or change something that’s already being output. You could use a filter to output metadata in a specific format, to override text output by your plugin, or to prevent something from being displayed at all.

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.

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.

Social Share

9 thoughts on “WordPress Actions, Filters, and Hooks : A guide for non-developers”

  1. I think that is among the mst vital information for me.

    And i’m satisfied studying your article.
    But wanna statement on few normal things, The web site style is perfect, the articles is in point of fact excellent : D.
    Excellent task, cheers

    Reply
  2. Heya! I’m at work browsing your blog from my new apple iphone! Just wanted to say I love reading through your blog and look forward to all your posts! Keep up the fantastic work!

    Reply

Leave a Comment