Jetpack Menu Stats

If you run a Multisite, you have a list of all your sites in the ‘My Sites’ menu. Adding your own custom menus to that isn’t all that complicated. Here’s a quick, practical, bit of code:

<?php
/*
Plugin Name: Jetpack Menu Stats
Plugin URI: http://halfelf.org/hacks/jetpack-menu-stats/
Description: Show 'stats' in the per-site menu bar.
Version: 2
Author: Mika 'Ipstenu' Epstein
Author URI: http://ipstenu.org/
*/

function jetpack_stats_my_sites( $wp_admin_bar ) {
        global $wpdb;

        foreach ( (array) $wp_admin_bar->user->blogs as $blog ) {

                $menu_id  = 'blog-' . $blog->userblog_id;
                $args = array(
                   'parent' => $menu_id,
                   'id'     => $menu_id . '-stats',
                   'title'  => __( 'Site Stats', 'jetpack' ),
                   'href'   => get_admin_url( $blog->userblog_id ).'admin.php?page=stats',
                );
                $wp_admin_bar->add_node($args);
        }
}

add_action( 'admin_bar_menu', 'jetpack_stats_my_sites', 90 );

?>

You can tweak this to anything you want, obvious. If you change the numerical value in the add_action() call, you can move it up or down the menu. A value of 10 will put it at the top. You could probably toss in a check if the plugin was activated per-site as well (I didn’t bother for Jetpack, since I know how to tell if Jetpack’s active via is_plugin_active(), but not a sub-plugin).

As with all my code, is licensed GPL2. Use, abuse, tweak and customize. Don’t expect support, though.

Edited to fix i8n and use add_node, thanks to Thomas and kessiemeijer!

StudioPress Theme of the Month

Comments

  1. keesiemeijer says:

    Why not use add_node(): http://codex.wordpress.org/Function_Reference/add_node

    disclaimer: I wrote that codex page ;-)

    • Because I forgot we were supposed to use add_node now… I lifted the code from core (which uses add_menu)!

      *ahem* And now it does ;)

  2. Will __( 'Stats' ) be translated? I don’t remember that string from the core language files. If not, it would be better to use Jetpack text domain or to drop the call to the translation function.

    • No, and it would probably be better to use __( 'Site Stats', 'jetpack' ) instead (and leverage the built in to Jetpack ;) )

Half-Elf? Try Half OFF WordPress ebooks!