5

I would like to limit the +New admin menu to only show the single sub menu Event ("Veranstaltung"). Basically the users are allowed to create other items as well but not from that +New menu.

+New Admin Menu

I already tried it with "Adminimize" plugin as this can remove the other items but it will leave the new media link intact once you click directly "+New".

+New Admin Menu - link still there

I already added some other logic to remove items from the left admin menu like:

function remove_menus() {

    remove_menu_page('edit.php?post_type=mdocs-posts');
 }
 add_action('admin_menu', 'remove_menus');

But I can't get how to modify the +New. Any hints?

Thank you!

3 Answers 3

11

To hide everything (menu and submenu)-

function wpse_260669_remove_new_content(){
    global $wp_admin_bar;
    $wp_admin_bar->remove_menu( 'new-content' );
}
add_action( 'wp_before_admin_bar_render', 'wpse_260669_remove_new_content' );

To hide specific menu/submenu item(s)-

function wpse_260669_remove_new_content_items(){
    global $wp_admin_bar;
    $wp_admin_bar->remove_menu( 'new-post' ); // hides post CPT
    $wp_admin_bar->remove_menu( 'new-product' ); // hides product CPT
    $wp_admin_bar->remove_menu( 'new-page' ); // hides page CPT
    $wp_admin_bar->remove_menu( 'new-media' ); // hides media
}
add_action( 'wp_before_admin_bar_render', 'wpse_260669_remove_new_content_items' );

So, the basic rule is-

function your_boo_bar_function() {
    global $wp_admin_bar;
    $wp_admin_bar->remove_menu( 'your-unique-menu-id' );
}
add_action( 'wp_before_admin_bar_render', 'your_boo_bar_function' );

Add a new menu-

function wpse_260669_add_menu(){
    global $wp_admin_bar;
    $wp_admin_bar->add_node(
        array(
            'id'        => 'google-menu',
            'title'     => 'Google',
            'href'      => 'http://google.com',
            'parent'    => 'new-content', // so, it'll be set as a child of 'new-content'. remove this to use this as a parent menu
            'meta'      => array( 'class' => 'my-custom-class' ),
        )
    );

}
add_action( 'wp_before_admin_bar_render', 'wpse_260669_add_menu' );

Update an existing menu-

If you want to update an existing menu item, just add a new item using the ID of your desired menu.

To update +New ('content-new'), use this code-

function wpse_260669_update_menu(){
    global $wp_admin_bar;
    $wp_admin_bar->add_node(
        array(
            'id'    => 'new-content', // id of an existing menu
            'href'  => 'your_new_url_goes_here', // set new URL
        )
    );

}
add_action( 'wp_before_admin_bar_render', 'wpse_260669_update_menu' );

How to get menu ID-

The easiest way is to inspect element with Firebug and take the ID. See this screenshot-

How to get menu ID

Navigate to your desired menu item and get the string next to wp-admin-bar-

4
  • Hi mukto90, thanks for your explanation. I see that your approach removes items from the +New menu, but it still leaves the link of the "originally" first item once +New is clicked. If you would remove "Post" menu item in your screenshot the +New itself will still link to post-new.php. Commented Mar 21, 2017 at 13:41
  • Yes, that's correct. +New is hyperlinked to the first item post-new.php. So, what's your expectation?
    – mukto90
    Commented Mar 21, 2017 at 13:55
  • I removed the first item (in my case "media-new.php"). But the link still shows up behind +New. I want to achieve that +New links to the new first item (previously second item) Commented Mar 21, 2017 at 16:02
  • Well. I have updated my answer. please check.
    – mukto90
    Commented Mar 21, 2017 at 16:50
2

To follow up on @mukto90's answer, the following adds a menu to the toolbar that lists the node id (what you need to pass to $wp_admin_bar->remove_node()) of all other nodes in the toolbar.

add_action ('wp_before_admin_bar_render', 'add_all_node_ids_to_toolbar'), 99999) ;

function
add_all_node_ids_to_toolbar ()
{
    global $wp_admin_bar ;

    if (!current_user_can ('manage_options')) {
        // allow only "admins" to have our menu
        return ;
        }

    $all_toolbar_nodes = $wp_admin_bar->get_nodes () ;

    if (empty ($all_toolbar_nodes)) {
        // there are no top-level nodes, so bail
        return ;
        }

    // add our top-level menu to the toolbar
    $our_node_id = 'node_ids' ;
    $args = array (
        'id' => $our_node_id,
        'title' => "Node ID's",
        ) ;
    $wp_admin_bar->add_node ($args) ;

    // add all current Toolbar items to their parent node or to our top-level menu
    foreach ($all_toolbar_nodes as $node) {
        $args = array (
            'id' => "{$our_node_id}_{$node->id}", // prefix id with "node_id_" to make it a unique id
            'title' => $node->id,
            ) ;

        if (!(isset ($node->parent) && $node->parent)) {
            // the node has no parent, so add it to our top-level menu
            $args['parent'] = $our_node_id ;
            }
        else {
            // the node has a parent, so add it as a child to appropriate node in our menu
            $args['parent'] = "{$our_node_id}_{$node->parent}" ;
            }

        $wp_admin_bar->add_node ($args) ;
        }

    return ;
}
2

Another approach is to take advantage of the show_in_admin_bar property of post types. When post types are registered (both built-ins and custom post types) they declare whether they "want" to be included in the "New" toolbar menu, ala

$args = array (
    'show_in_admin_bar' => true|false,
    ) ;
register_post_type ('post_type_name', $args) ;

So, for CPTs you register, if you don't want them to appear in the "New" toolbar menu, just set 'show_in_admin_bar' => false,. If show_in_admin_bar is not specified in $args, then it defaults to the value of show_in_menu. See register_post_type() for details.

How about the built-ins? You can remove those that you don't want with the following:

add_action ('wp_before_admin_bar_render', 'remove_from_toolbar_new') ;

function
remove_from_toolbar_new ()
{
    $allow_in_toolbar_new = array (
        'page', // if you don't want any built-ins, just make this an empty array
        ) ;

    $args = array (
        '_builtin' => true,
        'show_in_admin_bar' => true,
        ) ;
    foreach (get_post_types ($args, 'objects') as $post_type) {
        if (in_array ($post_type->name, $allow_in_toolbar_new)) {
            continue ;
            }

        $post_type->set_props (array ('show_in_admin_bar' => false)) ;
        }

    return ;
}

See get_post_types() and WP_Post_Type::set_props() for details.

As far as I know, the only way to remove the "Users" item from the "New" toolbar menu is with the technique that @mukto90 mentions.

Your Answer

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

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