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' );
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.
#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' );
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( '<a', '<a target="_blank"', $content );//添加 target="_blank" return $content;//必须要把过滤后的内容返回回去,否则值就丢了 } add_filter( 'the_content', 'Bing_autoblank' );
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['wp_filter'] ); ?>
The above code will print out the hook list.
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!

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python are both high-level programming languages that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

WebStorm Mac version
Useful JavaScript development tools

Atom editor mac version download
The most popular open source editor

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software