How to Add a WordPress Admin Menu Notification Bubble June 27 2012

Adding one of those nifty WordPress admin menu notification number bubbles to your custom menu/submenu is quite easy.

The Basics

Just append the following snippet to your menu/submenu name and you’re good to go:




 $menu_title .= ' ' . $count . '';



Updating an Existing Menu

Adding a bubble to an existing menu/submenu is almost equally easy, and simply adds hooking into an action and identifying the menu item you wish to update. Lets say we want to add a notification bubble to the Users menu indicating the current number of Administrator users:


add_action( 'admin_menu', 'add_user_menu_bubble' );



function add_user_menu_bubble() {

  global $menu;



  $user_count = count_users();  // get whatever count you need

  $user_count = $user_count['avail_roles']['administrator'];



  if ( $user_count ) {

    foreach ( $menu as $key => $value ) {

      if ( $menu[$key][2] == 'users.php' ) {

        $menu[$key][0] .= ' ' . $user_count . '';

        return;

      }

    }

  }

}

Place the above code into your themes functions.php file, or into a custom plugin. The process is virtually the same for modifying submenu items, simply replace $menu above with $submenu and identify the submenu item to update.

Taking it Further

The above code is going to be executed with every admin page load, and perhaps your notification count involves some intensive database or network operation that you don’t want slowing down each and every admin page load (a good example of this is the plugin update check). If that’s the case, and it’s not critical that the count always be up-to-the-second fresh, you could make use of the WordPress Transients API to cache the value for some period of time, say 30 minutes. Just remember to add code to delete the transient after you perform whatever action the bubble is alerting you to.

The post How to Add a WordPress Admin Menu Notification Bubble appeared first on SkyVerge.