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' );

Comments are closed.

6 comments

  1. Rob

    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

    Handy indeed! Good job guys!

  3. Rob

    ^^^^
    show_admin_bar( false );

  4. Latz

    @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.

  5. Simon Fairbairn

    Woot! Thanks for this, very useful.

One pingback

  1. Adventskalender vom Wochenende und heute « F-LOG-GE