Concept explanation and analysis of WordPress hook

Source: Internet
Author: User

Intermediary transaction http://www.aliyun.com/zixun/aggregation/6858.html ">seo diagnose Taobao guest cloud host technology Hall

A lot of people look at other people's Plug-ins, the theme of the production will be mentioned action hooks, filter What, I did not know what the hook is, after a long time to learn slowly understand, today I say what is a hook. English is called hook--translation: hooks, hooks. Now, by example, what's the matter? WordPress hook, the general theme in the header.php will be added function Wp_head (), and some will also add wp_footer () function in footer.php, And many plug-ins will be reminded that if the theme code does not Wp_head () function may not use plug-ins (to find out what a function is, you can Baidu, you will be a general translation, if you understand English words to the official website wordpress.org to search, there are authoritative introduction). Take the Wp_head () function for example:

PHP wp_head ()?>

This function is located in the 1577 line of the wp-includes/general-template.php file, and you see the definition of this function:

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

This function is a simple sentence do_action (' Wp_head '), the do_action () function can be interpreted as: to perform an action, here to perform a call called Wp_head action, the inside of the parameter wp_head is not a function, but only a name. We call this action hook, this is called Wp_head action can contain a lot of functions, we can customize a number of functions so that it belongs to the Wp_head action, can be portrayed as a function of the image to hang on this action oh. Ha ha.. This is my understanding ... The Do_action () function is described as follows:

<?php do_action ($tag, $arg); $tag required, the name of the action hook you want to create//$arg the value passed to the existing action hook?>

To be exact, the do_action () function is to create an action and execute it, because the action to be performed does not need to be stated as a variable, do_action (' Wp_head ') is directly creating an action called Wp_head and then executing it. Instance:

<?php do_action (' Kutailang '); Create an action called Kutailang and execute it, and then you can hang the function on this hook ...?>

This action is now created, but the action does not contain any functions, so performing this empty action will not perform any function, so I'm going to hang a function on this action, using the Add_action () function to hang the function on the action Hook, add_action () Introduction:

<?php add_action ($tag, $function _to_add, $priority, $accepted _args); $tag parameter required, the name of the action hook///$function _to_add parameter is required, the function//$priority parameter to be executed is int (integer), the smaller the number the first call//$accepted _ar GS parameter is int (integer), the number of parameters required by the function, default 1;?>

Our example needs to add code:

<?php add_action (' Kutailang ', ' Kutailang_echo ', 88); To hang the function Kutailang_echo on the name Kutailang action//below define functions Kutailang_echo function Kutailang_echo () {echo ' I am the bitter wolf        Ah </br> '; }?>

This performs do_action (' Kutailang ') and executes all functions that the Kutailang contains. The Wp_head () function mentioned above executes the action called Wp_head, many plug-ins need to load JS and CSS files or code on the page, it needs to use this function in the head of the page to output the corresponding code. Why not use the Wp_footer function to output JS code, JS is not placed at the bottom of the page better? Because general topics have wp_head () functions, the Wp_footer function is not necessarily. The functions associated with the action hook are not just these two, but Has_action () Do_action_ref_array () remove_action () remove_all_action () to search the 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.