6

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 )

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!

2
  • I had a similar issue, and so fare all I have done is add unset( $cpts['post'] ); to line 481, as getting rid of the option to add a post that way entrily was not an issue for me.
    – David Gard
    Apr 3, 2012 at 15:13
  • Check out this answer. It might help.
    – Nick
    Apr 3, 2012 at 17:04

2 Answers 2

10

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
  • Awesome! I was destroying the new-content menu completely and then copying over the default functions (taking out the check for posts with the + New menu item) - your solution is A LOT simpler and is incredibly useful moving forward. Thanks again!
    – Zach
    Apr 4, 2012 at 12:35
  • Note if you only need to change the default + New default post type, instead of making the href = #, instead append your desired post type query: $new_content_node->href .= '?post_type=my_custom_post_type'; (make sure to add the period before the equals)
    – Slam
    Apr 30, 2018 at 3:59
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);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.