Analysis of the use of hooks in WordPress

Source: Internet
Author: User
This paper mainly introduces the functions and basic usage of the function hook in WordPress, and hook is the important usage of calling function in WordPress, and it is also the foundation of plug-in development. We hope to help you.

WordPress plug-in mechanism is actually just this hook, it is translated into a hook, it allows you to participate in the operation of the core WordPress, is a very good thing, let us learn more about it.
Hook classification

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

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

Action Hooks

Wp_head is a very common action hook, in the development of the theme, the developer will add the Wp_head () function in the head tag, in fact, it is this function called Wp_head hooks.

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


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, look at the front page source code to see what we added in the Head tab.

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


Encountered 404 page to send administrator mail function bing_404_page_mail () {if  (!is_404 ()) return;//exit function if not 404 page  $to = get_option (' Ad Min_email ');//Get admin address  $subject = ' encountered 404 page! ';//mail header  $message = ' 404 page Address: '/'. $_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 experience, the filter hooks may be difficult to understand, especially for those unfamiliar with PHP.

Filter hooks allow 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 that was used to invoke the content of the article, which provides a the_content filter.

To add a function to the the_content hook, this function needs to receive a parameter, which is the current value.


Article content full link New window open function Bing_autoblank ($content) {//$content variable is the content of the article, because other filters are also filtered, so this content may be filtered by other functions  $content = Str_replace (' <a ', ' <a target= ' _blank "', $content);//Add target=" _blank "  return $content;//must return the filtered content back, Otherwise the value is lost}add_filter (' the_content ', ' Bing_autoblank ');


Hook principle

In fact, when calling Add_action () and Add_filter (), just add an array element to the $WP _filter global variable, and also say that the action and the filter is a common global variable, that is, the filter and action can not duplicate the name.

When Do_action () is called, it looks for functions that are added to this action in the global variable of the $WP _filter, and the loop executes.

Apply_filters () A step more than do_action (), that is, each time the function is called to receive the return value of the function, and finally the number of filtered values are returned for use.

Get list of current hooks
WordPress's actions and filters are a core part of the plug-in mechanism, allowing you to proactively add the actions you need to perform in a specific place, typically using the add_action () and Add_filter () function mount functions.

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


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


The above code will print out a list of hooks.

Related recommendations:

How to make general settings in WordPress

Using WordPress to develop a small program in real-life tutorial

Examples of PHP functions used to create and get sidebar in WordPress

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.