Simple usage and principle of WordPress plug-in mechanism (hook)

Source: Internet
Author: User

The WordPress plug-in mechanism is actually just a hook, which is translated into a hook in Chinese and allows you to participate in the running of the WordPress core. It is a great thing, let's take a closer look at it.

PS: This article is just a simple summary. For more methods, refer to the functions provided in this Article.

Hook category

There are two types of hooks: Action and filter ). The implementation principles of these two hooks are basically the same. As we will talk about later, the difference in usage is that the filter has a returned value, but the action does not.

The idea of action is to allow you to execute some functions in one case or in a special location, such as sending an email. A filter is a value that is required to change the core of WordPress, wordPress then uses these values to do some things, such as the return values of functions.

Action hook

Wp_head is a very common action hook. During the development of the topic, developers will add the wp_head () function to the head tag. In fact, it is this function that calls the wp_head hook.

If the plug-in developer wants to add a sentence to the head tag, they can use the wp_head hook. Below is a simple example.

12345
// Add some content in the head tag function bing_add_head_tag () {echo 'add content';} add_action ('wp _ head', 'bing _ add_head_tag ');

After adding the code, you can view the source code of the web page on the front-end to view the added content in the head tab.

The above is a simple example, just printing a sentence. With this hook, we can also create a plug-in that sends an email to the Administrator on the 404 page, and write a simple one below.

123456789
// Send an email to the Administrator on the 404 page. Function bing_404_page_mail () {If (! Is_404 () return; // exit the function $ to = get_option ('admin _ email ') if the page is not 404 '); // obtain the Administrator address $ subject = '. The 404 page is displayed! '; // Mail title $ message = '2017 page address: http ://'. $ _ server ['HTTP _ host']. $ _ server ['request _ URI ']; // wp_mail ($ to, $ subject, $ message); // send email} add_action ('wp _ head ', 'bing _ 404_page_mail ');

For more information about add_action (), see add_action ().

Filter hook

According to my personal experience, it may be difficult to understand filter hooks, especially for PHP users who are not familiar with them.

The filter hook allows you to change the value of something. The filter callback function accepts a parameter, which is the current value. Remember the the_content () function used to call the content of the article. This function provides a_content filter.

Add a function to the_content hook. This function needs to receive a parameter, which is the current value.

123456
// Link all the content of the article to a new window. OPEN function bing_autoblank ($ content) {// $ content variable is the content of the article, because other filters also need to be filtered, therefore, this content may be $ content = str_replace ('<A',' <a target = "_ blank" ', $ content) filtered by other functions ); // Add target = "_ blank" return $ content; // The filtered content must be returned; otherwise, the value is lost.} add_filter ('the _ content ', 'bing _ autoblank ');

For more information about add_filter (): WordPress function: add_filter () (Add filter)

Call hook

The core of WordPress is how to call these hooks. You can refer to the following two articles.

Action HOOK: WordPress function: do_action () (execution action)

Filter HOOK: WordPress function: apply_filters () (create filter)

Hook Principle

In fact, when add_action () and add_filter () are called, an array element is added to the $ wp_filter global variable. The action and filter are public global variables, that is to say, the filters and actions cannot be named again.

When you call do_action (), you can find the functions added to the $ wp_filter global variable and execute them cyclically.

Apply_filters () has one more step than do_action (), that is, each time you call a function, you must receive the return value of this function, and finally return the filtered value for use.

 

Reference from: http://www.endskin.com/hook.html

Simple usage and principle of WordPress plug-in mechanism (hook)

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.