Modify Admin Bar Link

I’ve not worked with the admin-bar before. However, I found your question interesting and decided to take a look. If you add a function to handle the action hook ‘admin_bar_menu’ and set the priority to be higher than 70, you will have access to the raw admin_bar_menu nodes where you can modify the properties you are trying to access. Below is a detailed set of examples on how you can manipulate the admin-menu-bar from your theme functions.php file.

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',)
            );

}

If you add this to your functions.php file, note the following changes to the admin menu bar:

  1. +New link is now ‘#’
  2. Link for New Post is no longer listed.
  3. New Menu Link added called Custom Menu Link pointing to ‘#custom-menu-link’

Best Regards,

David Carroll

Leave a Comment