Analyze the role and basic usage of function hooks in WordPress, _PHP tutorial

WBOY
Release: 2016-07-12 09:02:35
Original
811 people have browsed it

Analysis of the function and basic usage of function hooks in WordPress.

The plug-in mechanism of WordPress is actually just this Hook. It is translated into Chinese as a hook, allowing you to participate The operation of WordPress core is a great thing, let’s learn more about it below.
Hook classification

Hooks are divided into two types, one is called action, and the other is called filter. The implementation principles of these two hooks are basically the same. As will be mentioned later, the difference in usage is that filters have return values, but actions do not.

The idea of ​​an action is to let you perform some functions in a situation or a special location, such as sending an email, etc.; a filter allows you to modify a value that the WordPress core needs to use, and then WordPress uses these Values ​​do something, such as the return value of a function, etc.

Action hook

wp_head is a very commonly used action hook. During theme development, 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 plugin developer wants to add a sentence in the head tag, they can use the wp_head hook. Here is a simple example.

//在 head 标签添加一些内容
function Bing_add_head_tag(){
  echo '添加内容';
}
add_action( 'wp_head', 'Bing_add_head_tag' );
Copy after login

After adding the code, view the source code of the front-end web page, and you can see the content we added in the head tag.

20151222154540066.png (437×83)

The above is a simple example, just printing a sentence. Using this hook, we can also make a plug-in that sends an email to the administrator when encountering a 404 page. I simply wrote one below.

//遇到 404 页面给管理员发送邮件
function Bing_404_page_mail(){
  if( !is_404() ) return;//如果不是 404 页面就退出函数
  $to = get_option( 'admin_email' );//获取管理员地址
  $subject = '遇到 404 页面啦!';//邮件标题
  $message = '404 页面的地址:http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];//邮件内容
  wp_mail( $to, $subject, $message );//发送邮件
}
add_action( 'wp_head', 'Bing_404_page_mail' );
Copy after login

Filter Hooks

Based on my personal experience, filter hooks may be difficult to understand, especially for people who are not familiar with PHP.

Filter hooks allow you to change the value of something. The filter callback function will accept a parameter, which is the current value. Remember the the_content() function used to call the content of the article. This function provides a the_content filter.

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

//文章内容全部链接新窗口打开
function Bing_autoblank( $content ){//$content 变量就是文章内容,因为其它过滤器也要过滤,所以这个内容可能是经过其它函数过滤的
  $content = str_replace( '<a', '<a target="_blank"', $content );//添加 target="_blank"
  return $content;//必须要把过滤后的内容返回回去,否则值就丢了
}
add_filter( 'the_content', 'Bing_autoblank' );

Copy after login

Hook Principle

In fact, when calling add_action() and add_filter(), only an array element is added to the $wp_filter global variable. What’s more, actions and filters are a common global variable, that is to say, Filters and actions cannot have the same name.

When do_action() is called, it will search for the functions added to this action in the $wp_filter global variable and execute them in a loop.

apply_filters() has one more step than do_action(), which is to receive the return value of this function every time it is called, and finally return the value that has been filtered multiple times for use.

Get the current hook list
WordPress actions and filters are the core part of the plug-in mechanism, allowing you to actively add actions you need to perform in specific places. Generally, you use the add_action() and add_filter() functions to mount functions.

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

<pre class="brush:php;toolbar:false"><&#63;php var_dump( $GLOBALS['wp_filter'] ); &#63;>
Copy after login

The above code will print out the hook list.

20151222154645343.png (388×432)

Articles you may be interested in:

  • PHP code example for adding article list page page number navigation in WordPress theme
  • Enabling theme to support gadgets and adding plug-in enabling functions in WordPress
  • Detailed explanation of the basic method of writing shortcode format tags in WordPress

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1084563.htmlTechArticleAnalysis of the role and basic usage of function hooks in WordPress. The plug-in mechanism of WordPress is actually just this Hook. , it is translated into Chinese as a hook, allowing you to participate in WordPress...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template