最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Hide admin toolbar based on a query string

programmeradmin2浏览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?

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;
});
发布评论

评论列表(0)

  1. 暂无评论