How do I know what variables are passed in a filter/action and what their meaning is?

Something I have seen several times now and I don’t understand is the following: In a theme’s functions.php a function is defined and then attached to a hook, like so (simplified example):

function do_stuff($a, $b) {
    // Do stuff with $a and $b
}
add_filter( 'bloginfo_url', 'do_stuff', 10, 2 );

Basically I think I understand whats happening there, but how do I know what $aand $bis?
In a “traditional” PHP way, one would call the function maybe like that:

do_stuff("var a content", $var_b_content);

Then its clear what $aand $b contain, but how can I know that with WordPress?

Real life example, take the following function (credit to Frank Bültge):

if ( ! function_exists( 'fb_css_cache_buster' ) ) {
        function fb_css_cache_buster( $info, $show ) {
                if ( ! isset($pieces[1]) )
                        $pieces[1] = '';
                if ( 'stylesheet_url' == $show ) {

                        // Is there already a querystring? If so, add to the end of that.
                        if ( strpos($pieces[1], '?' ) === FALSE ) {
                                return $info . "?" . filemtime( WP_CONTENT_DIR . $pieces[1] );
                        } else {
                                $morsels = explode( "?", $pieces[1] );
                                return $info . "&" . filemtime( WP_CONTENT_DIR . $morsles[1] );
                        }
                } else {
                        return $info;
                }
        }

        add_filter( 'bloginfo_url', 'fb_css_cache_buster', 9999, 2 );
}

The function can be used for CSS versioning by attaching the date of the last change (using filemtime) as querystring to the CSS call.

You can see, the uses $info and $show as variables that are passed to that function. But how can I know what these variables contain? He even uses these variables in conditional logic ('stylesheet_url' == $show) so somehow there something must be passed automatically?

1 Answer
1

Basically every filter has to be registered in the original function code (since a filter is just a way you can overwrite what is happening in the default function). So before you can attach your custom function to a filter using add_filter() the filter has to be registered before with apply_filters() (there are other functions which do the same – but this is the most important one).

So in the example you have given in the original function there is a call to apply_filters() (line 533):

$output = apply_filters('bloginfo_url', $output, $show);

As you can see, the two parameters in the original function context are $output and $show.

So to sum up, the best way to find out what the parameters of a filter is:

  • Look up the documentation of the parameters in the codex (although not all filters are completely documented there).
  • Look at the sourcecode of the original function to see which parameters are passed to the filter.

Edit to answer your comment

Because $info is just another name for $output?

You are right. The names of the variables can change when they are passed through a filter. This is simply due to how declaring a function in PHP works:

my_function($output);

function my_function($info) {
    // in the context of this function the content of $output is now inside the variable $info.
}

Also what exactly happens in the filter named ‘bloginfo_url’? I tried
to find it in the core, but its not there.

By default nothing happens with bloginfo_url. WordPress Core doesn’t attach any functions to this filter. It just provides this filter for you, so you can change the native WordPress behaviour without needing to modify any of the core files. (This is only the case for this filter, some of the other filters are used by WordPress itself.)

One thing you always got to keep in mind when working with those filters is, that others (plugins or WordPress itself) can attach functions that conflict with things that you want to achieve with your function. You can control, when your function will run in regards to the other functions attached to the same filter with the $priority param.

Leave a Comment