WordPress Filter Detailed and Analysis

Source: Internet
Author: User

Intermediary transaction http://www.aliyun.com/zixun/aggregation/6858.html ">seo diagnose Taobao guest cloud host technology Hall

This article to undertake an article: WordPress hook, say what is filter, filter English: Filters, is the meaning of filtration. The filter is also called the filter hook, it and the action hook, the basic also has two functions, the action hook has do_action it has the Apply_filters function, is also creates and executes a filter function, different is the action hook only executes some functions, The filter is to change the value of the object or variable, which is equivalent to filter the object or variable, and then return. How does Apply_filters () work:

<?php apply_filters ($tag, $value); $tag required, the name of the filter you want to create//$value required, hanging in this filter can be modified values (parameters)//These two parameters are required, the following can also take the parameters of the road to see?>

The same action hook has a function of add_action, and the filter hook also has a function add_filters. The use of these two functions is the same:

<?php Add_filter ($tag, $function _to_add, $priority, $accepted _args); $tag required, hook name//$function required, call function//priority//Accept number of parameters, default 1//Note that the filter added with Add_filter must have a return value? &   Gt;

Note that the filter 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 has to have a return value. Example: There is a function wp_title () in the <title></title> tag within the header.php of the default theme, and the definition of this function is the same as Wp_head () in wp-includes/ general-template.php file, located in line 528, from 529 lines to 607 of the content has been through a variety of if statements to define the value of the variable $title, the key is 609 lines:

$title = apply_filters (' Wp_title ', $title, $sep, $seplocation);//This creates a filter and executes the filter wp_title, the variable to be changed is $title, This is a big piece of the front to define the $title variable, to the back of a filter to you, so you can change the value of $title//This is the role of the filter

Application example, change $title:

&lt;?php add_filter (' wp_title ', ' ash_titlename '); Add filter wp_title, or hang the function ash_titlename on the filter hook Wp_title//define function Functions Ash_titlename ($title) {//$ti            The value of tle to bitter wolf $title = ' bitter wolf ';            return $title; Be sure to remember the return value}?&gt;

The above code makes the return value of the Wp_title () function $title The value of the bitter wolf, call the Wp_title () function of the output is bitter wolf, no matter what page, add it to the theme of the function.php, you can see the effect of the ( If your theme uses the Wp_title function). With this example, you should understand what a filter is. Search in the official website, the filter related function also has has_filter () Current_filter () merge_filters () Remove_filter () remove_all_filters () ...

Article 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.