如何自定义WordPress 3.3工具栏

You can make the WordPress interface easier for clients by removing unnecessary menus, widgets and meta boxes. However, in WordPress 3.3, the admin and header bars have been merged to create a single toolbar. It may also contain options you want to hide…

您可以通过删除不必要的菜单小部件和meta框简化客户端的WordPress界面。 但是,在WordPress 3.3中,管理栏和标题栏已合并为一个工具栏。 它还可能包含您要隐藏的选项...

WordPress工具栏API (The WordPress Toolbar API)

The new toolbar is defined using a single WP_Admin_Bar object (see wp-includes/class-wp-admin-bar.php). This provides a number of useful methods:

新工具栏是使用单个WP_Admin_Bar对象定义的(请参阅wp-includes / class-wp-admin-bar.php)。 这提供了许多有用的方法:

  • add_node() — add a new toolbar item

    add_node() —添加一个新的工具栏项

  • remove_node() — remove a toolbar item

    remove_node() —删除工具栏项

  • get_node() — fetch a node’s properties

    get_node() —获取节点的属性

  • get_nodes() — fetch a list of all nodes

    get_nodes() —获取所有节点的列表

删除工具栏项 (Removing Toolbar Items)

We’re going to place our code into a reusable plugin named wp-content/plugins/change-toolbar.php but you could put it within your theme’s functions.php file.

我们将把代码放入名为wp-content / plugins / change-toolbar.php的可重用插件中,但是您可以将其放在主题的functions.php文件中。

WordPress plugins require a header at the top of the file, e.g.

WordPress插件在文件顶部需要一个标头,例如

<?php
/*
Plugin Name: Change Toolbar
Plugin URI: https://www.sitepoint.com/change-wordpress-33-toolbar
Description: Modifies the WordPress 3.3+ toolbar.
Version: 1.0
Author: Craig Buckler
Author URI: http://optimalworks.net/
License: GPL2
*/

We now require a single function where our changes will be made:

现在,我们需要一个函数来进行更改:

function change_toolbar($wp_toolbar) {
	/* ... code to go here ... */
}

followed by an action hook which runs the function and passes the toolbar object:

然后是一个动作钩子,该钩子运行函数并传递工具栏对象:

add_action('admin_bar_menu', 'change_toolbar', 999);

We can now remove toolbar items within the change_toolbar() function. For example, the following line hides the WordPress logo and help sub-menu by referencing its ID, “wp-logo”:

现在,我们可以在change_toolbar()函数中删除工具栏项。 例如,以下行通过引用其ID“ wp-logo”来隐藏WordPress徽标和帮助子菜单:

$wp_toolbar->remove_node('wp-logo');

To remove other items you need to discover what ID they’re using. You could delve into the PHP code but there’s an easier way:

要删除其他项目,您需要发现他们正在使用的ID。 您可以深入研究PHP代码,但是有一种更简单的方法:

  • Open Firebug or your favorite Firebug-like development console.

    打开Firebug或您喜欢的类似Firebug的开发控制台。
  • Locate the toolbar item you want to remove (in most browsers you can right-click the item and select “Inspect Element”).

    找到要删除的工具栏项目(在大多数浏览器中,您可以右键单击该项目,然后选择“检查元素”)。
  • Navigate up the parent nodes until you find an LI tag. It will have an ID starting “wp-admin-bar-” followed by the internal ID code:

    向上浏览父节点,直到找到LI标签。 它的ID以“ wp-admin-bar-”开头,后跟内部ID代码:
WordPress toolbar ID

In this example, the Comments item is highlighted. Therefore, to remove it from the toolbar, we use:

在此示例中,注释项被突出显示。 因此,要从工具栏中删除它,我们使用:

$wp_toolbar->remove_node('comments');

添加工具栏项 (Adding Toolbar Items)

We can add toolbar items within the same function. The syntax is:

我们可以在同一功能内添加工具栏项。 语法为:

$wp_toolbar->add_node($arg);

Where $arg is an associative array containing:

其中$ arg是包含以下内容的关联数组:

  • id — the item’s ID

    id —商品的ID

  • title — the title text

    title标题文字

  • parent — the parent menu ID (optional)

    parent —父菜单ID(可选)

  • href — the link URL (optional)

    href —链接URL(可选)

  • group — true if the node is a group (optional)

    group —如果节点是一个组,则为true(可选)

  • meta — another array providing other keys including: html, class, onclick, target, title, tabindex

    meta —提供其他键的另一个数组,包括:html,class,onclick,target,title,tabindex

Let’s add a “Help” item which links to our support pages:

让我们添加一个“帮助”项,它链接到我们的支持页面:

$wp_toolbar->add_node(array(
	'id' => 'myhelp',
	'title' => 'Help',
	'href' => 'http://mysite.com/support/',
	'meta' => array('target' => 'help')
));

We could now add an email support link within a sub-menu by referencing the ‘myhelp’ ID in the parent:

现在,我们可以通过在parent菜单中引用“ myhelp” ID来在子菜单中添加电子邮件支持链接:

$wp_toolbar->add_node(array(
	'id' => 'mysupport',
	'title' => 'Email Support',
	'parent' => 'myhelp',
	'href' => 'mailto:support@mysite.com?subject=support%20request'
));

I hope you find it useful — it’s easy to customize the whole WordPress 3.3 toolbar using a few API calls.

我希望您觉得它有用-使用几个API调用即可轻松自定义整个WordPress 3.3工具栏。

翻译自: https://www.sitepoint.com/change-wordpress-33-toolbar/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值