Wordpress filter details and Analysis

Source: Internet
Author: User

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:

  1. <? Php
  2. Apply_filters ($ tag, $ value );
  3. // $ Tag required. Name of the filter you want to create
  4. // $ Value is required. The value that can be modified by this filter is attached (parameter)
  5. // These two parameters are required and can be viewed on the official website with parameters later.
  6. ?>

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:

  1. <? Php
  2. Add_filter ($ tag, $ function_to_add, $ priority, $ accepted_args );
  3. // $ Tag required, hook name
  4. // $ Function required, called function
  5. // Priority
  6. // The number of accepted parameters. The default value is 1.
  7. // Note that the filter added with add_filter must have a return value.
  8. ?>

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:

  1. $ 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.
  2. // This is the role of the filter.

Application Instance, change $ title:

  1. <? Php
  2. Add_filter ('wp _ title', 'ash _ titlename ');
  3. // Add the filter wp_title, or put the function ash_titlename on the filter hook wp_title.
  4. // Define a function
  5. Function ash_titlename ($ title ){
  6. // Change the value of $ title to bitter Wolf
  7. $ Title = 'bitter wolf ';
  8. Return $ title;
  9. // Remember the return value
  10. }
  11. ?>

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.