WordPress如何让用户默认不显示WPAdminBar工具栏

By | April 5, 2015

WordPress的WPAdminBar是很不错的功能,不过有的时候容易造成主页变形,有的时候我们也不喜欢这个东西显示出来,所以需要对这个栏目进行调整,今天研究了一下,找到了几个方法.

如下图,用户注册成功后,在浏览站点时显示工具栏 这个选项默认处于勾选状态,我们让它默认处于不勾选状态即可。

toolbar

在当前主题目录下的functions.php中加入以下php代码,即可实现上面的需求:

add_action(“user_register”, “set_user_admin_bar_false_by_default”, 10, 1);
function set_user_admin_bar_false_by_default($user_id) {
update_user_meta( $user_id, ‘show_admin_bar_front’, ‘false’ );
update_user_meta( $user_id, ‘show_admin_bar_admin’, ‘false’ );
}

==========================附加资料==========

wordpress为了方便管理员快速的从前台进入后台来管理网站在wordpress顶部强制加入了一个工具条(admin bar),而且默认是对所有用户都显示的,有时候看着挺烦心。那么怎么来去除这个烦人的工具条(admin bar)呢?下面小V上代码。

 

一、完全禁用工具条:

 

1、完全去除wordpress工具条(代码一)

show_admin_bar(false);

2、完全去除wordpress工具条(代码二)

add_filter(‘show_admin_bar’, ‘__return_false’);

2、只对特定用户显示工具条

只对管理员显示

if (!current_user_can(‘manage_options’)) {

add_filter(‘show_admin_bar’, ‘__return_false’);

}

只对管理员和编辑显示

if(!current_user_can(‘edit_posts’)) {

add_filter(‘show_admin_bar’, ‘__return_false’);

}

3、将工具条从顶部移至页脚

functionfb_move_admin_bar() {

echo’

<style type=”text/css”>

body {

margin-top: -28px;

padding-bottom: 28px;

}

body.admin-bar #wphead {

padding-top: 0;

}

body.admin-bar #footer {

padding-bottom: 28px;

}

#wpadminbar {

top: auto !important;

bottom: 0;

}

#wpadminbar .quicklinks .menupop ul {

bottom: 28px;

}

</style>’;

}

// 如果你想让工具条显示在后台顶部,请删除这行代码

add_action( ‘admin_head’, ‘fb_move_admin_bar’);

//如果你想让工具条显示在前台顶部,请删除这行代码

add_action( ‘wp_head’, ‘fb_move_admin_bar’);

 

 

PS:以上代码都是加入到functions.php中即可。

 

 

=================仅不显示:========================

方法一:彻底不显示。打开主题的CSS.div#wpadminbar{display:none;}

方法二:显示在底部。打开主题CSS.div#wpadminbar{top:auto;bottom:0px;}
#wpadminbar .quicklinks .menupop ul, #wpadminbar .shortlink-input {bottom:28px;}

方法三:在后台,打开用户,在用户选项里,把登陆时显示WPAdminBar去掉。

OK,总结完毕

Leave a Reply