Title: Use in admin bar?
Last modified: August 21, 2016

---

# Use in admin bar?

 *  Resolved [thisisbbc](https://wordpress.org/support/users/thisisbbc/)
 * (@thisisbbc)
 * [12 years, 4 months ago](https://wordpress.org/support/topic/use-in-admin-bar/)
 * Is it possible to use the menu in the admin bar that always stays on top?
 * Regards,
    Bastien
 * [http://wordpress.org/plugins/woocommerce-menu-bar-cart/](http://wordpress.org/plugins/woocommerce-menu-bar-cart/)

Viewing 10 replies - 1 through 10 (of 10 total)

 *  Plugin Contributor [Jeremiah](https://wordpress.org/support/users/jprummer/)
 * (@jprummer)
 * [12 years, 4 months ago](https://wordpress.org/support/topic/use-in-admin-bar/#post-4471093)
 * That is not a feature, but you can customize it and wrap the output in an if 
   admin statement
 *  Thread Starter [thisisbbc](https://wordpress.org/support/users/thisisbbc/)
 * (@thisisbbc)
 * [12 years, 4 months ago](https://wordpress.org/support/topic/use-in-admin-bar/#post-4471094)
 * Thank you for your answer Jeremiah. Would you mind guiding me in the process?
   Im kind of a novice in plugin coding and I wouldnt know where to start :/
 * All best,
    Bastien
 *  Plugin Contributor [Ewout](https://wordpress.org/support/users/pomegranate/)
 * (@pomegranate)
 * [12 years, 4 months ago](https://wordpress.org/support/topic/use-in-admin-bar/#post-4471095)
 * Not out of the box, but you could try adding the following code to your functions.
   php file:
 *     ```
       add_action('admin_bar_menu', 'add_toolbar_wcmenucart', 100);
       function add_toolbar_wcmenucart($admin_bar){
       	if (!class_exists( 'WcMenuCart' ))
       		return;
   
       	$wcMenuCart = new WcMenuCart();
       	$menucart_html = $wcMenuCart->wcmenucart_menu_item();
       	if (!is_admin() && $menucart_html != '') {
       		$dom = new DOMDocument();
       		$dom->loadHTML($menucart_html);
   
       		foreach ($dom->getElementsByTagName('a') as $a) {
       		    $menutitle = $a->nodeValue;
       			$menuhref = $a->getAttribute('href');
       		}
   
       		$admin_bar->add_menu( array(
       			'id'    => 'wcmenucart',
       			'title' => '<i class="wcmenucart-icon-shopping-cart-0"></i>'.$menutitle,
       			'href'  => $menuhref,
       			'meta'  => array(
       				'title' => __('View your shopping cart', 'wcmenucart'),
       			),
       		));
       	}
       }
       ```
   
 * Works on my test setup. Note that this doesn’t work with AJAX, so whenever you
   add something to the cart it doesn’t appear until you navigate to the next page.
   I’m assuming that you have some sort of plugin to enable the admin bar for everybody
   browsing the site?
 *  Plugin Contributor [Jeremiah](https://wordpress.org/support/users/jprummer/)
 * (@jprummer)
 * [12 years, 4 months ago](https://wordpress.org/support/topic/use-in-admin-bar/#post-4471098)
 * Thanks Ewout!
 *  Thread Starter [thisisbbc](https://wordpress.org/support/users/thisisbbc/)
 * (@thisisbbc)
 * [12 years, 4 months ago](https://wordpress.org/support/topic/use-in-admin-bar/#post-4471302)
 * Hi Ewout!
 * Thank you very much for your answer. Well we use buddypress so the admin bar 
   stays on top all the time, the notifications, messages and friend requests goes
   there. About your solution… It is indeed working… on the front end! But there’s
   a problem with the back-end. The admin-bar is gone and most of the tabs display
   an empty white page (I can’t even go to the theme editor anymore).
 * Also, the menu doesn’t seem to display a drop-down of the items in the cart on
   the front-end.
 * A quick fix would be tremendously appreciated 🙂
 * Thank you again.
 * All best,
    Bastien
 *  Plugin Contributor [Ewout](https://wordpress.org/support/users/pomegranate/)
 * (@pomegranate)
 * [12 years, 4 months ago](https://wordpress.org/support/topic/use-in-admin-bar/#post-4471303)
 * Hi Bastien,
    First of all, the fact that it doesn’t work on the back-end is on
   purpose, because it’s a bit unstable, depending on all sorts of factors. Front-
   end only is more of a fool proof method. I think your error actually demonstrates
   that, because it’s most likely this sort of bug that causes the behavior you’re
   seeing.
 * The following code detects the backend one step earlier, and doesn’t do anything
   at all in the backend. Can you try if this works for you?
 *     ```
       add_action('admin_bar_menu', 'add_toolbar_wcmenucart', 100);
       function add_toolbar_wcmenucart($admin_bar){
       	if (!class_exists( 'WcMenuCart' ) || is_admin() )
       		return;
   
       	$wcMenuCart = new WcMenuCart();
       	$menucart_html = $wcMenuCart->wcmenucart_menu_item();
       	if ( $menucart_html != '') {
       		$dom = new DOMDocument();
       		$dom->loadHTML($menucart_html);
   
       		foreach ($dom->getElementsByTagName('a') as $a) {
       		    $menutitle = $a->nodeValue;
       			$menuhref = $a->getAttribute('href');
       		}
   
       		$admin_bar->add_menu( array(
       			'id'    => 'wcmenucart',
       			'title' => '<i class="wcmenucart-icon-shopping-cart-0"></i>'.$menutitle,
       			'href'  => $menuhref,
       			'meta'  => array(
       				'title' => __('View your shopping cart', 'wcmenucart'),
       			),
       		));
       	}
       }
       ```
   
 * The fly-out is a pro feature, so that won’t work. Even if you have the pro version
   it would take quite a bit of customizing to make it work, because the admin bar
   works very different from regular menus.
 *  Thread Starter [thisisbbc](https://wordpress.org/support/users/thisisbbc/)
 * (@thisisbbc)
 * [12 years, 4 months ago](https://wordpress.org/support/topic/use-in-admin-bar/#post-4471304)
 * Hey Ewout!
 * Thanks for your quick answer that’s really appreciated 🙂
    Working like a charm!
 * I effectively seen this feature was reserved to the pro version between the time
   I posted my answer and I’ve read yours, so no problems with this.
 * However since you mention it wouldn’t work in the admin bar, it makes me a bit
   reluctant to buy the pro version. If you’d be able to add admin bar support you
   can add me to the satisfied customers list 😉
 * Meanwhile I’ll stick with the free version since it’s pretty much what I needed.
 * Regards,
    Bastien
 *  [hyperV](https://wordpress.org/support/users/hyperv/)
 * (@hyperv)
 * [12 years, 2 months ago](https://wordpress.org/support/topic/use-in-admin-bar/#post-4471386)
 * Hi all,
 * nice stuff!!
    I#m looking the opposit wa to schow the my-account menue from the
   adminbar in the etop-menu beside the chat menu.
 * Have someone a solution?
 * Would be great!!!
 * Regards
 * Josef
 *  [hyperV](https://wordpress.org/support/users/hyperv/)
 * (@hyperv)
 * [12 years, 2 months ago](https://wordpress.org/support/topic/use-in-admin-bar/#post-4471387)
 * In addition:
 * I try to integrate somthing like thi:
 *     ```
       add_filter( 'wp_nav_menu_items', 'add_myaccount_item', 10, 2 );
       function add_myaccount_item( $wp_admin_bar ) {
       	$user_id      = get_current_user_id();
       	$current_user = wp_get_current_user();
       	$profile_url  = get_edit_profile_url( $user_id );
   
       	if ( ! $user_id )
       		return;
   
       	$avatar = get_avatar( $user_id, 26 );
       	$howdy  = sprintf( __('Howdy, %1$s'), $current_user->display_name );
       	$class  = empty( $avatar ) ? '' : 'with-avatar';
   
       	$wp_admin_bar->add_menu( array(
       		'id'        => 'my-account',
       		'parent'    => 'top-secondary',
       		'title'     => $howdy . $avatar,
       		'href'      => $profile_url,
       		'meta'      => array(
       			'class'     => $class,
       			'title'     => __('My Account'),
       		),
       	) );
       }
       ```
   
 * How can I show this in the Top-Menu of my mystile wootemplate.
 * Thanks in advanced
 * Josef
 *  Plugin Contributor [Ewout](https://wordpress.org/support/users/pomegranate/)
 * (@pomegranate)
 * [12 years, 2 months ago](https://wordpress.org/support/topic/use-in-admin-bar/#post-4471388)
 * Hi Josef,
    This has nothing to do with the Menu Cart plugin, so I’m afraid I 
   can’t help you.
 * The code below should get you on the way though: it changes the default ‘Howdy’
   link to the my-account page. The main thing that I changed was the action hook(
   admin_bar_menu) and the my_account_url
 *     ```
       add_action('admin_bar_menu', 'admin_bar_my_account', 100);
       function admin_bar_my_account( $admin_bar ){
       	$user_id		= get_current_user_id();
       	$current_user	= wp_get_current_user();
       	$my_account_url	= get_permalink( woocommerce_get_page_id( 'myaccount' ));
   
       	if ( empty($user_id) ) {
       		return;
       	}
   
       	$avatar	= get_avatar( $user_id, 26 );
       	$howdy	= sprintf( __('Howdy, %1$s'), $current_user->display_name );
       	$class	= empty( $avatar ) ? '' : 'with-avatar';
   
       	$admin_bar->add_menu( array(
       		'id'		=> 'my-account',
       		'parent'	=> 'top-secondary',
       		'title'		=> $howdy . $avatar,
       		'href'		=> $my_account_url,
       		'meta'		=> array(
       			'class'	=> $class,
       			'title'	=> __('My Account'),
       		),
       	) );
       }
       ```
   

Viewing 10 replies - 1 through 10 (of 10 total)

The topic ‘Use in admin bar?’ is closed to new replies.

 * ![](https://ps.w.org/woocommerce-menu-bar-cart/assets/icon-256x256.png?rev=2190481)
 * [Menu Cart for WooCommerce](https://wordpress.org/plugins/woocommerce-menu-bar-cart/)
 * [Support Threads](https://wordpress.org/support/plugin/woocommerce-menu-bar-cart/)
 * [Active Topics](https://wordpress.org/support/plugin/woocommerce-menu-bar-cart/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/woocommerce-menu-bar-cart/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/woocommerce-menu-bar-cart/reviews/)

 * 10 replies
 * 4 participants
 * Last reply from: [Ewout](https://wordpress.org/support/users/pomegranate/)
 * Last activity: [12 years, 2 months ago](https://wordpress.org/support/topic/use-in-admin-bar/#post-4471388)
 * Status: resolved