WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Change item order for +Add menu in the admin bar (7 posts)

  1. Kevin
    Member
    Posted 5 months ago #

    I hope the title makes sense. Basically I would like to know how to change the order of the items a user sees when he clicks on the "Add" button in the admin bar. For instance, say you have a couple of different post-types where a user can add a new post directly from the admin bar. How do you get it to display the post-types in the order you would like? The default post-type and media items are always on top and it would be great to be able to have control over this. Thanks guys.

  2. bcworkz
    Member
    Posted 5 months ago #

    You would need to remove the associated nodes from the $wp_admin_bar object, then add them back in in the desired order. You can view the nodes defined by dumping $wp_admin_bar->nodes from 'admin_bar_menu' action.

    See Class Reference/WP Admin Bar for more information.

  3. Kevin
    Member
    Posted 5 months ago #

    Thanks again for your input bc. I'll work on it and post the results if and when it is working.

  4. Kevin
    Member
    Posted 5 months ago #

    So this was pretty easy. If anyone is interested, here is a simple step by step method I used to do this:

    1. Go to your admin page, open up your web inspector and look at the html for the "Add New" menu. It is displayed as a list with the following structure:

    <ul id="wp-admin-bar-new-content-default" class="ab-submenu">
         <li id="wp-admin-bar-MENU NAME>
              <a class="ab-item" href="LINK"> TITLE </a>
         </li>
         <li id="wp-admin-bar-MENU NAME>
              <a class="ab-item" href="LINK"> TITLE </a>
         </li>
    etc...

    2. Create a function that removes each individual link by using $wp_admin_bar->remove_menu(). Whatever the value of MENU NAME is in the above code, that is the value you put as the parameter. Here is what mine looks like:

    // Remove default "Add New" menu
    function tru_remove_default_new_content_menu() {
        global $wp_admin_bar;
            $wp_admin_bar->remove_menu('new-post');
    	$wp_admin_bar->remove_menu('new-posts');  // I needed this CPT because of the way I wanted to organize taxonomies
    	$wp_admin_bar->remove_menu('new-page');
    	$wp_admin_bar->remove_menu('new-media');
    	$wp_admin_bar->remove_menu('new-books');
    	$wp_admin_bar->remove_menu('new-teachings');
    	$wp_admin_bar->remove_menu('new-our-media');
    	$wp_admin_bar->remove_menu('new-user');
    	$wp_admin_bar->remove_menu('new-tru_main-info');
    	$wp_admin_bar->remove_menu('new-tru_home');
    
    }

    3. Hook your function like so:

    add_action( 'wp_before_admin_bar_render', 'tru_remove_default_new_content_menu' );

    4. Manually add new links by inserting them as children of the new-content menu. The order you add them is the order in which they will appear:

    // Add custom "Add New" menu
    function tru_new_content_menu() {
    	global $wp_admin_bar;
            $wp_admin_bar->add_menu( array(
                 'parent' => 'new-content',
                 'id' => 'MENU NAME',
                 'title' => 'TITLE',
                 'href' => admin_url( 'LINK' ),
               )
            );
    	global $wp_admin_bar;
            $wp_admin_bar->add_menu( array(
                 'parent' => 'new-content',
                 'id' => 'MENU NAME2',
                 'title' => 'TITLE2',
                 'href' => admin_url( 'LINK2' ),
              )
            );
    }
    
    etc...

    5. Hook your function the same way you did in step 3.

    This was my final code:

    // Remove default "Add New" menu
    function tru_remove_default_new_content_menu() {
        global $wp_admin_bar;
        $wp_admin_bar->remove_menu('new-post');
    	$wp_admin_bar->remove_menu('new-posts');  // I needed this CPT because of the way I wanted to organize taxonomies
    	$wp_admin_bar->remove_menu('new-page');
    	$wp_admin_bar->remove_menu('new-media');
    	$wp_admin_bar->remove_menu('new-books');
    	$wp_admin_bar->remove_menu('new-teachings');
    	$wp_admin_bar->remove_menu('new-our-media');
    	$wp_admin_bar->remove_menu('new-user');
    	$wp_admin_bar->remove_menu('new-tru_main-info');
    	$wp_admin_bar->remove_menu('new-tru_home');
    
    }
    add_action( 'wp_before_admin_bar_render', 'tru_remove_default_new_content_menu' );
    
    // Add custom "Add New" menu
    function tru_new_content_menu() {
    	global $wp_admin_bar;
        $wp_admin_bar->add_menu( array(
    								'parent' => 'new-content',
    								'id' => 'new_teaching',
    								'title' => 'Teaching',
    								'href' => admin_url( 'post-new.php?post_type=teachings' ),
    							)
    	);
    	$wp_admin_bar->add_menu( array(
    								'parent' => 'new-content',
    								'id' => 'new_post',
    								'title' => 'Post',
    								'href' => admin_url( 'post-new.php?post_type=posts' ),
    							)
    	);
    	$wp_admin_bar->add_menu( array(
    								'parent' => 'new-content',
    								'id' => 'new_media_post',
    								'title' => 'Media Post',
    								'href' => admin_url( 'post-new.php?post_type=our-media' ),
    							)
    	);
    	$wp_admin_bar->add_menu( array(
    								'parent' => 'new-content',
    								'id' => 'new_book',
    								'title' => 'Book',
    								'href' => admin_url( 'post-new.php?post_type=books' ),
    							)
    	);
    
    }
    add_action( 'wp_before_admin_bar_render', 'tru_new_content_menu' );

    Thanks again bc...

  5. bitpath
    Member
    Posted 3 months ago #

    This does not work for me.
    I have the basic syntax correct, as I am able to modify, remove or add to any other WP admin bar menu. The only one that won't listen using this method is "new-content". That menu won't go away. I can add to it, and remove other ones though, so it is like something is over-riding "new-content" when I try to remove it this way although all the other ones listen.
    Is there a special trick with "new-content" or is it being generated after this, over-riding any "remove_menu" command somehow? remove_menu is also not working on any "new-content" menu items.
    I've searched and found similar instructions many places for adding and removing toolbar menus, or the whole toolbar, and can remove all of them but the "new-content" one or any of it's children (unless I just turn the menu off).
    If there is a conflicting plugin that is over-riding this, can I over-ride it back with my own? I can even add to "new-content" parent menu just fine, just not remove anything from it.
    Thank you

    UPDATE -
    Big embarrassed face. As I thought of order and all that, I realized the code I had been using "add_action( 'bp_setup_admin_bar', 'your_bp_admin_bar_add', 300 );" to call the function instead of "add_action( 'wp_before_admin_bar_render', 'your_bp_admin_bar_remove', 301 );". I was getting confused because calling bp_setup_admin_bar was working for everything I tried but removing media. Changing this call to use wp_before_admin_bar render worked. Sorry for the confusion. This worked perfect.
    Thank you!!

  6. bitpath
    Member
    Posted 3 months ago #

    Can I ask an advanced question please? I've managed to remove the default media add new button, because i think it's a security risk in a professional/mixed website people don't agree to share all media on, but this is not translating right to the iPhone. It still shows a "pencil/new" icon on the front end but now it has no function.
    Can anyone please help understand how to control the pencil icon on iPhones?
    I love wordpress and respect and appreciate the countless hours people have poured into it to make it an amazing option. It's hard to implement in HIPAA secure environments though because even premium plugins have the caveat basic wordpress security applies. I've spent enough time to love it, even if i'm not an expert. It's just not designed for granular security of everything, including user uploads and avatars by default. I was shocked to find out even in secure sites there were calls to a gravatar site and google fonts sticking things up and showing up as identity and data leaks. Whatever I've read otherwise, these were with pretty much tracking every user view.
    I will do anything I can to help.

  7. bitpath
    Member
    Posted 3 months ago #

    My bad again. I'll just shut up now. The pencil icon was just the admin edit sign.
    Thank you again for this great info!

Reply

You must log in to post.

About this Topic

Tags

No tags yet.