Support » Fixing WordPress » Admin Toolbar correct coding (to be used in the codex)

Viewing 4 replies - 1 through 4 (of 4 total)
  • Hi Paal,

    The question is should I use admin_bar or wp_admin_bar? I have seen online the newest reference is to use admin_bar.

    Did you mean to say admin_bar_menu or wp_admin_bar? If the question is which hook should be used to add or remove Toolbar menus, it’s admin_bar_menu.

    As for where you should submit examples, I would suggest submitting them directly to the new code reference instead of the Codex, here:

    https://developer.wordpress.org/reference/classes/wp_admin_bar/add_node/

    WP_Admin_Bar::remove_node()

    Here is an example I added to my own tutorial:

    add_action( 'admin_bar_menu', 'add_top_link_to_admin_bar',999 );
    
     function add_top_link_to_admin_bar($admin_bar) {
             // add a parent item
             	$args = array(
             		'id'    => 'custom',
             		'title' => 'Custom Made',
             		'href'   => esc_url( admin_url( '#' ) ),
             	);
             	$admin_bar->add_node( $args );
    
             // add a child item to our parent item
             	$args = array(
             		'parent' => 'custom',
             		'id'     => 'media-libray',
             		'title'  => 'Media Library',
             		'href'   => esc_url( admin_url( 'upload.php' ) ),
             		'meta'   => false
             	);
             	$admin_bar->add_node( $args );
    
             // add a child item to our parent item
             	$args = array(
             		'parent' => 'custom',
             		'id'     => 'plugins',
             		'title'  => 'Plugins',
             		'href'   => esc_url( admin_url( 'plugins.php' ) ),
             		'meta'   => false
             	);
             	$admin_bar->add_node( $args );
     }

    Which uses $admin_bar and not $wp_admin_bar. $wp_admin_bar is used in the codex pages today.

    The codex uses:

    add_action( 'admin_bar_menu', 'remove_wp_logo', 999 );
    
    function remove_wp_logo( $wp_admin_bar ) {
    	$wp_admin_bar->remove_node( 'wp-logo' );
    }

    I will add the examples I have to the links you suggested.

    The second question is that the code I use. Adding two add node functions after each other one is cancelled out. I do not know why.

    I would use $wp_admin_bar just because that’s the actual global for WP_Admin_Bar. Realistically, you can define the single-passed parameter as whatever you want and use that, but I’d recommend using $wp_admin_bar for clarity.

    The second question is that the code I use. Adding two add node functions after each other one is cancelled out. I do not know why.

    It’s probably because you’re reusing the $args variable over and over. Why not just pass the array directly? e.g.

    function add_top_link_to_admin_bar($wp_admin_bar) {
    	// add a parent item
    	$wp_admin_bar->add_node( array(
    		'id'    => 'custom',
    		'title' => 'Custom Made',
    		'href'   => esc_url( admin_url( '#' ) ),
    	) );
    
    	// add a child item to our parent item
    	$wp_admin_bar->add_node( array(
    		'parent' => 'custom',
    		'id'     => 'media-libray',
    		'title'  => 'Media Library',
    		'href'   => esc_url( admin_url( 'upload.php' ) ),
    		'meta'   => false
    	) );
    
    	// add a child item to our parent item
    	$wp_admin_bar->add_node( array(
    		'parent' => 'custom',
    		'id'     => 'plugins',
    		'title'  => 'Plugins',
    		'href'   => esc_url( admin_url( 'plugins.php' ) ),
    		'meta'   => false
    	) );
    }

    Thank you Drew!

    Aha! I got it!
    When I repeat two identical IDs which I did when I reused plugins and media library IDs in the site name submenu and a new custom made menu one will of course get cancelled out. As one can not have two identical ID tags….

    I gathered two functions into one:

    add_action( 'admin_bar_menu', 'add_nodes_to_admin_bar',999 );
    
     function add_nodes_to_admin_bar($wp_admin_bar) {
             // add a top level parent node
            $wp_admin_bar->add_node( array(
             		'id'    => 'custom',
             		'title' => 'Custom Made',
             		'href'   =>  'http://easywebdesigntutorials.com/', // Showing how to add an external link
             ));
    
             // add a child node to custom
             $wp_admin_bar->add_node( array(
             		'parent' => 'custom',
             		'id'     => 'media-libray',
             		'title'  => 'Media Library',
             		'href'   => esc_url( admin_url( 'upload.php' ) ),
             		'meta'   => false
             ));
    
             // add a child node to custom
             $wp_admin_bar->add_node( array(
             		'parent' => 'custom',
             		'id'     => 'plugins',
             		'title'  => 'Plugins',
             		'href'   => esc_url( admin_url( 'plugins.php' ) ),
             		'meta'   => false
             	));   
    
             // NB! Add a child node to the site name parent node
              $wp_admin_bar->add_node( array(
              		'parent' => 'site-name',
              		'id'     => 'media-libray2', // Needs a different id tag
              		'title'  => 'Media Library',
              		'href'   => esc_url( admin_url( 'upload.php' ) ),
              		'meta'   => false
              ));	   	
    
             // NB! Add a child node to the site name parent node (submenu).
             $wp_admin_bar->add_node( array(
             		'parent' => 'site-name',
             		'id'     => 'plugins2', // It needs a different id tag as there can not be two identical ID tags.
             		'title'  => 'Plugins',
             		'href'   => esc_url( admin_url( 'plugins.php' ) ),
             		'meta'   => false
             	));
     }
Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Admin Toolbar correct coding (to be used in the codex)’ is closed to new replies.