Parse the functions and basic usage of function hook in WordPress. _ PHP Tutorial

Source: Internet
Author: User
Parse the functions and basic usage of function hook in WordPress ,. Parse the function hook function and its basic usage in WordPress. the WordPress plug-in mechanism is actually only the Hook, which is translated into a hook in Chinese, allow you to use WordPres to parse the function hook function and its basic usage in WordPress,

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.
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.

// 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.

// 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 ');

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.

// 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 ('

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.

Get the current hook list
WordPress actions and filters are the core part of the plug-in mechanism. you can add actions that you want to execute in specific places. Generally, you can use add_action () and add_filter () functions to Mount functions.

These hooks are stored in the $ wp_filter global variable. therefore, to obtain the hook list, you can directly obtain the $ wp_filter global variable.

<?php var_dump( $GLOBALS['wp_filter'] ); ?>

The above code will print out the hook list.

Articles you may be interested in:
  • PHP code instance for page number navigation added to the article list page in a WordPress topic
  • Enable theme support gadgets and add plug-ins in WordPress
  • Describes the basic methods for compiling simple code format tags in WordPress.

The plugin mechanism of WordPress is actually only the Hook. it is translated into a Hook in Chinese and allows you to participate in WordPres...

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.