: This article describes the PHP functions used to create and add filters in WordPress. For more information about PHP tutorials, see. Apply_filters () (create a filter)
The apply_filters () function is used to create a filter. most of the filters are used in functions. it is a very important function in the WordPress plug-in mechanism, allowing other themes and plug-ins to modify and filter values.
Usage
apply_filters( $tag, $value, $var... );
Parameters
$ Tag
(String) (required) name of the filter.
Default value: None
$ Value
(Mixed) (required) the value to be filtered. If no one is filtered, this value is returned directly.
$ Var
(Mixed) (optional) additional variable parameters are passed to the filter function. the auxiliary filter function operates on the returned values and can add unlimited values.
Return value
(Mixed) filtered values. If no one filters the values, the $ value is directly returned.
Example
No one filters:
Echo apply_filters ('test', 'modifiable value ');
Print result:
Modifiable values
Someone filters:
Function test_func () {return 'modify the value';} add_filter ('test', 'Test _ func'); echo apply_filters ('test ', 'modifiable value ');
Print result:
Modify value
Receiving parameters:
Function test_func () {return 'modify the value';} add_filter ('test', 'Test _ func'); function test_func2 ($ text) {return $ text. '2';} add_filter ('test', 'Test _ func2 '); echo apply_filters ('test', 'modifiable value ');
Multiple parameters:
Function test_func ($ text, $ var, $ var2) {return 'modify value '. $ var1. $ var2;} add_action ('test', 'Test _ func', 10, 3); echo apply_filters ('test', 'modifiable value ', 'auxiliary value 1', 'auxiliary value 2 ');
Others
This function is located in: wp-uplodes/plugin. php
Add_filter () (add filter)
Add_filter () can be used to mount a function to the specified filter.
Usage
add_filter( $tag, $function_to_add, $priority, $accepted_args );
Parameters
$ Tag
(String) (required) name of the mounted filter (same as the $ tag attribute of the target apply_filters () function ).
Default value: None
$ Function_to_add
(Callback) (required) the callback function to be mounted. for details, refer to the PHP callback function type document.
Default value: None
$ Priority
(Integer) (optional) execution sequence. smaller functions are executed first.
Default value: 10
$ Accepted_args
(Integer) (optional) number of parameters received by the callback function. multiple parameters can be received by more apply_filters () functions.
Default value: 1
Return value
(Boolean) always True
Example
Function test_func ($ text, $ var1, $ var2) {return $ text. $ var1. $ var2;} add_action ('test', 'Test _ func', 10, 3); echo apply_filters ('test', 'parameter 2', 'parameter 3 ', 'parameter 4 ');
Print:
Test parameter 2 parameter 3
Others
This function is located in: wp-uplodes/plugin. php
The above describes the related PHP functions used to create and add filters in WordPress, including related content. I hope to help anyone who is interested in the PHP Tutorial.