Wordpress function integration (2): Basic Knowledge-wordpress hook

Source: Internet
Author: User
To add features to a topic as you like, you must understand the hooks and filters of wordpress. Wordpress hooks and filters are also the basic knowledge of plug-in development.

To add features to a topic as you like, you must understand the hooks and filters of wordpress. Wordpress hooks and filters are also the basic knowledge of plug-in development.

We use examples to illustrate the wordpress hook. in the first basic tutorial, we said that we should add wp_head (); function to the header file of the topic. otherwise, some plug-ins will not work normally, this function is located in row 1577 of the wp-des/general-template.php file, the original function is as follows

 

function wp_head() {      do_action('wp_head');  }

You can see that this function calls the do_action function and passes in the "wp_head" parameter. if you are interested, you can continue to trace the do_action function, which is located in the wp-uplodes/plugin. php file. However, this function is complicated. I will not explain it here, but I just need to understand how to use it.

The do_action function is interpreted as follows: do_action executes an "action" and the action name is the parameter "wp_head". This wp_head action does not need to be defined. do_action also has a defined function, that is to say, we didn't need to define what the wp_head was and what it was going to do, but do_action defined an action named wp_head and executed it.

However, if there is a blank name, it is not recommended that we use do_action ('preview'). of course, the parameters here are hypothetical and cannot be used in Chinese. what should we do before meals?

Therefore, we also need a function: add_action (), where add_action and do_action are located in the same file.

The add_action () function can add specific transactions to the "action". for example, we have defined and executed an action "before meals". then we add_action ('before meals ', 'Wash your hands '), add specific content to this "before meals" action. in the program, the "wash hands" parameter should be a function name, that is, execute this function.

Therefore, the action in do_action is called an action hook, which can be used to hook objects. Therefore, add_action is used to mount a function to the hook and then execute the hook, that is, execute all the functions attached to this hook.

Function:

 

 

 

 

 

Note: The do_action and add_action functions have another parameter. we will explain the parameter application in the instance.

First LookSimple Example 1We use the wp_head () function to output a meta information from the wp header:

 ';}?>

 

We only need to add a function to the wp_head action and output the meta information. Where can I add the above function? Functions. php of the topic, or function files contained in other topics. okay, as long as the topic is executed.

This application is very simple. many plug-ins work like this to output css or js code.

Example 2:

This time we will pass the parameter through the hook. do_action ('hookname', $ arg_1, $ arg_2, $ arg_3) includes three parameters besides the hook name, and then add_action () in addition to the Hook name, function name, and priority, the rest is the number of parameters, that is, the number of parameters required for the affiliated function. do_action has passed three parameters, the accepted_args of the corresponding add_action can be 0, 1, 2, or 3. It is acceptable to accept less, but if it is more, it will be available.

For example:

 

 

 

You can see that an action hook called "ashu" is defined and executed, and three parameters are passed to the action function. then, add_action is used to mount two functions to the hook, the function1 function requires two parameters, and function2 requires three parameters. Therefore, when add_action is used, the number of parameters corresponding to this function is also 2 and 3. Then the priority is 10 and 1 respectively, so function2 is executed first.

I thought of this, and the use of the action is clear. However, there are not only these two functions about the action hook (hook), but also has_action () do_action_ref_array () remove_action () remove_all_action ().... If you want to learn more, you can go to the official website.

 

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.