Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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?

share|improve this question

1 Answer 1

up vote 4 down vote accepted

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.

share|improve this answer
1  
Thanks for your answer, I think I just understood a major principle of Wordpress. Basically its one big passing around of variables and sometimes the name of them just changes, right? Because $info is just another name for $output? Also what exactly happens in the filter named 'bloginfo_url'? I tried to find it in the core, but its not there. Is it just a name? –  Sven Nov 2 '13 at 13:03
    
@Sven I updated my answer. –  s1lv3r Nov 2 '13 at 13:31
    
Thank you so much, perfect! –  Sven Nov 2 '13 at 13:46

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.