Home>

apply_filters () (create filters)The apply_filters () function is used to create a filter.Most are used in functions,Is a very important function in the wordpress plugin mechanism,Allows other themes and plugins to modify and filter a value.

usage

apply_filters ($tag, $value, $var ...);

parameter

$tag

(String) (required) The name of the filter.

Default:none

$value

(Mixed) (required) values ​​to filter,If no one is filtering, this value is returned directly.

$var

(Mixed) (optional) additional variable parameters to pass to the filter function,The auxiliary filter function operates on the return value,You can add unlimited.

return value

(Blended) filtered values,If no one is filtering, the value of $value is returned directly.

example

No one filters:

echo apply_filters ("test", "values ​​that can be modified");

Print results:

Can be modified

Someone filter:

function test_func () {
  return "Modify value";
}
add_filter ("test", "test_func");
echo apply_filters ("test", "values ​​that can be modified");

Print results:

Modify value

Receive parameters:

function test_func () {
  return "Modify value";
}
add_filter ("test", "test_func");
function test_func2 ($text) {
  return $text. "2";
}
add_filter ("test", "test_func2");
echo apply_filters ("test", "values ​​that can be modified");

Multiple parameters:

function test_func ($text, $var, $var2) {
  return "Modified value". $var1. $var2;
}
add_action ("test", "test_func", 10, 3);
echo apply_filters ("test", "values ​​that can be modified", "auxiliary value 1", "auxiliary value 2");

other

This function is located at:wp-includes/plugin.php

add_filter () (add a filter)add_filter () can attach a function to a specified filter.

usage

add_filter ($tag, $function_to_add, $priority, $accepted_args);

parameter

$tag

(String) (required) the name of the mounted filter (same as the $tag attribute of the target apply_filters () function).

Default:none

$function_to_add

(Callback) (required) the callback function to be mounted,Refer to the php callback function type documentation.

Default:none

$priority

(Integer) (optional) execution order,The smaller the function is executed first.

Default value:10

$accepted_args

(Integer) (optional) the number of parameters received by the callback function,Set multiple parameters that can receive 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

other

This function is located at:wp-includes/plugin.php

php
  • Previous Android and JavaScript call each other
  • Next Java servlet combined with mysql to build java web development environment
  • Trends