In the previous article, the wordrpess hook is used. the filter is equally important. it is also an important interface for plug-in development. the filter principle is not as good as the hook, but the function is different. The "action hook"
In the previous article, the wordrpess hook is used. the filter is equally important and is also an important interface for plug-in development. the filter principle is not as good as the hook, but the function is different, "Action hook" I used to add an action because the filter is also called "filter hooks ). The action hook function is more inclined to execute a series of functions, and the filter function is used to output a variable to give you a chance to modify it.
In contrast to the action hook, the basic filter functions also have two: apply_filters () \ add_filter. these functions are located in the wp-include/plugin. php file.
The pply_filters () function is also used to create and execute a filter, which is the same as the action parameter. However, both of its parameters are required because the filter function is used to change the value of a variable, therefore, one or more variables must be passed. Add_filter () attaches a function to the hook. these functions must have a return value. if you modify a parameter and modify it, an error is returned.
That is to say, if a variable provides a modification filter, you can use the function to modify it N times before output. This most common application should be the seo plug-in, which uses filters to modify page information.
Here, we will use a simple example to describe the filter. in the future, we will provide several instance applications.
Well, let's modify the title information of the website. The output title generally uses the function wp_title (); this function is located in line 528th of the wp-shortdes/general-template.php file. Let's look at the function. The first part is some judgments to determine the current page and the title of the current page. The following code is displayed in row 3:
1 |
$ Title = apply_filters ('WP _ title', $ title, $ sep, $ seplocation ); |
Next to this sentence is to determine whether to output the statement. Here, a filter named wp_title is created and executed, and three parameters are passed. $ titile is the title, and $ sep is the front or back of the title, for example, "| ", $ seplocation determines whether the character $ sep ("|") is displayed on the left of the title or on the right of the title. Here, apply_filers passes three parameters, but note that the main thing is to modify the $ titile variable, and the other two are additional parameters, the two parameters can be used for modification. although three parameters are passed, the return value can only be one? Can a function return three values?
Add the following code to the topic functions. php file to modify the $ titile information.
123456789 |
Add_filter ('WP _ title','m M _ title', 10, 3); function custom_title ($ title, $ a, $ B) {if ($ B = 'right') {$ title = 'tree studio '. $ a;} else {$ title = $ B. 'Tree studio ';} return $ title ;} |
OK. In this way, the title value is successfully modified. This article may have only one instance, but it may not be detailed enough. if you have any questions, you can check the previous article, the action hook and filter hook should be the same in many places.
The filter hook also has other functions, such as has_filter () current_filter () merge_filters () remove_filter () remove_all_filters ()... If you want to continue, go to the official website.