Home > Backend Development > PHP Tutorial > Analyzing the use of hooks in WordPress

Analyzing the use of hooks in WordPress

*文
Release: 2023-03-18 17:08:02
Original
2275 people have browsed it

This article mainly introduces the role and basic usage of function hooks in analyzing WordPress. Hooks are an important usage of calling functions in WordPress and are also the basis for plug-in development. I hope to be helpful.

The plug-in mechanism of WordPress is actually just this Hook. Its Chinese name is translated into a hook, which allows you to participate in the running of the core of WordPress. It 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 ​​actions is to let you perform some functions in a situation or a special location, such as sending an email, etc.; filters allow you to modify a value that the WordPress core needs to use, and then WordPress Then use these values ​​to do something, such as the return value of the function, etc.

Action hook

wp_head is a very commonly used action hook. During the theme development process, developers will add the wp_head() function to the head tag. In fact , it is this function that calls the wp_head hook.

If a plug-in developer wants to add a sentence to 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 page 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. We 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 hook

Based on my personal experience, maybe Filter hooks can be a bit more 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( &#39;<a&#39;, &#39;<a target="_blank"&#39;, $content );//添加 target="_blank"
  return $content;//必须要把过滤后的内容返回回去,否则值就丢了
}
add_filter( &#39;the_content&#39;, &#39;Bing_autoblank&#39; );
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, the action and the filter are a common global variable. That is to say, the filter and the action 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(), that is, it needs to receive the return value of this function every time it is called, and finally returns 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 using the add_action() and add_filter() functions Mount function.

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"><?php var_dump( $GLOBALS[&#39;wp_filter&#39;] ); ?>
Copy after login


The above code will print out the hook list.

20151222154645343.png (388×432)

Related recommendations:

How to make general settings in WordPress

Practical Tutorial on Using WordPress to Develop WeChat Mini Programs

Used to create and obtain sidebars in WordPress PHP function examples explained

The above is the detailed content of Analyzing the use of hooks in WordPress. For more information, please follow other related articles on the PHP Chinese website!

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