Analysis of functions and basic usage of function hook hook in wordpress _php skill

Source: Internet
Author: User

WordPress plug-in mechanism is actually just this hook, it is translated into a hook, allowing you to participate in the WordPress core operation, is a very good thing, below we have a detailed understanding of it.
Hook category

There are two kinds of hooks, one called action, and the other is called filter. The two hooks are basically the same principle, and later on, the difference is that the filter has a return value, and the action does not.

The idea of action is to allow you to perform functions in a situation or in a particular place, such as sending an email, and the filter is a value that you need to modify the WordPress core, and then WordPress uses these values to do things like the return value of the function.

Action Hooks

Wp_head is a very popular action hook, in the development of the theme process, developers will be in the head tag to add the Wp_head () function, in fact, it is this function calls the Wp_head hook.

If the plugin developer wants to add a word to the head tag, you can use the Wp_head hook, which is a simple example below.

Add some content to the head tag
function Bing_add_head_tag () {
  echo ' add content ';
}
Add_action (' Wp_head ', ' Bing_add_head_tag ');

After adding the code, look at the front page source code to see what we've added in the head tag.

The top is a simple example, just a word printed. Using this hook, we can also do a 404 page to the administrator to send a message to the plugin, the bottom of the simple write A.

404 pages are encountered to send mail to the Administrator function
Bing_404_page_mail () {
  if (!is_404 ()) return;//exit the function if it is not a 404 page
  $to = get_ Option (' Admin_email ')//Get administrator address
  $subject = ' Encounter 404 pages! '//mail header
  $message = ' 404 page address: http://'. $_server[' http_host '. $_server[' Request_uri '];//Mail content
  wp_mail ($to, $subject, $message);//Send mail
}
add_action (' Wp_head ', ' bing_404_page_mail ');

Filter hooks

According to my personal experience, it may be difficult to understand the filter hooks, especially for people unfamiliar with PHP.

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

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

Article content all link New window open
function Bing_autoblank ($content) {//$content variable is the article content, because other filters also want to filter, so this content may be filtered by other functions
  $ Content = Str_replace (' <a ', ' <a target= ' _blank ', $content);//Add target= "_blank" return
  $content; The filtered content must be returned, otherwise the value will be lost
}
add_filter (' the_content ', ' Bing_autoblank ');

Hook principle

In fact, when Add_action () and Add_filter () are invoked, only an array element is added to the $WP _filter global variable, and the action and filter are common global variables, that is, filters and actions cannot be duplicate.

When you call Do_action (), you look for $WP _filter the functions that are added to this action in the global variable, looping through.

Apply_filters () is more than do_action () One more step, that is, every time the function is called to receive the return value of the function, and finally the value of the filtered back out for use.

Get the current hook list
WordPress's actions and filters are the core of the plug-in mechanism, allowing you to actively add the actions you need to perform in a particular place, typically using the add_action () and Add_filter () functions.

These hooks are stored in the $WP _filter global variable, so to get the hook list, you can get $WP _filter global variable directly.

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

The top code prints out a list of hooks.

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.