I have a question that I’m curious.
In WordPress we can add a filter or action like this code:
add_filter($filter_Name, $function_will_be_hook);
add_action($action_Name, $function_will_be_hook);
We can pass some parameters into function_will_be_hook(). But how does WordPress know that these parameters are related to its filters/actions.
Example:
function my_the_content_filter($content) {
$content .= "I added some additon content";
return $content;
}
add_filter( 'the_content', 'my_the_content_filter' );
How does WordPress know $content
is the content of Post/Page (even when we change name of this param to some other name)?
For every filter or action hook that you use with the
add_filter
oradd_action
functions, there is a correspondingapply_filters
ordo_action
function called somewhere in the WordPress core. This function sets up which parameters will be sent to the filter or action.For the filter hook
the_content
example that you gave, the filter hook is used in the functionthe_content
found inwp-includespost-template.php
in WordPress 3.7.1:In the
apply_filters
function call, the first parameter is the hook name, in this case'the_content'
. The next parameter,$content
, is the value that we want to filter. This will be the first parameter in your filter function (which is named$content
in yourmy_the_content_filter
function, but it could have any name). Optionally, more parameters can be specified in theapply_filters
function, and these will be passed in additional input parameters to the filter function.For a filter hook, your filter function returns a filtered value, which in turn is returned by the
apply_filters
function. For action hooks, there isn’t any return value used.