Wordpress 4.3为非管理员隐藏管理栏

Toa*_*sty 1 wordpress backend

更新到Wordpress 4.3后,用户可以看到管理栏.我使用这段代码来正常隐藏它,但这在4.3中不再起作用了.

add_action('after_setup_theme', 'remove_admin_bar');

function remove_admin_bar() {
     if (!current_user_can('administrator') && !is_admin()) {
         show_admin_bar(false);
     }
}
Run Code Online (Sandbox Code Playgroud)

有任何想法吗?

xph*_*han 5

函数current_user_can是指功能或用户角色名称.所以请尝试manage_options:

add_action('after_setup_theme', 'remove_admin_bar');

function remove_admin_bar() {
     // 'manage_options' is a capability assigned only to administrators
     if (!current_user_can('manage_options') && !is_admin()) {
         show_admin_bar(false);
     }
}
Run Code Online (Sandbox Code Playgroud)

您也可以添加过滤器(适用于较新的WP版本),而不是使用after_setup_theme操作:

add_filter( 'show_admin_bar' , 'handle_admin_bar');

function handle_admin_bar($content) {
     // 'manage_options' is a capability assigned only to administrators
     // here, the check for the admin dashboard is not necessary
     if (!current_user_can('manage_options')) {
         return false;
     }
}
Run Code Online (Sandbox Code Playgroud)