Hide admin toolbar based on a query string

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

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 badges
Add a comment  | 

3 Answers 3

Reset to default 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');
?>

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745101920a4611325.html

相关推荐

  • Hide admin toolbar based on a query string

    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

    12小时前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信