使用WordPress開發(fā)網(wǎng)站項(xiàng)目,很多時(shí)候都需要對(duì)進(jìn)行后臺(tái)定制,今天倡萌主要分享下自定義頂部管理工具條的使用技巧。

注:如無特殊說明,請(qǐng)將下面的代碼添加到主題的 functions.php 或者插件的函數(shù)文件中。
對(duì)所有用戶和訪客隱藏工具條
/*
* 對(duì)所有用戶和訪客隱藏工具條
*/
add_filter('show_admin_bar', '__return_false');
只對(duì)管理員顯示工具條
/*
* 只對(duì)管理員顯示工具條
*/
if ( !current_user_can( 'manage_options' ) ) {
add_filter('show_admin_bar', '__return_false');
}
只在后臺(tái)隱藏工具條
/*
* 只在后臺(tái)隱藏工具條
*/
if ( is_admin() ) {
remove_action( 'init', '_wp_admin_bar_init' );
}
只在前臺(tái)隱藏工具條
/*
* 只在前臺(tái)隱藏工具條
*/
if ( !is_admin() ) {
add_filter('show_admin_bar', '__return_false');
}
多站點(diǎn)管理后臺(tái)隱藏工具條
/*
* 對(duì)多站點(diǎn)后臺(tái)隱藏工具條
*/
if ( is_network_admin() ) {
remove_action( 'init', '_wp_admin_bar_init' );
}
移除工具條占位高度
隱藏工具條以后,頂部可能會(huì)殘留 28 像素的空白,你可以使用下面的代碼刪除空白。
/*
* 移除工具條占位空白
*/
function remove_adminbar_margin() {
$remove_adminbar_margin = '<style type="text/css">
html { margin-top: -28px !important; }
* html body { margin-top: -28px !important; }
</style>';
echo $remove_adminbar_margin;
}
/* 針對(duì)后臺(tái) */
if ( is_admin() ) {
remove_action( 'init', '_wp_admin_bar_init' );
add_action( 'admin_head', 'remove_adminbar_margin' );
}
/* 針對(duì)前臺(tái) */
if ( !is_admin() ) {
add_filter('show_admin_bar', '__return_false');
add_action( 'wp_head', 'remove_adminbar_margin' );
}
移除工具條默認(rèn)菜單
下面的代碼可以移除WordPress頂部工具條的默認(rèn)項(xiàng)目,請(qǐng)根據(jù)自己的需要選擇
function wpdaxue_admin_bar() {
global $wp_admin_bar;
$wp_admin_bar->remove_menu('wp-logo'); //移除Logo
$wp_admin_bar->remove_menu('my-account'); //移除個(gè)人中心
$wp_admin_bar->remove_menu('comments'); //移除評(píng)論
$wp_admin_bar->remove_menu('my-sites'); //移除我的網(wǎng)站(多站點(diǎn))
$wp_admin_bar->remove_menu('site-name'); //移除網(wǎng)站名稱
$wp_admin_bar->remove_menu('new-content'); // 移除“新建”
$wp_admin_bar->remove_menu('search'); //移除搜索
$wp_admin_bar->remove_menu('updates'); //移除升級(jí)通知
}
add_action( 'wp_before_admin_bar_render', 'wpdaxue_admin_bar' );
添加一個(gè)簡(jiǎn)單的菜單
添加一個(gè)簡(jiǎn)單的菜單,并且在設(shè)置為新窗口打開
/*
* 添加一個(gè)簡(jiǎn)單的菜單
* 自行修改 'title' 和 'href' 的值
*/
function custom_adminbar_menu( $meta = TRUE ) {
global $wp_admin_bar;
if ( !is_user_logged_in() ) { return; }
if ( !is_super_admin() || !is_admin_bar_showing() ) { return; }
$wp_admin_bar->add_menu( array(
'id' => 'custom_menu',
'title' => __( 'Menu Name' ),
'href' => 'http://google.com/',
'meta' => array( target => '_blank' ) )
);
}
add_action( 'admin_bar_menu', 'custom_adminbar_menu', 15 );
/* add_action # 后面的數(shù)字表示位置:
10 = 在 Logo 的前面
15 = 在 logo 和 網(wǎng)站名之間
25 = 在網(wǎng)站名后面
100 = 在菜單的最后面
*/
添加一個(gè)菜單(只顯示圖標(biāo))
上面的例子是添加一個(gè)顯示文字的鏈接,如果你只希望顯示一個(gè)圖標(biāo),可以使用下面的代碼
/*
* Add Icons Instead of Text to the Main Admin Bar
*/
function custom_adminbar_menu( $meta = TRUE ) {
global $wp_admin_bar;
if ( !is_user_logged_in() ) { return; }
if ( !is_super_admin() || !is_admin_bar_showing() ) { return; }
$wp_admin_bar->add_menu( array(
'id' => 'custom_menu',
'title' => __( '<img src="http://domain.com/wp-content/themes/theme_name/images/wpdaxue-icon.gif" width="25" height="25" />' ),
'href' => 'http://www.ydqwiac.cn/',
'meta' => array( target => '_blank' ) )
);
}
add_action( 'admin_bar_menu', 'custom_adminbar_menu', 15 );
function custom_menu_css() {
$custom_menu_css = '<style type="text/css">
#wp-admin-bar-custom_menu img { margin:0 0 0 12px; } /** moves icon over */
#wp-admin-bar-custom_menu { width:75px; } /** sets width of custom menu */
</style>';
echo $custom_menu_css;
}
add_action( 'admin_head', 'custom_menu_css' );
注意:第 9 行的 ‘id’ => ‘custom_menu’ 要和 17、18 行的 #wp-admin-bar-custom_menu 的后半段對(duì)應(yīng)。同時(shí)注意修改第 10 行的圖片鏈接。
添加后臺(tái)管理菜單
通過下面的代碼,可以添加任何左邊菜單到頂部工具條,支持單站點(diǎn)和多站點(diǎn)模式。這里以“添加 主題編輯 ”為例,更換為其他菜單,請(qǐng)修改里面的 admin_url( ‘theme-editor.php’ )
/*
* 添加快捷菜單到 主題編輯 (支持單站點(diǎn)和多站點(diǎn))
*/
function add_theme_menu() {
global $wp_admin_bar;
if ( !is_user_logged_in() ) { return; }
if ( !is_super_admin() || !is_admin_bar_showing() ) { return; }
if ( function_exists('is_multisite') && is_multisite() ) {
$wp_admin_bar->add_menu( array(
'id' => 'theme-editor',
'title' => __('Edit Theme'),
'href' => network_admin_url( 'theme-editor.php' ) )
);
}else{
$wp_admin_bar->add_menu( array(
'id' => 'theme-editor',
'title' => __('Edit Theme'),
'href' => admin_url( 'theme-editor.php' ) )
);
}
}
add_action( 'admin_bar_menu', 'add_theme_menu', 100 ); //關(guān)于數(shù)字 100 ,請(qǐng)查看上一條技巧
添加下拉菜單
添加下拉菜單到工具條,設(shè)置為 在新窗口或新標(biāo)簽打開
/*
* 添加下拉菜單
* 修改菜單名、鏈接名和鏈接地址
*/
function custom_adminbar_menu( $meta = TRUE ) {
global $wp_admin_bar;
if ( !is_user_logged_in() ) { return; }
if ( !is_super_admin() || !is_admin_bar_showing() ) { return; }
$wp_admin_bar->add_menu( array(
'id' => 'custom_menu',
'title' => __( 'Menu Name' ) ) /* 設(shè)置菜單名 */
);
$wp_admin_bar->add_menu( array(
'parent' => 'custom_menu',
'id' => 'custom_links',
'title' => __( 'Google'), /* 設(shè)置鏈接名*/
'href' => 'http://google.com/', /* 設(shè)置鏈接地址 */
'meta' => array( target => '_blank' ) )
);
}
add_action( 'admin_bar_menu', 'custom_adminbar_menu', 15 );
添加包含多個(gè)鏈接的子菜單
/*
* 添加包含多個(gè)鏈接的子菜單,在新窗口打開鏈接
* 請(qǐng)修改菜單名和鏈接地址
*/
function custom_adminbar_menu( $meta = TRUE ) {
global $wp_admin_bar;
if ( !is_user_logged_in() ) { return; }
if ( !is_super_admin() || !is_admin_bar_showing() ) { return; }
$wp_admin_bar->add_menu( array(
'id' => 'custom_menu',
'title' => __( 'Menu Name' ) ) /* 設(shè)子菜單名 */
);
/* sub-menu */
$wp_admin_bar->add_menu( array(
'parent' => 'custom_menu',
'id' => 'custom_links',
'title' => __( 'Sub menu') ) /* 設(shè)置子菜單名 */
);
/* menu links */
$wp_admin_bar->add_menu( array(
'parent' => 'custom_links',
'title' => 'Google', /* 設(shè)置鏈接名 */
'href' => 'http://google.com/', /* 設(shè)置鏈接地址 */
'meta' => array( target => '_blank' ) )
);
$wp_admin_bar->add_menu( array(
'parent' => 'custom_links',
'title' => 'Yahoo', /* 設(shè)置鏈接名 */
'href' => 'http://yahoo.com/', /* 設(shè)置鏈接地址 */
'meta' => array( target => '_blank' ) )
);
}
add_action( 'admin_bar_menu', 'custom_adminbar_menu', 15 );
在新窗口打開“訪問站點(diǎn)”
默認(rèn)的情況下,點(diǎn)擊”訪問網(wǎng)站“這個(gè)菜單時(shí),是直接在本窗口打開的,你可以使用下面的代碼讓它默認(rèn)在新窗口/標(biāo)簽 打開。
/*
* 新窗口打開:我的網(wǎng)站 > 網(wǎng)站名 > 訪問網(wǎng)站
*/
function my_site_links() {
global $wp_admin_bar;
foreach ( (array) $wp_admin_bar->user->blogs as $blog ) {
$menu_id = 'blog-' . $blog->userblog_id;
$wp_admin_bar->add_menu( array(
'parent' => $menu_id,
'id' => $menu_id . '-v',
'title' => __( 'Visit Site' ),
'href' => get_home_url( $blog->userblog_id, '/' ),
'meta' => array( target => '_blank' ) )
);
}
}
add_action( 'wp_before_admin_bar_render', 'my_site_links' );
隱藏“我的站點(diǎn)”的子菜單(多站點(diǎn))
/*
* 移除多站點(diǎn)的“我的網(wǎng)站”的子菜單: 儀表盤、新文章、管理評(píng)論 和 訪問文章
*/
function remove_mysites_links () {
global $wp_admin_bar;
foreach ( (array) $wp_admin_bar->user->blogs as $blog ) {
$menu_id_d = 'blog-' . $blog->userblog_id . '-d'; /* Dashboard var */
$menu_id_n = 'blog-' . $blog->userblog_id . '-n'; /* New Post var */
$menu_id_c = 'blog-' . $blog->userblog_id . '-c'; /* Manage Comments var */
$menu_id_v = 'blog-' . $blog->userblog_id . '-v'; /* Visit Site var */
$wp_admin_bar->remove_menu($menu_id_d); /* 移除 儀表盤 */
$wp_admin_bar->remove_menu($menu_id_n); /* 移除 發(fā)布新文章 */
$wp_admin_bar->remove_menu($menu_id_c); /* 移除 管理評(píng)論 */
$wp_admin_bar->remove_menu($menu_id_v); /* 移除 訪問網(wǎng)站 */
}
}
add_action( 'wp_before_admin_bar_render', 'remove_mysites_links' );
添加子菜單到“我的站點(diǎn)”(多站點(diǎn))
/*
* 添加子菜單到“我的網(wǎng)站”: Log Out, Media, Links, Pages, Appearance, Plugins, Users, Tools and Settings
*/
function add_mysites_link () {
global $wp_admin_bar;
foreach ( (array) $wp_admin_bar->user->blogs as $blog ) {
$menu_id = 'blog-' . $blog->userblog_id;
/* Add a Log Out Link */
$wp_admin_bar->add_menu( array(
'parent' => $menu_id,
'id' => $menu_id . '-logout',
'title' => __( 'Log Out' ),
'href' => get_home_url( $blog->userblog_id, '/wp-login.php?action=logout' ) )
);
/* Media Admin */
$wp_admin_bar->add_menu( array(
'parent' => $menu_id,
'id' => $menu_id . '-media',
'title' => __( 'Media Admin' ),
'href' => get_home_url( $blog->userblog_id, '/wp-admin/upload.php' ) )
);
/* Links Admin */
$wp_admin_bar->add_menu( array(
'parent' => $menu_id,
'id' => $menu_id . '-links',
'title' => __( 'Links Admin' ),
'href' => get_home_url( $blog->userblog_id, '/wp-admin/link-manager.php' ) )
);
/* Pages Admin */
$wp_admin_bar->add_menu( array(
'parent' => $menu_id,
'id' => $menu_id . '-pags',
'title' => __( 'Pages Admin' ),
'href' => get_home_url( $blog->userblog_id, '/wp-admin/edit.php?post_type=page' ) )
);
/* Appearance Admin */
$wp_admin_bar->add_menu( array(
'parent' => $menu_id,
'id' => $menu_id . '-appearance',
'title' => __( 'Appearance' ),
'href' => get_home_url( $blog->userblog_id, '/wp-admin/themes.php' ) )
);
/* Plugin Admin */
$wp_admin_bar->add_menu( array(
'parent' => $menu_id,
'id' => $menu_id . '-plugins',
'title' => __( 'Plugin Admin' ),
'href' => get_home_url( $blog->userblog_id, '/wp-admin/plugins.php' ) )
);
/* Users Admin */
$wp_admin_bar->add_menu( array(
'parent' => $menu_id,
'id' => $menu_id . '-users',
'title' => __( 'Users Admin' ),
'href' => get_home_url( $blog->userblog_id, '/wp-admin/users.php' ) )
);
/* Tools Admin */
$wp_admin_bar->add_menu( array(
'parent' => $menu_id,
'id' => $menu_id . '-tools',
'title' => __( 'Tools Admin' ),
'href' => get_home_url( $blog->userblog_id, '/wp-admin/tools.php' ) )
);
/* Settings Admin */
$wp_admin_bar->add_menu( array(
'parent' => $menu_id,
'id' => $menu_id . '-settings',
'title' => __( 'Settings Admin' ),
'href' => get_home_url( $blog->userblog_id, '/wp-admin/options-general.php' ) )
);
}
}
add_action( 'wp_before_admin_bar_render', 'add_mysites_link' );
使用 Domain.com 作為顯示名稱(多站點(diǎn))
默認(rèn)情況下,多站點(diǎn)的網(wǎng)站名稱都是顯示“站點(diǎn)名”,如果你要顯示為 Domain.com 這樣的,可以使用下面的代碼:
/*
* 以域名 to Domain.com 作為菜單名
*/
function change_site_names() {
global $wp_admin_bar;
$blue_wp_logo_url = includes_url('images/wpmini-blue.png');
$blavatar = '<img src="' . esc_url($blue_wp_logo_url) . '" alt="' . esc_attr__( 'Blavatar' ) . '" width="16" height="16" class="blavatar"/>';
foreach ( (array) $wp_admin_bar->user->blogs as $blog ) {
$menu_id = 'blog-' . $blog->userblog_id;
$blogname = ucfirst( $blog->domain );
$wp_admin_bar->add_menu( array(
'parent' => 'my-sites-list',
'id' => $menu_id,
'title' => $blavatar . $blogname,
'href' => get_admin_url( $blog->userblog_id ) )
);
}
}
add_action( 'wp_before_admin_bar_render', 'change_site_names' );
移除網(wǎng)站LOGO(多站點(diǎn))
移除多站點(diǎn)下子站點(diǎn)的logo
/*
* Remove the WP Logo from the My Sites Menu
*/
function remove_wplogo_mysites() {
global $wp_admin_bar;
foreach ( (array) $wp_admin_bar->user->blogs as $blog ) {
$menu_id = 'blog-' . $blog->userblog_id;
$blogname = emptyempty( $blog->blogname ) ? $blog->domain : $blog->blogname;
$wp_admin_bar->add_menu( array(
'parent' => 'my-sites-list',
'id' => $menu_id,
'title' => $blogname,
'href' => get_admin_url( $blog->userblog_id ) )
);
}
}
add_action( 'wp_before_admin_bar_render', 'remove_wplogo_mysites' );
修改“我的站點(diǎn)”的logo(多站點(diǎn))
將logo圖片上傳到 你主題的 images 文件夾,然后根據(jù)實(shí)際修改第 10 行的 NEW-ICON-HERE.png
/*
* Change the WP Logo Icon within the My Sites Menu to any icon you want
* Update the NEW-ICON-HERE.png name to match the proper file name.
*/
function add_mysites_logo() {
global $wp_admin_bar;
foreach ( (array) $wp_admin_bar->user->blogs as $blog ) {
$menu_id = 'blog-' . $blog->userblog_id;
$blogname = emptyempty( $blog->blogname ) ? $blog->domain : $blog->blogname;
$blavatar = '<img src="' . get_bloginfo('template_directory') . '/images/NEW-ICON-HERE.png" alt="' . esc_attr__( 'Blavatar' ) . '" width="16" height="16" class="blavatar"/>';
$wp_admin_bar->add_menu( array(
'parent' => 'my-sites-list',
'id' => $menu_id,
'title' => $blavatar . $blogname,
'href' => get_admin_url( $blog->userblog_id ) )
);
}
}
add_action( 'wp_before_admin_bar_render', 'add_mysites_logo' );
對(duì)訪客顯示工具條
對(duì)沒有登錄的訪客也顯示工具條
/*
* 對(duì)沒有登錄的訪客顯示工具條
*/
add_filter( 'show_admin_bar', '__return_true' );
對(duì)已注銷的用戶創(chuàng)建一個(gè)菜單
/*
* Create a menu for Logged Out Users
*/
function loggedout_menu( $meta = TRUE ) {
global $wp_admin_bar;
if ( is_user_logged_in() ) { return false; }
$wp_admin_bar->add_menu( array(
'id' => 'custom_menu',
'title' => __( 'Menu Name' ),
'href' => 'http://google.com/',
'meta' => array( target => '_blank' ) )
);
}
add_action( 'admin_bar_menu', 'loggedout_menu', 15 );
為已注銷的用戶添加“登錄”鏈接
/*
* Add a Log In Link for Logged Out Users to the Admin Bar
*/
function add_login_link( $meta = FALSE ) {
global $wp_admin_bar, $blog_id;
if ( is_user_logged_in() ) { return false; }
$wp_admin_bar->add_menu( array(
'id' => 'custom_menu',
'title' => __( 'Login' ),
'href' => get_home_url( $blog_id, '/wp-login.php' ) )
);
}
add_filter( 'show_admin_bar', '__return_true' ); /* turn on adminbar for logged out users */
add_action( 'admin_bar_menu', 'add_login_link', 15 );
修改工具條的透明度
/*
* 修改工具條的透明度
*/
function adminbar_opacity() {
$adminbar_opacity = '<style type="text/css">#wpadminbar { filter:alpha(opacity=50); opacity:0.5; }</style>';
echo $adminbar_opacity;
}
/* 后臺(tái)*/
if ( is_admin() ) {
add_action( 'admin_head', 'adminbar_opacity' );
}
/* 前臺(tái) */
if ( !is_admin() ) {
add_action( 'wp_head', 'adminbar_opacity' );
}
鼠標(biāo)懸停時(shí)才顯示工具條
隱藏工具條,當(dāng)鼠標(biāo)懸停在上面時(shí)才顯示
/*
* Hide the WordPress Admin Bar with CSS, then display the Admin Bar on hover with CSS and jQuery
*/
function hide_adminbar() {
$hide_adminbar = '<script type="text/javascript">
$(document).ready( function() {
$("#wpadminbar").fadeTo( "slow", 0 );
$("#wpadminbar").hover(function() {
$("#wpadminbar").fadeTo( "slow", 1 );
}, function() {
$("#wpadminbar").fadeTo( "slow", 0 );
});
});
</script>
<style type="text/css">
html { margin-top: -28px !important; }
* html body { margin-top: -28px !important; }
#wpadminbar {
-ms-filter:"progid:DXImageTransform.Microsoft.Alpha(Opacity=0)";
filter: alpha(opacity=0);
-moz-opacity:0;
-khtml-opacity:0;
opacity:0;
}
#wpadminbar:hover, #wpadminbar:focus {
-ms-filter:"progid:DXImageTransform.Microsoft.Alpha(Opacity=100)";
filter: alpha(opacity=100);
-moz-opacity:1;
-khtml-opacity:1;
opacity:1;
}
</style>';
echo $hide_adminbar;
}
/* wp-admin area */
if ( is_admin() ) {
add_action( 'admin_head', 'hide_adminbar' );
}
/* websites */
if ( !is_admin() ) {
add_action( 'wp_head', 'hide_adminbar' );
}
修改工具條的顏色
下面的例子是將工具條修改為“藍(lán)色”,你可以通過修改顏色的值來改變顏色
/*
* Change the Admin Bar Color Scheme
*/
function change_adminbar_colors() {
$change_adminbar_colors = '<style type="text/css">
#wpadminbar *, #wpadminbar{ color:#ffffff;text-shadow:#444444 0 -1px 0; }
#wpadminbar{
background-color:#003399;
background-image:-ms-linear-gradient(bottom,#000033,#003399 5px);
background-image:-moz-linear-gradient(bottom,#000033,#003399 5px);
background-image:-o-linear-gradient(bottom,#000033,#003399 5px);
background-image:-webkit-gradient(linear,left bottom,left top,from(#000033),to(#003399));
background-image:-webkit-linear-gradient(bottom,#000033,#003399 5px);
background-image:linear-gradient(bottom,#000033,#003399 5px);
}
/* menu separators */
#wpadminbar .quicklinks>ul>li{border-right:1px solid #003399;}
#wpadminbar .quicklinks>ul>li>a,#wpadminbar .quicklinks>ul>li>.ab-emptyempty-item{border-right:1px solid #000033;}
#wpadminbar .quicklinks .ab-top-secondary>li{border-left:1px solid #000033;}
#wpadminbar .quicklinks .ab-top-secondary>li>a,#wpadminbar .quicklinks .ab-top-secondary>li>.ab-emptyempty-item{border-left:1px solid #003399;}
/* menu hover color and hover link color */
#wpadminbar.nojs .ab-top-menu>li.menupop:hover>.ab-item,#wpadminbar .ab-top-menu>li.menupop.hover>.ab-item{background:#333333;color:#ffffff;}
#wpadminbar .hover .ab-label,#wpadminbar.nojq .ab-item:focus .ab-label{color:#ffffff;}
#wpadminbar .menupop.hover .ab-label{color:#ffffff;}
/* menu, on mouse over hover colors */
#wpadminbar .ab-top-menu>li:hover>.ab-item,#wpadminbar .ab-top-menu>li.hover>.ab-item,#wpadminbar .ab-top-menu>li>.ab-item:focus,#wpadminbar.nojq .quicklinks .ab-top-menu>li>.ab-item:focus{
color:#fafafa;
background-color:#000033;
background-image:-ms-linear-gradient(bottom,#003399,#000033);
background-image:-moz-linear-gradient(bottom,#003399,#000033);
background-image:-o-linear-gradient(bottom,#003399,#000033);
background-image:-webkit-gradient(linear,left bottom,left top,from(#003399),to(#003399));
background-image:-webkit-linear-gradient(bottom,#003399,#000033);
background-image:linear-gradient(bottom,#003399,#000033);
}
/* menu item links hover color */
#wpadminbar .menupop li:hover,#wpadminbar .menupop li.hover,#wpadminbar .quicklinks .menupop .ab-item:focus,#wpadminbar .quicklinks .ab-top-menu .menupop .ab-item:focus{background-color:#ccffcc;}
/* menu item non-link colors */
#wpadminbar .ab-submenu .ab-item{color:#333333;}
/* menu item link colors */
#wpadminbar .quicklinks .menupop ul li a,#wpadminbar .quicklinks .menupop ul li a strong,#wpadminbar .quicklinks .menupop.hover ul li a,#wpadminbar.nojs .quicklinks .menupop:hover ul li a{color:#0099cc;}
/* my sites hover color */
#wpadminbar .quicklinks .menupop .ab-sub-secondary>li:hover,#wpadminbar .quicklinks .menupop .ab-sub-secondary>li.hover,#wpadminbar .quicklinks .menupop .ab-sub-secondary>li .ab-item:focus{background-color:#dfdfdf;}
/* update menu colors */
#wpadminbar .quicklinks a span#ab-updates{background:#eeeeee;color:#333333;}
#wpadminbar .quicklinks a:hover span#ab-updates{background:#ffffff;color:#000000;}
/* howdy menu */
#wpadminbar .ab-top-secondary{
background-color:#003399;
background-image:-ms-linear-gradient(bottom,#000033,#003399 5px);
background-image:-moz-linear-gradient(bottom,#000033,#003399 5px);
background-image:-o-linear-gradient(bottom,#000033,#003399 5px);
background-image:-webkit-gradient(linear,left bottom,left top,from(#000033),to(#003399));
background-image:-webkit-linear-gradient(bottom,#000033,#003399 5px);
background-image:linear-gradient(bottom,#000033,#003399 5px);
}
/* Howdy menu, username text color in dropdown */
#wpadminbar #wp-admin-bar-user-info .display-name{color:#333333;}
#wpadminbar #wp-admin-bar-user-info .username{color:#999999;}
/* search */
#wpadminbar #adminbarsearch .adminbar-input{color:#ccc;text-shadow:#444 0 -1px 0;background-color:rgba(255,255,255,0);}
#wpadminbar #adminbarsearch .adminbar-input:focus{color:#555;text-shadow:0 1px 0 #fff;background-color:rgba(255,255,255,0.9);}
#wpadminbar.ie8 #adminbarsearch .adminbar-input{background-color:#003399;}
#wpadminbar.ie8 #adminbarsearch .adminbar-input:focus{background-color:#fff;}
#wpadminbar #adminbarsearch .adminbar-input::-webkit-input-placeholder{color:#ddd;}
#wpadminbar #adminbarsearch .adminbar-input:-moz-placeholder{color:#ddd;}
</style>';
echo $change_adminbar_colors;
}
/* wp-admin area */
if ( is_admin() ) {
add_action( 'admin_head', 'change_adminbar_colors' );
}
/* websites */
if ( !is_admin() ) {
add_action( 'wp_head', 'change_adminbar_colors' );
}
PHP類:只為管理員顯示工具條(移除占位高度)
/*
* PHP Class that enables the Admin Bar for Admins Only and Removes 28px Space
*/
class admins_only_admin_bar {
/*
* Loads when class is called
*/
function __construct() {
/* disables admin bar */
remove_action( 'init', '_wp_admin_bar_init' );
/* calls function to remove 28px space */
add_action( 'admin_head', array( &$this, 'remove_adminbar_margin' ) );
add_action( 'wp_head', array( &$this, 'remove_adminbar_margin' ) );
}
/*
* Removes the 28px margin for the Admin Bar
*/
public function remove_adminbar_margin() {
$remove_adminbar_margin = '<style type="text/css">
html { margin-top: -28px !important; }
* html body { margin-top: -28px !important; }
</style>';
echo $remove_adminbar_margin;
}
}
/* Admins Only - Call Class */
if ( current_user_can( 'manage_options' ) ) {
$display_admin_bar = new admins_only_admin_bar();
}
PHP類:自定義已注銷的用戶的工具條
為已注銷用戶(游客)顯示工具條、添加登錄鏈接、移除WP Logo、添加自定義菜單
下面的例子,將移除WordPress 的logo、添加一個(gè) 登錄鏈接、創(chuàng)建一個(gè)包含2個(gè)網(wǎng)站名為“Our Other Sites”的下拉菜單
/*
* Force Admin Bar for logged out users, add a login link, remove the wp logo, and add a custom link menu
*/
class force_admin_bar {
/*
* Loads when class is called
*/
function __construct() {
/* logged out users only */
if ( is_user_logged_in() ) { return false; }
/* remove wp logo */
add_action( 'wp_before_admin_bar_render', array( &$this, 'remove_wp_logo' ) );
/* remove search icon [uncomment to activate] */
//add_action( 'wp_before_admin_bar_render', array( &$this, 'disable_bar_search' ) );
/* force adminbar to logged out users */
add_filter( 'show_admin_bar', '__return_true' );
/* call function to add login link to admin bar */
add_action( 'admin_bar_menu', array( &$this, 'logged_out_menus' ), 15 );
}
/*
* Menus for logged out users
*/
function logged_out_menus( $meta = FALSE ) {
global $wp_admin_bar, $blog_id;
/* logout menu link */
$wp_admin_bar->add_menu( array(
'id' => 'login_menu',
'title' => __( 'Login' ),
'href' => get_home_url( $blog_id, '/wp-login.php' ) )
);
/* create menus */
$wp_admin_bar->add_menu( array(
'id' => 'custom_menu',
'title' => __( 'Our Other Websites' ) ) /* set the menu name */
);
/* menu link */
$wp_admin_bar->add_menu( array(
'parent' => 'custom_menu',
'id' => 'techNerdia', /* unique id name */
'title' => 'techNerdia', /* Set the link title */
'href' => 'http://technerdia.com/', /* Set the link a href */
'meta' => array( target => '_blank' ) )
);
/* menu link */
$wp_admin_bar->add_menu( array(
'parent' => 'custom_menu',
'id' => 'Google', /* unique id name */
'title' => 'Google', /* Set the link title */
'href' => 'http://google.com/', /* Set the link a href */
'meta' => array( target => '_blank' ) )
);
}
/*
* Remove the WordPress Logo from the WordPress Admin Bar
*/
function remove_wp_logo() {
global $wp_admin_bar;
$wp_admin_bar->remove_menu('wp-logo');
}
/*
* Disable the Search Icon and Input within the Admin Bar [uncomment to activate]
*/
//function disable_bar_search() {
// global $wp_admin_bar;
// $wp_admin_bar->remove_menu('search');
//}
}
/* Call Class */
$force_admin_bar = new force_admin_bar();
將登錄鏈接從左邊移動(dòng)到右邊
只針對(duì)上面的那個(gè)例子
/*
* Move the Login Link from the left side to the right side within the Admin Bar for logged out users.
*/
function move_login_link() {
$move_login_link = '<style type="text/css">
#wpadminbar #wp-admin-bar-login_menu{float:right}
}
</style>';
echo $move_login_link;
}
add_action( 'wp_head', 'move_login_link' );
參考資料:http://technerdia.com/1140_wordpress-admin-bar.html





添加一個(gè)簡(jiǎn)單的菜單,,
這個(gè)只能添加一個(gè),加兩個(gè)就出現(xiàn)錯(cuò)誤是什么問題呀,修改了id,title都不一樣還是出錯(cuò)。。
評(píng)論說不能用了,我現(xiàn)在是16年,用的可以的呀,基本都好用。
766057766@qq.com
感覺是WP的一個(gè)bug,算不算?
似乎失效了,在4.0版本的WORDPRESS中,請(qǐng)更新代碼的
4.1版本也沒有用了,4.1版本應(yīng)該怎么隱藏呢
添加一個(gè)菜單(只顯示圖標(biāo)) 這個(gè)只能管理員看到嗎? if ( !is_user_logged_in() ) { return; }
if ( !is_super_admin() || !is_admin_bar_showing() ) { return; } 是因?yàn)檫@代碼嗎?
更正,remove_action( ‘init’, ‘_wp_admin_bar_init’ );這種寫法是錯(cuò)誤的,官方明確提示不要移除,
正確方法見下:
add_filter( ‘show_admin_bar’, ‘__return_false’ );
更新最新版wordpress后移除前臺(tái)的adminbar代碼不起作用了