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
6 responses to “Advent Calendar – Predefined callback functions for filters”
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.
Handy indeed! Good job guys!
[…] 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 […]
^^^^
show_admin_bar( false );
@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.
Woot! Thanks for this, very useful.