As far as I know the showing and hiding of the admin toolbar on the front-end is a global setting, which applies to any page containing wp_footer()
.
I want to have more specific control over the visibility admin bar—specifically, to be able to hide it based on a URL query string, such as (e.g., ?hidetoolbar
at the end of a URL).
I know that I can hide the toolbar from a specific template file by adding this to the top:
add_filter('show_admin_bar', '__return_false');
What hook would I use to apply the filter conditionally in functions.php?
As far as I know the showing and hiding of the admin toolbar on the front-end is a global setting, which applies to any page containing wp_footer()
.
I want to have more specific control over the visibility admin bar—specifically, to be able to hide it based on a URL query string, such as (e.g., ?hidetoolbar
at the end of a URL).
I know that I can hide the toolbar from a specific template file by adding this to the top:
add_filter('show_admin_bar', '__return_false');
What hook would I use to apply the filter conditionally in functions.php?
Share Improve this question asked Jun 14, 2012 at 21:32 supertruesupertrue 3,01610 gold badges46 silver badges60 bronze badges3 Answers
Reset to default 5You should be able to just add the filter inside of a conditional:
<?php if ($_GET['hidetoolbar'])
{
add_filter('show_admin_bar', '__return_false');
}
?>
or, since conditionally adding action handlers and filters is sometimes frowned upon, you could add your own function as a filter and then put your conditional inside that:
<?php
function my_manage_toolbar()
{
if ($_GET['hidetoolbar'])
{
return false;
}
return true;
}
add_filter('show_admin_bar', 'my_manage_toolbar');
?>
You can try this in your functions.php file :
if (isset($_GET['hidetoolbar'])) {
add_filter('show_admin_bar', '__return_false');
}
Code for PHP7:
This will only act on non-admin pages, and will respect whatever status the admin bar was before checking. It also will not output any warnings in the log.
add_filter( 'show_admin_bar', function( $show ) {
if (
! is_admin() &&
isset( $_GET['hidetoolbar'] )
) {
return false;
}
return $show;
});