Advent Calendar – Predefined callback functions for filters

Filters are often used for removing content. For example, if you want to hide the admin bar you can do the following:

function hide_admin_bar() {
    return FALSE;
}
add_filter( 'show_admin_bar' , 'hide_admin_bar' );

It’s a bit tedious to define an additional function just to return “FALSE“. Since version 3.0 WordPress provides various functions to shorten the code:

  • __return_true()
  • __return_false()
  • __return_zero()
  • __return_empty_array()

The above code can now be written as

add_filter( 'show_admin_bar', '__return_false' );

Posted

in

by

Comments

6 responses to “Advent Calendar – Predefined callback functions for filters”

  1. Rob Avatar

    In this example isn’t it easier to do this:

    But nice to know that these functions are at hand 🙂 WordPress has so many usefull functions, love it.

  2. Jay Tillery Avatar

    Handy indeed! Good job guys!

  3. […] im neuen Standardtheme von WordPress. Am Sonntag erschien ein ganz kurzer Beitrag über vordefinierte Callback-Funktionen für Filter. Heute geht es um ein Detail beim Umgang mit dem […]

  4. Rob Avatar

    ^^^^
    show_admin_bar( false );

  5. Latz Avatar

    @Rob: The example was just an … example. Unfortunately I selected one where the effect could be easier accomplished with the function you mention but there are situations where you do not have a distinct function replacing the use of a filter.
    Nevertheless thank you for pointing out that there’s an alternative.

  6. Simon Fairbairn Avatar

    Woot! Thanks for this, very useful.