钩子文档

wp_before_admin_bar_render

💡 云策文档标注

概述

wp_before_admin_bar_render 是一个 WordPress 动作钩子,在管理工具栏渲染前触发,允许开发者修改 $wp_admin_bar 对象。此钩子主要用于在工具栏显示前直接访问和操作其内容。

关键要点

  • wp_before_admin_bar_render 钩子在管理工具栏渲染前执行,提供修改 $wp_admin_bar 对象的机会。
  • 使用此钩子时,必须声明全局变量 $wp_admin_bar 以访问工具栏对象。
  • 常见用途包括添加、移除或修改工具栏菜单项,通过 $wp_admin_bar->add_menu()、$wp_admin_bar->remove_menu() 等方法实现。
  • 此钩子自 WordPress 3.1.0 版本引入,与 wp_admin_bar_render() 函数相关。

代码示例

function my_tweaked_admin_bar() {
    global $wp_admin_bar;
    // 添加一个名为 'My Link' 的菜单项
    $wp_admin_bar->add_menu( array(
        'id'    => 'my-link',
        'title' => 'My Link',
        'href'  => admin_url()
    ));
}
add_action( 'wp_before_admin_bar_render', 'my_tweaked_admin_bar' );

注意事项

  • 确保在函数中声明 global $wp_admin_bar; 以正确访问工具栏对象。
  • 使用 add_menu() 或 add_node() 方法添加菜单项时,需提供唯一的 id 和其他参数如 title、href。
  • 移除菜单项可使用 remove_menu() 方法,指定要移除的菜单 id。

📄 原文内容

Fires before the admin bar is rendered.

More Information

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.

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.

Source

do_action( 'wp_before_admin_bar_render' );

Changelog

Version Description
3.1.0 Introduced.

User Contributed Notes

  1. Skip to note 5 content

    (From Codex)
    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' );

  2. Skip to note 6 content

    if ( !function_exists('add_custom_admin_bar_menu') ) {
    
    	add_action( 'wp_before_admin_bar_render', 'add_custom_admin_bar_menu');
    
    	function add_custom_admin_bar_menu()
    
    	{
    		global $wp_admin_bar;
    
    		$args = [
    			'id' => 'custom_admin_bar_menu_id', // id must be unique
    			'title' => 'Custom Menu', // title for display in admin bar
    			'href' => 'http://add-your-link-here.com', // link for the achor tag
    
    			// meta for link e.g: class, target, and custom data attributes etc
    			'meta' => [ 
    				'class' => 'custom_class', // your custom class
    			],
    		];
    		$wp_admin_bar->add_menu($args);
    
    		$args_submenu_1 = [
    			'id' => 'cusotm-sub-menu-1',
    			'title' => 'Sub menu-1',
    			'parent' => 'custom_admin_bar_menu_id', // add parent id in which you want to add sub menu
    			'href' => 'http://add-your-link-here.com',
    			'meta' => [
    				'class' => 'custom_sub_menu_class',
    			],
    		];
    		$wp_admin_bar->add_menu($args_submenu_1);
    
    		$args_submenu_2 = [
    			'id' => 'cusotm-sub-menu-2',
    			'title' => 'Sub menu-2',
    			'parent' => 'custom_admin_bar_menu_id', // add parent id in which you want to add sub menu
    			'href' => 'http://add-your-link-here.com',
    			'meta' => [
    				'class' => 'custom_sub_menu_class',
    			],
    		];
    		$wp_admin_bar->add_menu($args_submenu_2);
    		
    	}
    
    }