Plugin API/Action Reference/wp before admin bar render

来自WordPress中文文档
跳转至: 导航搜索

Description

The wp_before_admin_bar_render action allows developers to modify the $wp_admin_bar object before it is used to render the Toolbar to the screen.

Usage

function my_tweaked_admin_bar() {
	global $wp_admin_bar;
	//Do stuff
}
add_action( 'wp_before_admin_bar_render', 'my_tweaked_admin_bar' ); 

Please note that you must declare the $wp_admin_bar global object, as this hook is primarily intended to give you direct access to this object before it is rendered to the screen.

Examples

The following examples show some use cases for this action hook.

Remove a Menu Item

function my_tweaked_admin_bar() {
	global $wp_admin_bar;

	//Remove the WordPress logo...
	$wp_admin_bar->remove_menu('wp-logo');
}
add_action( 'wp_before_admin_bar_render', 'my_tweaked_admin_bar' ); 

Add a Top-Level Menu Item

function my_tweaked_admin_bar() {
	global $wp_admin_bar;

	//Add a link called 'My Link'...
	$wp_admin_bar->add_node(array(
		'id'    => 'my-link',
		'title' => 'My Link',
		'href'  => admin_url()
	));

}
add_action( 'wp_before_admin_bar_render', 'my_tweaked_admin_bar' ); 

Tip: For more information on the add_menu() args, see documentation for WP_Admin_Bar::add_node()

Add a Submenu Item

function my_tweaked_admin_bar() {
	global $wp_admin_bar;

	//Add a link called 'My Link'...
	$wp_admin_bar->add_menu( array(
		'id'    => 'my-link',
		'title' => 'My Link',
		'href'  => admin_url()
	));

	//THEN add a sub-link called 'Sublink 1'...
	$wp_admin_bar->add_menu( array(
		'id'    => 'my-link-sub-1',
		'title' => 'Sublink 1',
		'href'  => admin_url(),
		'parent'=>'my-link'
	));
}
add_action( 'wp_before_admin_bar_render', 'my_tweaked_admin_bar' ); 

Resources

Related

模板:Toolbar Tags