I\'ve read WordPress codex many times but still don\'t understand how to return the value if more than one arguments involved. For example:
function bbp_get_
add_filter
and apply_filters
First thing to know is that apply_filters
returns its second argument, try this in functions.php
:
echo apply_filters('cat_story', 'A cat'); // echoes "A cat"
Second thing to know is that before apply_filters
returns "A cat", it applies filters where the "A cat" can be modified, add filters with add_filter
:
function add_chasing_mice($cat) {
return $cat . ' is chasing a mice';
}
add_filter('cat_story', 'add_chasing_mice');
echo apply_filters('cat_story', 'A cat'); // echoes "A cat is chasing a mice"
Third thing to know is that we can add multiple filters:
// #1
function add_chasing_mice($cat) {
return $cat . ' is chasing a mice';
}
add_filter('cat_story', 'add_chasing_mice');
// #2
function add_something_else($cat) {
return $cat . ', but it\'s not gonna catch it';
}
add_filter('cat_story', 'add_something_else');
echo apply_filters('cat_story', 'A cat'); // echoes "A cat is chasing a mice but it\'s not gonna catch it"
Forth thing to know is that you can apply filters in specific order:
// #1
function add_chasing_mice($cat) {
return $cat . ' is chasing a mice';
}
add_filter('cat_story', 'add_chasing_mice', 10); // 10 - is priority
// #2
function add_something_else($cat) {
return $cat . ', but it\'s not gonna catch it';
}
add_filter('cat_story', 'add_something_else'); // 10 as well, if omitted
// The filter will be applied before `add_chasing_mice` and `add_something_else`
function replace_the_cat($cat) {
return 'A dog';
}
add_filter('cat_story', 'replace_the_cat', 9); // 9 < 10, so the filter will be applied first
echo apply_filters('cat_story', 'A cat'); // echoes "A dog is chasing a mice but it's not gonna catch it";
Fifth thing to know is that you can pass additional arguments to your filters:
function add_chasing_mice($cat) {
return $cat . ' is chasing mice';
}
add_filter('cat_story', 'add_chasing_mice', 10); // 10 - is priority
function add_something_else($cat, $exclam, $wft) {
return $cat . ', but it\'s not gonna catch it' . $exclam . $wft;
}
add_filter('cat_story', 'add_something_else', 10, 3); // 3 arguments
function replace_the_cat($cat) {
return 'A dog';
}
add_filter('cat_story', 'replace_the_cat', 9); // 9 < 10, so the filter will be applied first
echo apply_filters('cat_story', 'A cat', '!!!', '!1wTf!?');
// 3 arguments are: 'A cat', '!!!', '!1wTf!?'.
// echoes "A dog is chasing a mice but it's not gonna catch it!!!!1wTf!?";