Question about how do wordpress filters/actions work

I have a question that I’m curious.

In WordPress we can add a filter or action like this code:

Read More
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)?

Related posts

1 comment

  1. For every filter or action hook that you use with the add_filter or add_action functions, there is a corresponding apply_filters or do_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 function the_content found in wp-includespost-template.php in WordPress 3.7.1:

    function the_content( $more_link_text = null, $strip_teaser = false) {
        $content = get_the_content( $more_link_text, $strip_teaser );
        $content = apply_filters( 'the_content', $content );
        $content = str_replace( ']]>', ']]>', $content );
        echo $content;
    }
    

    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 your my_the_content_filter function, but it could have any name). Optionally, more parameters can be specified in the apply_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.

Comments are closed.