WordPress Hooks action and filters Part -2

How does WordPress add hooks?

Custom hooks are created and called in the same way that Core’s hooks are, with add_action() / do_action() and add_filter() / apply_filters(). Since any plugin or theme can create a custom hook, it’s important to prefix your hook‘s name to avoid collisions with other plugins.


function wp_publish_post($post) {
<--- stuff that publishes the post --->
do_action('wp_insert_post', $post=>ID, $post, true);
}

Lets take an example of a WordPress function wp_publish post() which. also defines a hook called wp_insert_post() inside of its functon wp_publish post(). You can tie your won function to these hooks and they will be executed when the script reaches the execution point. So whenever WordPress publishes a post it takes necessary steps and just before it is published the WordPress give us the hook which is wp_insert() using the do_action() function. So any function tied to this will be executed in this point.

Variable_hooks

Means there can be number of hooks define for combination. Below is an example of variable hook in comment.php. Where publish_to_trash will run when a published post is trashed. future_to_draft will run when a scheduled post is set to a draft status.


do_action( "comment_{sold_out}_to_{new_status}" .comment);

Action

Actions are called with function do_action(). Let’s take the syntax of do-action().

// single argument 

do_action($tag, $arg);

// multiple arguments

add_action($tag, $arg, $arg2..);

// multiple arguments are passed in form of an array 

do_action_ref_array($tag, $array_arg);

do_action ($tag, $arg) : Whatever is hooked onto the do action hook is called by the do_action() function. It can accept single parameter or multiple parameters. The first parameter is $tag which will be the hook name and the second parameter is $arg(argument).

do_action($tag, $arg1, $arg2) : It also accepts multiple argument as you can see. It can be $arg1, $arg2 so on a so forth.

do_action($tag, $array_arg) : This function takes multiple arguments in form of an array. So instead of taking $arg1, $arg2 it take an $array_arg[an array of arguments].

do_action() are defined at certain point in WordPress so that you can insert our codes. As we discussed in the first example with wp_insert_post. So if we have any function register with this action it will get executed.


add_action() : Let’s check the syntax of add_action() first.

add_action($tag, $function_to_add, $priority = 10, $accepted_arg = 1)
  • $tag : $tag(string) name of the action to which the $function_to_add is to be hooked( It’s in a string format and required).
  • $Function_to_add : Custom function.
  • $priority : $priority(int) Specifies the order in which the function associated with a particular action are executed(If there are two functions latched to the same action. Than $priority decides which function will be executed first. The lower $priority number are executed first. And if two function have the same priority number, the one which is declared first will be executed first).
  • $accepted_arg : $accepted_arg(int) Number of arguments the function accepts. Default value is always 1(but some $tags i.e wp_insert_post can behave otherwise)

How to find out the number of $arg for an action?

To find out the number and name for an argument search the codebase for a matching do_action() call. You can also click on the link and click on the action name. I will give you the information for which WordPress files its do_action() is defined in.

If you click on View all hooks it should take you to a page that looks like something we have below. And if you click on one of the hook it should tell you the location of where exactly is it defined.

If you click on View action hooks. It should take you take page that looks like something we have below. We also have View filter hook option. It also gives you the version number

So all you do it go into that particular file and define the do_action function before the activated plugin.


Using add_action with class.

To use add-action when your plugin or theme is build using classes. You would pass your function to add_action() as an array with $this as the first element. Which refers to the name of your class. Than the name of the class method as mentioned below.

/**
 *  Class WP_Docs_Class
 */

Class WP_Docs_Class {

/**
 * Constructor
 */
    public function __construct() {
        add_action( 'save_post', array( $this, 'wpdocs_save_posts'));
        }
/**
 * Handle saving post data
 */
    public function wpdocs_save_posts() {
        // do stuff here    
    }
}

$wpdocsclass = new WP_Docs_Class();

Using add_action() with static function of a class.

If the class is called statically than we would use get_called_class in place of $this. That’s because $this wont be available. get_called_class returns the name of the class. This also works if class is extended. Let’s see how.

/**
 *  Class WP_Docs_Class
 */

Class WP_Docs_Class {

/**
 * Constructor
 */
    public function __construct() {
        add_action( 'save_post', array( get_called_class(), 'wpdocs_save_posts'));
        }
/**
 * Handle saving post data
 */
    public function wpdocs_save_posts() {
        // do stuff here    
    }
}

$wpdocsclass = new WP_Docs_Class();

We are using add_action() using the hook function and are using get_called_class.

remove_action()

Removes a function attached to a specified hook. This method can be used to remove default functions attached to a specific filter hook and possibly replace them with a substitute.

remove_action($tag, $func_to_remove, $priority = 10);
  • $tag : (string)(required) The action hook to which the function to be removed is hooked.
  • $function_to_remove : (callable)(required) The name of the function to be removed.
  • $priority : (int)(optional) The priority of the function. Default value = 10.

i.e

remove_action('the_content', 'function_name' );

Removing function attached to a hook defined in class.

global $my_class;
remove_action('the_content', array($my_class, 'class_filter_function'));

If an action has been added form within the class. i.e by a plugin. Removing it will require accessing the class variable. It is also worth nothing that you may priorities the removal of the action to a hook that occurs after the action is added. You cannot successfully remove the action before it has been added.

How do we find out what is the default $priority or what are the functions attached to this particular hook.


Thank you!

Leave a Comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: