Modify Admin Bar Link

Starting on line 474 of /wp-includes/admin-bar.php there is a function that is declared as the following:

function wp_admin_bar_new_content_menu( $wp_admin_bar )

Read More

What it does it generates the + New menu item along with any custom post types that a user has edit_posts capability for. The actual top-menu item, + New defaults to posts since it is the first called in the array (I believe that’s how it works). I’d like to actually change that first + New link to something else. In our setup, we need to give users edit_posts capability to manage Advanced Custom Fields Options pages, but are not giving them access to posts themselves (which we just had to hide from the menu system). A bit funky, but it’s more of a usability concern for us.

Aside from all the jumble I said above, can you modify an existing menu link based on the ID of the item itself (which is new-content in this case) or would I need to destroy and rebuild the menu itself? I’m just looking to change that href attribute to #. Thanks!

Related posts

Leave a Reply

2 comments

  1. 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

  2. To add to David Carroll awesome answer (thanks!), I must say that to get the node name (slug name) of the existing admin bar menu and this way the possibility to alter them, you must look in the code of this new-content menu.

    The ul#wp-admin-bar-new-content-default li have all an ID witch finishes by there name. Example: li#wp-admin-bar-new-post or li#wp-admin-bar-new-media where new-post or new-media is the name you can use in the get_node() function.

    For my purpose I add to change the name of the link to new Post by a custom one.

    //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);