Modify Admin Bar Link

时间:2012-04-03 作者:Zach

开始于line 474 属于/wp-includes/admin-bar.php 有一个函数声明如下:

function wp_admin_bar_new_content_menu( $wp_admin_bar )

它的作用是生成+ New 菜单项以及用户拥有的任何自定义帖子类型edit_posts 功能。实际的顶部菜单项,+ New 默认为posts 因为它是数组中的第一个调用(我相信它就是这样工作的)。我想先改变一下+ New 链接到其他内容。在我们的设置中,我们需要为用户edit_posts 能够管理高级自定义字段选项页面,但不允许他们访问帖子本身(我们只是不得不从菜单系统中隐藏)。有点怪,但对我们来说,这更像是一个可用性问题。

除了上面提到的所有混乱之外,您是否可以根据项目本身的ID(即new-content 在这种情况下)或者我需要销毁并重建菜单本身吗?我只是想改变一下href 把某事归因于某人#. 谢谢

2 个回复
最合适的回答,由SO网友:David Carroll 整理而成

我以前从未与管理栏合作过。然而,我发现你的问题很有趣,决定看一看。如果添加一个函数来处理操作挂钩“admin\\u bar\\u menu”,并将优先级设置为高于70,则可以访问原始admin\\u bar\\u菜单节点,从中可以修改尝试访问的属性。下面是一组详细的示例,介绍如何从主题函数操作管理菜单栏。php文件。

add_action( \'admin_bar_menu\', \'customize_my_wp_admin_bar\', 80 );
function customize_my_wp_admin_bar( $wp_admin_bar ) {

    //Get a reference to the new-content node to modify.
    $new_content_node = $wp_admin_bar->get_node(\'new-content\');

    // Parent Properties for new-content node:
        //$new_content_node->id     // \'new-content\'
        //$new_content_node->title  // \'<span class="ab-icon"></span><span class="ab-label">New</span>\'
        //$new_content_node->parent // false
        //$new_content_node->href   // \'http://www.somedomain.com/wp-admin/post-new.php\'
        //$new_content_node->group  // false
        //$new_content_node->meta[\'title\']   // \'Add New\'

    //Change href
    $new_content_node->href = \'#\';

    //Update Node.
    $wp_admin_bar->add_node($new_content_node);

    //Remove an existing menu item.
    $wp_admin_bar->remove_menu(\'new-post\');

    // Properties for new-post node:
        //$new_content_node->id     // \'new-post\'
        //$new_content_node->title  // \'Post\'
        //$new_content_node->parent // \'new-content\'
        //$new_content_node->href   // \'http://www.somedomain.com/wp-admin/post-new.php\'
        //$new_content_node->group  // false
        //$new_content_node->meta   // array()


    // Adding a new custom menu item that did not previously exist.
    $wp_admin_bar->add_menu( array(
               \'id\'    => \'new-custom-menu\',
               \'title\' => \'Custom Menu\',
               \'parent\'=> \'new-content\',
               \'href\'  => \'#custom-menu-link\',)
            );

}
如果将其添加到函数中。php文件,请注意管理菜单栏的以下更改:

新链接现在是“#”

  • 新帖子的链接不再列出
  • 添加了名为“自定义菜单链接”的新菜单链接,指向“#自定义菜单链接”
  • 致以问候,

    大卫·卡罗

    SO网友:anou

    要给David Carroll添加一个很棒的答案(谢谢!),我必须说,要获得现有管理栏菜单的节点名(slug name),并以这种方式更改它们,您必须查看这个新内容菜单的代码。

    这个ul#wp-admin-bar-new-content-default li 把所有的身份证都写上名字。示例:li#wp-admin-bar-new-postli#wp-admin-bar-new-media 哪里new-postnew-media 是您可以在中使用的名称get_node() 作用

    出于我的目的,我添加以将链接的名称更改为新Post 由一个定制的。

    //Get a reference to the new-post node to modify.
    $new_post_node = $wp_admin_bar->get_node(\'new-post\');
    
    //Change title
    $new_post_node->title = __(\'Interventions\', \'NAME-OF-YOUR-THEME\');
    
    //Update Node.
    $wp_admin_bar->add_node($new_post_node);
    

    结束