• Resolved psolaz

    (@psolaz)


    hey guys,

    i am trying to create a second adminbar which i can display in a nice sidebarwidget (of course not longer as a ‘bar’ but more a square but that should be some quick css styling).

    So after digging through the admin-bar, class-admin-bar php files and many others i thought i finally understood what is going on and how to create a clone of the adminbar which i can put where i want and style how i want. Guess i was wrong.

    This is the function i come up with:

    
    
    function admin_menu_test() {
    
      require_once( ABSPATH . WPINC . '/class-wp-admin-bar.php' );
      $admin_bar_class = apply_filters( 'wp_admin_bar_class', 'WP_Admin_Bar' );
      $wp_admin_bar = new $admin_bar_class;
      wp_admin_bar_my_account_menu($wp_admin_bar);
      $wp_admin_bar->add_menus();
      $wp_admin_bar->initialize();
      $wp_admin_bar->render();
    
    }
    
    

    Using the function i get a second adminbar, however it is empty. It would be much appreciated if someone could give me a hint how to do it correctly!

    Thanks a lot in advance!

Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator bcworkz

    (@bcworkz)

    The add_menus() call doesn’t really add menus, it sets up callbacks to add menus. Thus you need to fire the action to cause the callbacks to run. Do so just before you call render().
    do_action_ref_array( 'admin_bar_menu', array( &$wp_admin_bar ) );

    Note the ‘&’ before $wp_admin_bar. Your instance of WP_Admin_Bar must be passed by reference. No guarantees, untested, but explains your empty admin bar.

    Thread Starter psolaz

    (@psolaz)

    thanks bcworkz!

    this is what i currently use

    
    function admin_menu_test() {
    
     // sleep(1);
      /* Load the admin bar class code ready for instantiation */
        require_once( ABSPATH . WPINC . '/class-wp-admin-bar.php' );
    
        /* Instantiate the admin bar */
    
        /**
         * Filters the admin bar class to instantiate.
         */
        $admin_bar_class = apply_filters( 'wp_admin_bar_class', 'WP_Admin_Bar' );
        if ( class_exists( $admin_bar_class ) )
            $wp_admin_bar = new $admin_bar_class;
        else
            return false;
    
        $wp_admin_bar->initialize();
    
        $wp_admin_bar->add_menus();
    
        if ( ! is_admin_bar_showing() || ! is_object( $wp_admin_bar ) )
      		return;
    
      	/**
      	 * Load all necessary admin bar items.
      	 *
      	 * This is the hook used to add, remove, or manipulate admin bar items.
      	 */
      	do_action_ref_array( 'admin_bar_menu', array( &$wp_admin_bar ) );
    
      	/**
      	 * Fires before the admin bar is rendered.
      	 */
    //  	do_action( 'wp_before_admin_bar_render' );
    
      	$wp_admin_bar->render();
    
    //    do_action ('wp_after_admin_bar_render');
    
      }
      add_action('wp_after_admin_bar_render' , 'admin_menu_test');
    

    in the template i simply call admin_menu_test() .

    Et voila a second admin bar is display as i wanted. However all the glorious extras added by plugins to the original adminbar is missing. shouldn’t it be added as well when firing the action or am i missing a point?

    EDIT: for some reason it does not make any difference if i call $wp_adminbar->initalize() and ->add_menus() or not.

    This is the whole mess of a plugin anyways if anyone stumbles upon this and tries to do the same thing: https://hastebin.com/iwuquqotas.xml

    Thanks a lot for your help ??

    • This reply was modified 8 years ago by psolaz.
    Thread Starter psolaz

    (@psolaz)

    I am nearly there.

    Couldn’t figure out how to create a second adminbar, however i just ‘moved’ the original one. Still some styling issues but this *should* work:

    https://github.com/dreadkopp/wp_admin_bar_widget

    • This reply was modified 8 years ago by psolaz.
Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘‘adminbar’ in widget’ is closed to new replies.