Bill: WordPress Action Hook function add_action (), do_action () source code parsing

Source: Internet
Author: User

WordPress commonly used two kinds of hooks, filter hooks and action hooks. Filter hook correlation function and source code analysis in the previous article completed, this article mainly analyzes the action hook source code.

However, after understanding the source code of the action hook, you will find that the action hook core codes are similar to filter hooks! Yes, so far, I have to tell you, the action Hook is just WP developer to distinguish the concept of the filter hook to name another thing! Of course, they still have some subtle differences, below we will be in-depth interpretation from the source code.

Action Hook Concept : The action Hook is the WP code execution to a certain place or an event occurs when a series of functions triggered by the plug-in can take advantage of the action Hook API in the WP code to execute a specific point before inserting a series of functions to control execution. It is very similar to the filter hook, the only difference is that the filter hook returns a processed value, and the action hook only completes the function execution does not return a value , if the hook does not exist then return null and add the hook.

Action Hook Principle : Because the action hooks and filter hooks are almost the same, so the principle of their implementation is the same. It mainly uses a global variable $wp_filter, adding an action function when the add_action () function is used to add an array element to the global variable $wp_filter, which contains the name of the hook, the value contains the corresponding function and execution priority information, in the call Do_ When using an action hook, the action () function loops through the loop to find all the functions associated with the hook and calls it sequentially, and finally returns the processed data.

Action Hook use steps

Since the PHP code will be translated by Zend and other engines, the sequence of steps in the code is not important, so the following steps are only for the purpose of understanding the hook principle of the pseudo-step, does not have the actual reference significance!

1, Create hooks (can be omitted): Using the Do_action () function can create a hook without a mount function, the Mount function can be added through Add_action (), and then use Do_action () call execution;

2, create the action function : It can have passed parameters can not pass in parameters, the other and the creation of ordinary functions without any difference, the function of the functions of the completion of an action;

3, mount function : That is, using add_action () to attach the function to the specified hook;

4. Perform action hooks : Use Do_action () to execute all functions mounted on the specified hooks sequentially to complete the specified task;

The action hook function is detailed:

When you look at Action hook function, parameter description, etc., you will find that it is almost repetitive with the filter hooks. As for why this, it depends on the source, I promise, after reading the source you will feel that you have been WP developer RINSE!

1 , Add_action ($tag, $function _to_add, $priority = ten, $accepted _args = 1)

add_action () Functions: This function is used to add the specified mount function $function_to_add to the specified action hook $tag, and it can determine the execution priority of the Mount function and the number of its acceptable parameters;

add_action () parameter Description :

$tag is the name of the hook;

$function _to_add is the name of the attached letter;

The optional parameter $priority the priority that the Mount function performs, the default is 10, the smaller the number is executed earlier, the same number is executed in the order that it is added to the hook, and the sooner the addition is executed;

The optional parameter $accepted_args determines the number of parameters that are received by the Mount function, which defaults to 1;

add_action () Source Analysis:

function add_action ($tag$function _to_add$priority$accepted _args = 1 ) {    return add_filter ($tag$function _to_add$priority$ Accepted_args);}

What's up, see it! It's got a hole in the wood! The Code of the Add_action () function is actually called once Add_filter ()! This Nima is entirely a matter of a man's name and nickname there's Wood there!

2 , Do_action ($tag, $arg = ")

do_action () functions: The function calls to mount all functions on the filter hook $tag to a completely specific task;

do_action () parameter Description:

$tag is the name of the hook;

$arg is an incoming parameter of the Mount function on the action hook, the default is empty;

do_action () Source Analysis:

functionDo_action ($tag,$arg= ' ') {    Global $WP _filter,$WP _actions,$merged _filters,$WP _current_filter; if( !isset($WP _actions) )        $WP _actions=Array();#If the $wp_actions variable is not set, it is defined as an array;    if( !isset($WP _actions[$tag]) )        $WP _actions[$tag] = 1; Else++$WP _actions[$tag]; #if $wp_actions[$tag] is not set, it is assigned a value of 1, otherwise its value is added 1;    if(isset($WP _filter[' All ']) ) {        $WP _current_filter[] =$tag; $all _args=Func_get_args(); _wp_call_all_hook ($all _args); }    #all hooks in apply_filters () are handled exactly the same way! _wp_call_all_hook () source analysis see the previous article Filter hooks source parsing;    if( !isset($WP _filter[$tag]) ) {        if(isset($WP _filter[' All ']) )            Array_pop($WP _current_filter); return; }    #The current hook does not exist, it is returned directly, no longer executes the code;    if( !isset($WP _filter[' All ']) )        $WP _current_filter[] =$tag; #set the current hook to $tag;    $args=Array(); if(Is_array($arg) && 1 = =Count($arg) &&isset($arg[0]) &&Is_object($arg[0]) )        $args[] =&$arg[0]; Else        $args[] =$arg; #do_action () If there is an incoming parameter, and is an array, the array is only one element, the element has a value to set the $args value to reference $arg[0], otherwise directly assigned value;     for($a= 2;$a<Func_num_args();$a++ )        $args[] =Func_get_arg($a); #with a For loop, if Do_action () has more than one incoming parameter, assign the values to the array $args;    if( !isset($merged _filters[$tag ] ) ) {        Ksort($WP _filter[$tag]); $merged _filters[$tag] =true; }    #It's exactly like the Apply_filter () function sort Code! See above for a detailed explanation;    Reset($WP _filter[$tag ] );  Do {        foreach( (Array) Current($WP _filter[$tag]) as $the _ )            if( !Is_null($the _[' function ']) )                Call_user_func_array($the _[' function '],Array_slice($args, 0, (int)$the _[' Accepted_args '])); }  while(Next($WP _filter[$tag]) !==false ); Array_pop($WP _current_filter);} #with the exception of one line return $value the other is exactly the same as apply_filters ()! 

See the source code of the action hook, is not exclaimed, the original this is the filter hook for a name son just!

Bill: WordPress Action Hook function add_action (), do_action () source code parsing

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.