完全删除特定用户角色的WP_Admin_Bar

时间:2013-09-22 作者:Joren

如何隐藏/删除具有特定角色的用户登录时显示的管理栏?我想我得做点什么remove_menu(), 但不完全是什么和如何。

Codex

2 个回复
最合适的回答,由SO网友:Christine Cooper 整理而成

将以下内容添加到functions.php 按详细信息归档here.

Disable Admin Bar for everyone:

// Disable Admin Bar for everyone
if (!function_exists(\'disable_admin_bar\')) {

    function disable_admin_bar() {

        // for the admin page
        remove_action(\'admin_footer\', \'wp_admin_bar_render\', 1000);
        // for the front-end
        remove_action(\'wp_footer\', \'wp_admin_bar_render\', 1000);

        // css override for the admin page
        function remove_admin_bar_style_backend() { 
            echo \'<style>body.admin-bar #wpcontent, body.admin-bar #adminmenu { padding-top: 0px !important; }</style>\';
        }     
        add_filter(\'admin_head\',\'remove_admin_bar_style_backend\');

        // css override for the frontend
        function remove_admin_bar_style_frontend() {
            echo \'<style type="text/css" media="screen">
            html { margin-top: 0px !important; }
            * html body { margin-top: 0px !important; }
            </style>\';
        }
        add_filter(\'wp_head\',\'remove_admin_bar_style_frontend\', 99);
    }
}
add_action(\'init\',\'disable_admin_bar\');

Disable Admin Bar for everyone but administrators:

// Disable Admin Bar for everyone but administrators
if (!function_exists(\'disable_admin_bar\')) {

    function disable_admin_bar() {

        if (!current_user_can(\'manage_options\')) {

            // for the admin page
            remove_action(\'admin_footer\', \'wp_admin_bar_render\', 1000);
            // for the front-end
            remove_action(\'wp_footer\', \'wp_admin_bar_render\', 1000);

            // css override for the admin page
            function remove_admin_bar_style_backend() { 
                echo \'<style>body.admin-bar #wpcontent, body.admin-bar #adminmenu { padding-top: 0px !important; }</style>\';
            }     
            add_filter(\'admin_head\',\'remove_admin_bar_style_backend\');

            // css override for the frontend
            function remove_admin_bar_style_frontend() {
                echo \'<style type="text/css" media="screen">
                html { margin-top: 0px !important; }
                * html body { margin-top: 0px !important; }
                </style>\';
            }
            add_filter(\'wp_head\',\'remove_admin_bar_style_frontend\', 99);

        }
    }
}
add_action(\'init\',\'disable_admin_bar\');

Disable WP Admin Bar for specific users via user id:

// Disable Admin Bar for specific user
if (!function_exists(\'disable_admin_bar\')) {

    function disable_admin_bar() {

        // we\'re getting current user ID
        $user = get_current_user_id();

        // and removeing admin bar for user with ID 123
        if ($user == 123) {

            // for the admin page
            remove_action(\'admin_footer\', \'wp_admin_bar_render\', 1000);
            // for the front-end
            remove_action(\'wp_footer\', \'wp_admin_bar_render\', 1000);

            // css override for the admin page
            function remove_admin_bar_style_backend() { 
                echo \'<style>body.admin-bar #wpcontent, body.admin-bar #adminmenu { padding-top: 0px !important; }</style>\';
            }     
            add_filter(\'admin_head\',\'remove_admin_bar_style_backend\');

            // css override for the frontend
            function remove_admin_bar_style_frontend() {
                echo \'<style type="text/css" media="screen">
                html { margin-top: 0px !important; }
                * html body { margin-top: 0px !important; }
                </style>\';
            }
            add_filter(\'wp_head\',\'remove_admin_bar_style_frontend\', 99);

        }
    }
}
add_action(\'init\',\'disable_admin_bar\');

SO网友:Gfra54

下面是一个6行代码的要点,它将删除非贡献者用户的管理栏:

add_action( \'init\', \'fb_remove_admin_bar\', 0 );
function fb_remove_admin_bar() {
    if (!current_user_can(\'edit_posts\')) { // you can change the test here depending on what you want
        add_filter( \'show_admin_bar\', \'__return_false\', PHP_INT_MAX );
    }
}
把它放在你的函数中。php文件或自定义插件中。

结束

相关推荐

我的网站的wp-admin在将站点URL指向一个子目录后重定向到另一个网站的wp-admin

* DETAILS THAT LEAD UP TO MY PROBLEM *我很熟悉WP给WordPress提供自己的codex目录。我正在我的个人网站上进行试验,erons-expressions.com 重定向到不同的子目录。最初,我将WordPress文件加载到它自己的/WordPress子目录中,并成功地将我的站点重定向到它。我正在为一家非营利组织重新设计(wecareabilene.org) 并将使用WP。他们是一个单独的托管帐户,有自己的wp管理员,我需要使用这种方法将他们的站点重定向到全年