0

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?

5

You 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');
?>
| improve this answer | |
1

You can try this in your functions.php file :

if (isset($_GET['hidetoolbar'])) {
    add_filter('show_admin_bar', '__return_false');
}
| improve this answer | |
0

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;
});
| improve this answer | |
  • And what is the improvement over the existing answer? – fuxia Jul 2 '18 at 11:39
  • @fuxia • Smaller, more maintainable code. • Will only act on non-admin pages (faster) • will not create an error in the log when the feature is not used (which will be most of the time) • will respect the settings of other filters that also adjust the output – robrecord Aug 10 '18 at 10:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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