add_node()

向工具栏加入自定义链接

描述

此函数是 $wp_admin_bar 全局对象(请参阅:WP_Admin_Bar)的方法,可用于向工具栏添加新项。您还可以使用它来更改工具栏中已有项的属性。

工具栏取代了自WordPress版本3.3以来的管理栏。

工具栏项也称为“节点”。节点可以是其他节点的父节点,这会创建下拉菜单。将节点与 add_group() 组合在一起,以在工具栏菜单中创建不同的部分。

注意:此函数是 WP_Admin_Bar 类和 $wp_admin_bar 全局对象的方法,除了在 ‘admin_bar_menu’ 或 ‘wp_before_admin_bar_render’ 钩子期间,它可能不存在。

注意:add_menu是此方法的别名。

自 2015 年 6 月 17 日星期三起新增,请参阅用于对管理栏上的菜单项进行排序的代码。

用法

<?php $wp_admin_bar->add_node( $args ); ?>

参数

$args

(array) (必填) 参数数组。

默认值: None

Arguments

id

(string) (必填) 节点的 ID。

默认值: false

title

(string) (可选) 将在工具栏中可见的文本。允许包含 html 标记。

默认值: false

parent

(string) (可选) 父节点的 ID。

默认值: false

href

(string) (可选)链接的“href”属性。如果未设置“href”,则节点将是文本节点。

默认值: false

group

(boolean) (可选) 如果设置为“true”,这将使节点成为组(节点)。组节点在工具栏中不可见,但添加到工具栏的节点可见。请参见add_group()。

默认值: false

meta

(array) (可选) 节点的元数据数组。

默认值: array()

‘html’ – 用于节点的 html。

‘class’ – 包含链接或文本节点的列表项的类属性。

‘rel’ – rel 属性。

‘onclick’ – 链接的 onclick 属性。仅当存在“href”参数时,才会设置此选项。

‘target’ –  链接的目标属性。仅当存在“href”参数时,才会设置此选项。

‘title’ –标题属性。将设置为链接或包含文本节点的 div。

‘tabindex’ –  tabindex 属性。将设置为链接或包含文本节点的 div。

示例

将页面添加到工具栏

此示例将向工具栏添加一个具有“my-toolbar-page”类的页面。它将是一个顶级项目,因为未设置“parent”参数(它没有父节点)。把它放在你的主题的功能.php文件中。

/* ———————————-
* wordpress函数 Xingkongweb.com收集
* ———————————- */

add_action( ‘admin_bar_menu’, ‘toolbar_link_to_mypage’, 999 );

function toolbar_link_to_mypage( $wp_admin_bar ) {

$args = array(

‘id’    => ‘my_page’,

‘title’ => ‘My Page’,

‘href’  => ‘http://mysite.com/my-page/’,

‘meta’  => array( ‘class’ => ‘my-toolbar-page’ )

);

$wp_admin_bar->add_node( $args );

}

使现有子节点成为父节点

此示例将使用现有节点的 ID 更改该节点的属性。有关如何查找现有节点 ID 的信息,请参阅查找工具栏节点 ID。以下代码将使 ID 为“new-post”(New > Post)的子节点成为父节点。

/* ———————————-
* wordpress函数 星空站长网收集
* ———————————- */

add_action( ‘admin_bar_menu’, ‘make_parent_node’, 999 );

function make_parent_node( $wp_admin_bar ) {

$args = array(

‘id’     => ‘new-post’,     // id of the existing child node (New > Post)

‘title’  => ‘Add New Post’, // alter the title of existing node

‘parent’ => false,          // set parent to false to make it a top level (parent) node

);

$wp_admin_bar->add_node( $args );

}

添加父/子菜单项的单一功能

这是使用一个功能添加父菜单项或子菜单项的方法。

/* ———————————-
* wordpress函数 Xingkongweb.com收集
* ———————————- */

add_action(‘wp_before_admin_bar_render’, ‘baw_admin_bar_render’, 100);

/**
* Adds admin bar items for easy access to the theme creator and editor
*/

function baw_admin_bar_render() {

    baw_admin_bar_render(‘BAW’); // Parent item

    baw_admin_bar_render(‘BAW Sub1’, ‘some_link_to_the_settings’, ‘BAW’);

    baw_admin_bar_render(‘BAW Sub2’, ‘some_link_to_the_settings’, ‘BAW’);

}
/**
* Adds menu parent or submenu item.
* @param string $name the label of the menu item
* @param string $href the link to the item (settings page or ext site)
* @param string $parent Parent label (if creating a submenu item)
*
* @return void
* */

function baw_admin_bar_render( $name, $href = ”, $parent = ”, $custom_meta = array() ) {

    global $wp_admin_bar;

    if (!is_super_admin()

            || !function_exists(‘is_admin_bar_showing’)) {

            || !is_admin_bar_showing()

            || !is_object($wp_admin_bar)

        return;

    }

    // Generate ID based on the current filename and the name supplied.

    $id = sanitize_key( basename(__FILE__, ‘.php’ ) . ‘-‘ . $name );

    // Generate the ID of the parent.

    $parent = sanitize_key( basename(__FILE__, ‘.php’ ) . ‘-‘ . $parent );

    // links from the current host will open in the current window

    $meta = strpos( $href, site_url() ) !== false ? array() : array( ‘target’ => ‘_blank’ ); // external links open in new tab/window

    $meta = array_merge( $meta, $custom_meta );

    $wp_admin_bar->add_node( array(

        ‘parent’ => $parent,

        ‘id’ => $id,

        ‘title’ => $name,

        ‘href’ => $href,

        ‘meta’ => $meta,

    ) );

}

对管理菜单栏上的链接进行排序

在 PHP 中使用其他数组函数使用以下代码对管理菜单项进行排序。

由于管理栏链接的基本代码是为每个数组创建一个单独的$args变量,因此当您需要对链接进行排序时,对链接进行排序可能需要大量剪切和粘贴代码的工作。

Solution:
以下代码执行以下操作:
1. 使用 PHP 的 array_push() 函数将每个$args添加到同一$args的单独元素中
2. 每个$args元素都是一个可以排序的多维数组
3. 使用 PHP 的排序函数对它们进行排序。
4. 遍历 for 循环。

/* ———————————-
* wordpress函数 www.xingkongweb.com收集
* ———————————- */

add_action( ‘admin_bar_menu’, ‘social_media_links’, 900 );

function social_media_links($wp_admin_bar)

{

$args = array(

‘id’     => ‘social_media’,

‘title’ => ‘Social Media’,

‘meta’   => array( ‘class’ => ‘first-toolbar-group’ ),

);

$wp_admin_bar->add_node( $args );

$args = array();

array_push($args,array(

‘id’ => ‘twitter’,

‘title’ => ‘Twitter’,

‘href’ => ‘http://www.twitter.com’,

‘parent’ => ‘social_media’,

));

array_push($args,array(

‘id’     => ‘youtube’,

‘title’ => ‘YouTube’,

‘href’ => ‘http://www.YouTube.com’,

‘parent’ => ‘social_media’,

‘meta’   => array( ‘class’ => ‘first-toolbar-group’ ),

));

array_push($args,array(

‘id’ => ‘fb’,

‘title’ => ‘Facebook’,

‘href’ => ‘http://www.facebook.com’,

‘parent’ => ‘social_media’,

));

sort($args);

for($a=0;$a<sizeOf($args);$a++)

{

$wp_admin_bar->add_node($args[$a]);

}

}

源文件

add_node() 函数的代码位于 wp-includes/class-wp-admin-bar.php.

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索