你有想要跟本站一样,不使用插件来让管理栏一直显示吗?然后自定义自己的按钮,来美化主题呢?
首先我们需要认识一下管理工具栏(admin_bar.php),他主要有/wp-includes/admin-bar.php
.
通过查看admin_bar.php和修改主题中的functions.php来达到对admin_bar.php自定义设定。
1. 永远显示admin_bar.php
找到你的主题文件夹下的functions.php文件,直接添加代码:
在修改任何代码前注意备份您的文件,并添加备注
//Always Show the WordPress Admin Bar
1 2 3 4 5 6 7 8 | function OXP_login_adminbar( $wp_admin_bar ) { if ( !is_user_logged_in() ) $wp_admin_bar ->add_menu( array ( 'title' => __( 'Log In' ), 'href' => wp_login_url() ) ); } add_action( 'admin_bar_menu' , 'OXP_login_adminbar' ); add_filter( 'show_admin_bar' , '__return_true' , 1000 ); |
2. 添加自定义链接到admin_bar.php
2.1 添加"Visit Site/访问网站"到admin_bar.php
//Add "Visit Site" Link and link to homepage
1 2 3 4 5 6 7 8 | function OXP_admin_bar_edit() { global $wp_admin_bar ; $wp_admin_bar ->add_menu( array ( 'id' => 'view-site' , //the view-site ID that refers to what we are doing. 'title' => __( 'Visit Site' ), //the anchor text that links to homepage. 'href' => home_url() ) ); //the homepage URL to which the anchor text will connect. } add_action( 'wp_before_admin_bar_render' , 'OXP_admin_bar_edit' ); |
2.2 添加更多网站和添加二级菜单到"Add New"菜单
1 2 3 4 5 6 7 8 | function OXP_admin_bar_edit() { global $wp_admin_bar ; $wp_admin_bar ->add_menu( array ( 'id' => 'my-blogs' , 'title' => __( 'My Sites' ), 'href' => admin_url( 'my-sites.php' ) ) ); } add_action( 'wp_before_admin_bar_render' , 'OXP_admin_bar_edit' ); |
//Add sub-menu item in "Add New" menu
1 2 3 4 5 6 7 8 9 | function OXP_admin_bar_edit() { global $wp_admin_bar ; $wp_admin_bar ->add_menu( array ( 'parent' => 'new-content' , // new-content is the ID for "Add New" menu so we use it as parent ID. 'id' => 'new_media' , // You can add any value here as you are adding something very new here. 'title' => __( 'Media' ), // The anchor text. 'href' => admin_url( 'media-new.php' ) // name of file to which you will link to. } add_action( 'wp_before_admin_bar_render' , 'OXP_admin_bar_edit' ); |
3. 如何来移除admin_bar中的菜单呢?
3.1移除Name Menu和他的二级菜单
同样适用修改主题文件的functions.php文件来到达效果,修改前注意备份和添加批注
1 2 3 4 5 6 7 | function OXP_admin_bar_edit() { global $wp_admin_bar ; $wp_admin_bar ->remove_menu( 'my-account-with-avatar' ); // This (when used individually with other "remove menu" lines removed) will hide the menu item with author name. $wp_admin_bar ->remove_menu( 'edit-profile' ); // This (when used individually with other "remove menu" lines removed) will remove the sub-part "Edit My Profile". $wp_admin_bar ->remove_menu( 'logout' ); // This (when used individually with other "remove menu" lines removed) will remove the sub-part "Logout". } add_action( 'wp_before_admin_bar_render' , 'OXP_admin_bar_edit' ); |
3.2移除"Dashboard"链接
1 2 3 4 5 | function OXP_admin_bar_edit() { global $wp_admin_bar ; $wp_admin_bar ->remove_menu( 'dashboard' ); } add_action( 'wp_before_admin_bar_render' , 'OXP_admin_bar_edit' ); |
3.3移除"Add New"菜单和他的二级菜单
1 2 3 4 5 6 7 8 9 10 11 12 | function OXP_admin_bar_edit() { global $wp_admin_bar ; $wp_admin_bar ->remove_menu( 'new-content' ); // This removes the complete menu "Add New". You will not require the below "remove_menu" if you using this line. $wp_admin_bar ->remove_menu( 'new-post' ); // This (when used individually with other "remove menu" lines removed) will hide the menu item "Post". $wp_admin_bar ->remove_menu( 'new-page' ); // This (when used individually with other "remove menu" lines removed) will hide the menu item "Page". $wp_admin_bar ->remove_menu( 'new-media' ); // This (when used individually with other "remove menu" lines removed) will hide the menu item "Media". $wp_admin_bar ->remove_menu( 'new-link' ); // This (when used individually with other "remove menu" lines removed) will hide the menu item "Link". $wp_admin_bar ->remove_menu( 'new-user' ); // This (when used individually with other "remove menu" lines removed) will hide the menu item "User". $wp_admin_bar ->remove_menu( 'new-theme' ); // This (when used individually with other "remove menu" lines removed) will hide the menu item "Theme". $wp_admin_bar ->remove_menu( 'new-plugin' ); // This (when used individually with other "remove menu" lines removed) will hide the menu item "Plugin". } add_action( 'wp_before_admin_bar_render' , 'OXP_admin_bar_edit' ); |
3.4移除"Comments"菜单
1 2 3 4 5 | function OXP_admin_bar_edit() { global $wp_admin_bar ; $wp_admin_bar ->remove_menu( 'comments' ); } add_action( 'wp_before_admin_bar_render' , 'OXP_admin_bar_edit' ); |
3.5移除"Appearance"菜单和他的二级菜单
1 2 3 4 5 6 7 | function OXP_admin_bar_edit() { global $wp_admin_bar ; $wp_admin_bar ->remove_menu( 'appearance' ); // This removes the complete menu "Appearance". You will not require the below "remove_menu" if you using this line. $wp_admin_bar ->remove_menu(' new -themes'); // This (when used individually with other "remove menu" lines removed) will hide the menu item "Themes". $wp_admin_bar ->remove_menu( 'new-widgets' ); // This (when used individually with other "remove menu" lines removed) will hide the menu item "Widgets". $wp_admin_bar ->remove_menu( 'new-menus' ); // This (when used individually with other "remove menu" lines removed) will hide the menu item "Menus". } |
3.6移除"Updates"菜单
1 2 3 4 | function OXP_admin_bar_edit() { global $wp_admin_bar ; $wp_admin_bar ->remove_menu( 'updates' ); } |
3.7移除"Shortlink"菜单
1 2 3 4 | function OXP_admin_bar_edit() { global $wp_admin_bar ; $wp_admin_bar ->remove_menu( 'get-shortlink' ); } |
3.8移除插件安装的菜单
这里我们首先要查看admin_bar.php有插件安装的菜单,记录菜单的ID来修改functions.php文件达到移除插件菜单的效果,这里以stats plugin为例:
1 2 3 4 5 6 7 8 | function stats_admin_bar_menu( & $wp_admin_bar ) { $blog_id = stats_get_option( 'blog_id' ); $url = add_query_arg( 'page' , 'stats' , admin_url() ); $img_src = add_query_arg( array ( 'noheader' => '' , 'proxy' => '' , 'chart' => 'admin-bar-hours' , 'height' =>20, 'hours' =>48), $url ); $title = __( 'Views over 48 hours. Click for more Site Stats.' , 'stats' ); $menu = array ( 'id' => 'stats' , 'title' => "<img style='width:95px;height:20px' src='$img_src' alt='$title' title='$title' />" , 'href' => $url ); $wp_admin_bar ->add_menu( $menu ); } |
然后以下是我自定义菜单的代码供大家参考,大家有什么不懂的请直接留言:
// always show admin bar
function pjw_login_adminbar( $wp_admin_bar) {
if ( !is_user_logged_in() )
$wp_admin_bar->add_menu( array( 'title' => __( 'Log In' ), 'href' => wp_login_url() ) );
}
add_action( 'admin_bar_menu', 'pjw_login_adminbar' );
add_filter( 'show_admin_bar', '__return_true' , 1000 );
function OXP_admin_bar_edit() {
global $wp_admin_bar;
$wp_admin_bar->remove_menu('site-name');
$wp_admin_bar->remove_menu('wp-logo-external');
$wp_admin_bar->remove_menu('wp-logo');
$wp_admin_bar->add_menu( array(
'id' => 'home', //the view-site ID that refers to what we are doing.
'title' => __( 'HOME' ), //the anchor text that links to homepage.
'href' => home_url() ) ); //the homepage URL to which the anchor text will connect.
$wp_admin_bar->add_menu( array(
'id' => 'labmates',
'title' => __( 'Labmates' ),
'href' => __('http://labmates.cn/labmates/'),
) );
$wp_admin_bar->add_menu( array(
'parent' => 'labmates',
'id' => 'install',
'title' => __('install'),
'href' => __('http://labmates.cn/install/'),
) );
$wp_admin_bar->add_menu( array(
'parent' => 'labmates',
'id' => 'config',
'title' => __('Config'),
'href' => __('http://labmates.cn/config/'),
) );
$wp_admin_bar->add_menu( array(
'parent' => 'labmates',
'id' => 'project',
'title' => __('Project'),
'href' => __('http://labmates.cn/project/'),
) );
$wp_admin_bar->add_menu( array(
'parent' => 'labmates',
'id' => 'primer',
'title' => __('Primer'),
'href' => __('http://labmates.cn/primer1/'),
) );
$wp_admin_bar->add_menu( array(
'id' => 'guide',
'title' => __( 'Guide' ),
'href' => __('http://labmates.cn/primer/'),
'meta' => array('target' => '_blank'),
) );
$wp_admin_bar->add_menu( array(
'id' => 'forum',
'title' => __( 'Forum' ),
'href' => __('http://labmates.cn/BBS/'),
'meta' => array('target' => '_blank'),
) );
$wp_admin_bar->add_menu( array(
'id' => 'me',
'title' => __( 'About Me' ),
'href' => __('http://labmates.cn/about-me/'),
) );
}
add_action( 'wp_before_admin_bar_render', 'OXP_admin_bar_edit' );
希望这个博文对大家有帮助。
Orignal From: WordPress admin_bar 显示和自定义菜单
没有评论:
发表评论