This article undertakes the previous article: wordpress hook, to talk about what is a filter, filter English: Filters, is the meaning of filtering. A filter is also called a filter hook. Similar to an action hook, a filter has two basic functions: the action Hook has do_action and the apply_filters function. It also creates and executes a filter function, the difference is that the action hook only executes some functions, and the filter needs to change the value of an object or variable, which is equivalent to filtering the object or variable and then returning it. How does apply_filters () work:
- <? Php
- Apply_filters ($ tag, $ value );
- // $ Tag required. Name of the filter you want to create
- // $ Value is required. The value that can be modified by this filter is attached (parameter)
- // These two parameters are required and can be viewed on the official website with parameters later.
- ?>
Similarly, an action Hook has add_action function, and a filter hook also has add_filters function. The usage of these two functions is the same:
- <? Php
- Add_filter ($ tag, $ function_to_add, $ priority, $ accepted_args );
- // $ Tag required, hook name
- // $ Function required, called function
- // Priority
- // The number of accepted parameters. The default value is 1.
- // Note that the filter added with add_filter must have a return value.
- ?>
It should be noted that the filter mentioned above is to change the value of the object or variable, so the filter added by add_filter must have a return value, that is, the function $ function_to_add must have a return value. Example: in the default topic header. the <title> </title> label in php contains a function wp_title (); this function is defined in the same way as wp_head () in the wp-shortdes/general-template.php file, located in row 528, the content between row 529 and row 607 is always determined by various if statements to define the value of variable $ title. The key is row 609:
- $ Title = apply_filters ('wp _ title', $ title, $ sep, $ seplocation); // create a filter and execute the filter wp_title. The variable to be changed is $ title, this is the previous article that defines the $ title variable and then provides a filter for you to change the value of $ title.
- // This is the role of the filter.
Application Instance, change $ title:
- <? Php
- Add_filter ('wp _ title', 'ash _ titlename ');
- // Add the filter wp_title, or put the function ash_titlename on the filter hook wp_title.
- // Define a function
- Function ash_titlename ($ title ){
- // Change the value of $ title to bitter Wolf
- $ Title = 'bitter wolf ';
- Return $ title;
- // Remember the return value
- }
- ?>
The above Code sets the value of $ title returned by the wp_title () function as a bitter wolf. The output by calling the wp_title () function is too bitter, No matter what page it is, add it to the function of the topic. php, you can see the effect (If your topic uses the wp_title function ). Through this instance, you should understand what a filter is. Search on the official website. The filter-related functions include has_filter () current_filter () merge_filters () remove_filter () remove_all_filters ()...
The article comes from the bitter wolf blog