Skip to main content
Added custom method `is_plugin_active_for_network`
Source Link
brasofilo
  • 22.1k
  • 8
  • 69
  • 264

To add a menu item to each site of the network if an specific plugin is active in that site, I'm using a Must Use plugin. It only runs if is_multisite() and if is_admin_bar_showing(). There's no user capability checking.

<?php
/**
 * Plugin Name: Modify My Sites admin menu
 * Plugin URI: http://wordpress.stackexchange.com/q/55724/12615
 * Description: Add an admin menu link to specific plugin, if it is active in the site. Can remove default items as well.
 * Author: Rodolfo Buaiz
 * Author URI: http://wordpress.stackexchange.com/users/12615/brasofilo
 * License: GPLv3
 *  
 * Class based in http://wordpress.stackexchange.com/a/77236/12615
 **/

 
WPSE_55724_My_Sites_Extra_Items::init( 
    'Analytics360', // Menu item title
    'analytics360/analytics360.php', // Plugin to check for
    'index.php?page=analytics360.php', // Menu item URL
    array( 'newpost', 'comments', 'visit', 'dashboard' ) // Remove defaults
);

class WPSE_55724_My_Sites_Extra_Items
{
    private static $ins = null;

    private static $title = null;
    private static $plugin_file = null;
    private static $plugin_page = null;
    private static $remove_defaults = null;

    public static function instance()
    {
        is_null( self::$ins ) && self::$ins = new self;
        return self::$ins;
    }

    public static function init( $title, $plugin_file, $plugin_page, $remove_defaults )
    {
        self::$title = $title;
        self::$plugin_file = $plugin_file;
        self::$plugin_page = $plugin_page;
        self::$remove_defaults = $remove_defaults;
        
        if( is_multisite() )
            add_action( 'plugins_loaded', array( self::instance(), '_setup' ) );
    }

    public function _setup()
    {
        if( is_admin_bar_showing() )
            add_action( 'admin_bar_menu', array( $this, 'add_items' ), 9999 );          
    }

    /**
     * The global var is passed by reference in a do_action_ref_array
     */
    public function add_items( $wp_admin_bar ) 
    {
        foreach ( (array) $wp_admin_bar->user->blogs as $blog ) 
        {
            switch_to_blog( $blog->userblog_id );
            
            // Current menu ID
            $menu_id  = 'blog-' . $blog->userblog_id;
            
            // Add URL to current-site/wp-admin/plugins.php
            $wp_admin_bar->add_menu( array(
                'parent' => $menu_id,
                'id'     => $menu_id . '-p',
                'title'  => __( 'Plugins' ),
                'href'   => admin_url( 'plugins.php' ),
            ) );
            
            // Remove default menu items
            $this->remove_items( 
                &$wp_admin_bar, 
                self::$remove_defaults,
                $menu_id
            );
                    
            // Add custom menu for chosen Plugin it active in current site or network activated 
            if( current_user_can( 'manage_options' ) && ( $this->is_plugin_active( self::$plugin_file ) || $this->is_plugin_active_for_network( self::$plugin_file ) ) )
            {
                $wp_admin_bar->add_menu( array(
                    'parent' => $menu_id,
                    'id'     => $menu_id . '-my-p',
                    'title'  => self::$title,
                    'href'   => admin_url( self::$plugin_page ),
                ) );
            }
                
            restore_current_blog();
        }
    }   

    /**
     * 
     */
    private function remove_items( $wp_admin_bar, $items, $menu_id )
    {
        if( in_array( 'newpost', $items ) )
            $wp_admin_bar->remove_menu( $menu_id . '-n' ); // New Post

        if( in_array( 'comments', $items ) )
            $wp_admin_bar->remove_menu( $menu_id . '-c' ); // Comments
            
        if( in_array( 'visit', $items ) )
            $wp_admin_bar->remove_menu( $menu_id . '-v' ); // Visit Site
            
        if( in_array( 'dashboard', $items ) )
            $wp_admin_bar->remove_menu( $menu_id . '-d' ); // Dashboard
    }


    /**
     * WP function uses is_plugin_active_for_network 
     * which doesn't work in frontend
     */
    private function is_plugin_active( $plugin )
    {
        return in_array( $plugin, (array) get_option( 'active_plugins', array() ) );
    }

    private function is_plugin_active_for_network( $plugin )
    {
        $plugins = get_site_option( 'active_sitewide_plugins');
        if ( isset($plugins[$plugin]) )
            return true;
        return false;
    }
}

To add a menu item to each site of the network if an specific plugin is active in that site, I'm using a Must Use plugin. It only runs if is_multisite() and if is_admin_bar_showing(). There's no user capability checking.

<?php
/**
 * Plugin Name: Modify My Sites admin menu
 * Plugin URI: http://wordpress.stackexchange.com/q/55724/12615
 * Description: Add an admin menu link to specific plugin, if it is active in the site. Can remove default items as well.
 * Author: Rodolfo Buaiz
 * Author URI: http://wordpress.stackexchange.com/users/12615/brasofilo
 * License: GPLv3
 *  
 * Class based in http://wordpress.stackexchange.com/a/77236/12615
 **/

 
WPSE_55724_My_Sites_Extra_Items::init( 
    'Analytics360', // Menu item title
    'analytics360/analytics360.php', // Plugin to check for
    'index.php?page=analytics360.php', // Menu item URL
    array( 'newpost', 'comments', 'visit', 'dashboard' ) // Remove defaults
);

class WPSE_55724_My_Sites_Extra_Items
{
    private static $ins = null;

    private static $title = null;
    private static $plugin_file = null;
    private static $plugin_page = null;
    private static $remove_defaults = null;

    public static function instance()
    {
        is_null( self::$ins ) && self::$ins = new self;
        return self::$ins;
    }

    public static function init( $title, $plugin_file, $plugin_page, $remove_defaults )
    {
        self::$title = $title;
        self::$plugin_file = $plugin_file;
        self::$plugin_page = $plugin_page;
        self::$remove_defaults = $remove_defaults;
        
        if( is_multisite() )
            add_action( 'plugins_loaded', array( self::instance(), '_setup' ) );
    }

    public function _setup()
    {
        if( is_admin_bar_showing() )
            add_action( 'admin_bar_menu', array( $this, 'add_items' ), 9999 );          
    }

    /**
     * The global var is passed by reference in a do_action_ref_array
     */
    public function add_items( $wp_admin_bar ) 
    {
        foreach ( (array) $wp_admin_bar->user->blogs as $blog ) 
        {
            switch_to_blog( $blog->userblog_id );
            
            // Current menu ID
            $menu_id  = 'blog-' . $blog->userblog_id;
            
            // Add URL to current-site/wp-admin/plugins.php
            $wp_admin_bar->add_menu( array(
                'parent' => $menu_id,
                'id'     => $menu_id . '-p',
                'title'  => __( 'Plugins' ),
                'href'   => admin_url( 'plugins.php' ),
            ) );
            
            // Remove default menu items
            $this->remove_items( 
                &$wp_admin_bar, 
                self::$remove_defaults,
                $menu_id
            );
                    
            // Add custom menu for chosen Plugin it active in current site  
            if( current_user_can( 'manage_options' ) && $this->is_plugin_active( self::$plugin_file ))
            {
                $wp_admin_bar->add_menu( array(
                    'parent' => $menu_id,
                    'id'     => $menu_id . '-my-p',
                    'title'  => self::$title,
                    'href'   => admin_url( self::$plugin_page ),
                ) );
            }
                
            restore_current_blog();
        }
    }   

    /**
     * 
     */
    private function remove_items( $wp_admin_bar, $items, $menu_id )
    {
        if( in_array( 'newpost', $items ) )
            $wp_admin_bar->remove_menu( $menu_id . '-n' ); // New Post

        if( in_array( 'comments', $items ) )
            $wp_admin_bar->remove_menu( $menu_id . '-c' ); // Comments
            
        if( in_array( 'visit', $items ) )
            $wp_admin_bar->remove_menu( $menu_id . '-v' ); // Visit Site
            
        if( in_array( 'dashboard', $items ) )
            $wp_admin_bar->remove_menu( $menu_id . '-d' ); // Dashboard
    }


    /**
     * WP function uses is_plugin_active_for_network 
     * which doesn't work in frontend
     */
    private function is_plugin_active( $plugin )
    {
        return in_array( $plugin, (array) get_option( 'active_plugins', array() ) );
    }
}

To add a menu item to each site of the network if an specific plugin is active in that site, I'm using a Must Use plugin. It only runs if is_multisite() and if is_admin_bar_showing().

<?php
/**
 * Plugin Name: Modify My Sites admin menu
 * Plugin URI: http://wordpress.stackexchange.com/q/55724/12615
 * Description: Add an admin menu link to specific plugin, if it is active in the site. Can remove default items as well.
 * Author: Rodolfo Buaiz
 * Author URI: http://wordpress.stackexchange.com/users/12615/brasofilo
 * License: GPLv3
 *  
 * Class based in http://wordpress.stackexchange.com/a/77236/12615
 **/

 
WPSE_55724_My_Sites_Extra_Items::init( 
    'Analytics360', // Menu item title
    'analytics360/analytics360.php', // Plugin to check for
    'index.php?page=analytics360.php', // Menu item URL
    array( 'newpost', 'comments', 'visit', 'dashboard' ) // Remove defaults
);

class WPSE_55724_My_Sites_Extra_Items
{
    private static $ins = null;

    private static $title = null;
    private static $plugin_file = null;
    private static $plugin_page = null;
    private static $remove_defaults = null;

    public static function instance()
    {
        is_null( self::$ins ) && self::$ins = new self;
        return self::$ins;
    }

    public static function init( $title, $plugin_file, $plugin_page, $remove_defaults )
    {
        self::$title = $title;
        self::$plugin_file = $plugin_file;
        self::$plugin_page = $plugin_page;
        self::$remove_defaults = $remove_defaults;
        
        if( is_multisite() )
            add_action( 'plugins_loaded', array( self::instance(), '_setup' ) );
    }

    public function _setup()
    {
        if( is_admin_bar_showing() )
            add_action( 'admin_bar_menu', array( $this, 'add_items' ), 9999 );          
    }

    /**
     * The global var is passed by reference in a do_action_ref_array
     */
    public function add_items( $wp_admin_bar ) 
    {
        foreach ( (array) $wp_admin_bar->user->blogs as $blog ) 
        {
            switch_to_blog( $blog->userblog_id );
            
            // Current menu ID
            $menu_id  = 'blog-' . $blog->userblog_id;
            
            // Add URL to current-site/wp-admin/plugins.php
            $wp_admin_bar->add_menu( array(
                'parent' => $menu_id,
                'id'     => $menu_id . '-p',
                'title'  => __( 'Plugins' ),
                'href'   => admin_url( 'plugins.php' ),
            ) );
            
            // Remove default menu items
            $this->remove_items( 
                &$wp_admin_bar, 
                self::$remove_defaults,
                $menu_id
            );
                    
            // Add custom menu for chosen Plugin it active in current site or network activated 
            if( current_user_can( 'manage_options' ) && ( $this->is_plugin_active( self::$plugin_file ) || $this->is_plugin_active_for_network( self::$plugin_file ) ) )
            {
                $wp_admin_bar->add_menu( array(
                    'parent' => $menu_id,
                    'id'     => $menu_id . '-my-p',
                    'title'  => self::$title,
                    'href'   => admin_url( self::$plugin_page ),
                ) );
            }
                
            restore_current_blog();
        }
    }   

    /**
     * 
     */
    private function remove_items( $wp_admin_bar, $items, $menu_id )
    {
        if( in_array( 'newpost', $items ) )
            $wp_admin_bar->remove_menu( $menu_id . '-n' ); // New Post

        if( in_array( 'comments', $items ) )
            $wp_admin_bar->remove_menu( $menu_id . '-c' ); // Comments
            
        if( in_array( 'visit', $items ) )
            $wp_admin_bar->remove_menu( $menu_id . '-v' ); // Visit Site
            
        if( in_array( 'dashboard', $items ) )
            $wp_admin_bar->remove_menu( $menu_id . '-d' ); // Dashboard
    }


    /**
     * WP function uses is_plugin_active_for_network 
     * which doesn't work in frontend
     */
    private function is_plugin_active( $plugin )
    {
        return in_array( $plugin, (array) get_option( 'active_plugins', array() ) );
    }

    private function is_plugin_active_for_network( $plugin )
    {
        $plugins = get_site_option( 'active_sitewide_plugins');
        if ( isset($plugins[$plugin]) )
            return true;
        return false;
    }
}
Remake per OP specs.
Source Link
brasofilo
  • 22.1k
  • 8
  • 69
  • 264

Haven't foundAnswer remade and now working as per the Question specifications.

To add a waymenu item to each site of adding the new itemnetwork if an specific plugin is active in that site, so am rebuildingI'm using a Must Use plugin. It only runs if is_multisite() and if is_admin_bar_showing(). There's no user capability checking.

In this example, the plugin is instantiated with the following values that check for the plugin Analytics360º and remove all default menu. items:

  • Menu item title: Analytics360
  • Plugin to check for: analytics360/analytics360.php
  • Menu item URL: index.php?page=analytics360.php
  • Remove defaults: array( 'newpost', 'comments', 'visit', 'dashboard' )

enter image description hereplugin in action

The only caveat is not being able to position the new menu item at the end.plugin in action

<?php
/***
 * Plugin Name: TheModify MenuMy ItemSites admin menu
 * Plugin URI: http://wordpress.stackexchange.com/questionsq/55724/12615
 * Description: Add Menuan Itemsadmin menu link to Adminspecific Barplugin, if it is active in the site. Can remove default items as well.
Version * Author: 1.1Rodolfo Buaiz
 * Author URI: http://wordpress.stackexchange.com/users/12615/brasofilo
 */ License: GPLv3
 *  
class Brsfl_55724_MenuItem* Class based in http://wordpress.stackexchange.com/a/77236/12615
{ **/

 
WPSE_55724_My_Sites_Extra_Items::init(  
  public function __construct()
'Analytics360', // Menu item {title
    'analytics360/analytics360.php', // Plugin to check for
    'index.php?page=analytics360.php', // Menu item add_action(URL
 'wp_before_admin_bar_render',   array( $this'newpost', 'toolbar_link_to_mypage''comments', )'visit', 9999'dashboard' ) // Remove defaults
); 

class WPSE_55724_My_Sites_Extra_Items
{
   } private static $ins = null;

    //private http://stackoverflow.com/a/6228630/1287812static $title = null;
    private functionstatic contains($plugin_file $string,= arraynull;
 $search, $caseInsensitive=false )
 private static $plugin_page {= null;
    private static $remove_defaults = $expnull;

 = '/'.implode('|',array_map('preg_quote',$search)).  public static function instance($caseInsensitive?'/i':'/');
    {
    return preg_match   is_null($exp, $stringself::$ins )?true && self:false;:$ins = new self;
    }    return self::$ins;
    }

    public static function toolbar_link_to_mypageinit( $wp_admin_bar$title, )$plugin_file, $plugin_page, $remove_defaults )
    {
        globalself::$title $wp_admin_bar;= $title;
        $the_admin_barself::$plugin_file = $wp_admin_bar->get_nodes();$plugin_file;
        self::$plugin_page = $plugin_page;
        foreach(self::$remove_defaults $the_admin_bar= as$remove_defaults;
 $key=>$value )      
        {if( is_multisite() )
            //add_action( Get'plugins_loaded', Onlyarray( theself::instance(), Blog'_setup' Nodes) );
    }

    public function _setup()
    {
        if( strposis_admin_bar_showing() $key)
            add_action( 'admin_bar_menu', 'blog-'array( )$this, !=='add_items' false), 9999 ); 
           
   { }

    /**
     * The global var is passed by //reference Getin Bloga IDdo_action_ref_array
     */
    public function add_items( $wp_admin_bar )  
  $pos = substr({
 $key, strpos( $key,'-'     foreach ( (array) +$wp_admin_bar->user->blogs 1as $blog ); 
        {
            switch_to_blog( $blog->userblog_id );
             
    // Blog Menu Items
     // Current menu ID
        $search_sub_menus = array( '-d','-n',' $menu_id  = 'blog-c',' . $blog-v'>userblog_id;
 );           
            // Add URL to current-site/wp-admin/plugins.php
            $wp_admin_bar->add_menu( array(
   // Blog Dashboar Address
          'parent' => $menu_id,
    $search_dashboard_url = array( '-d' );
        'id'     => $menu_id . '-p',
                //'title' Remove Each Blog=> Node__( and'Plugins' Subnodes),
                $wp_admin_bar->remove_node'href'   => admin_url( $key'plugins.php' );,
            ) );
   
           
       // Grab the Blog Address
 // Remove default menu items
           if( $this->contains>remove_items( $key, 
 $search_dashboard_url ) )
             &$wp_admin_bar,  
      $site_url = $value->href;        self::$remove_defaults,
                $menu_id
            );
                // Main Blog Submenu 
            // Add custom menu if(for $this->contains(chosen $key,Plugin $search_sub_menusit )active )in current site  
            if( current_user_can( 'manage_options' ) && $this->is_plugin_active( self::$plugin_file {))
            {
                $wp_admin_bar->add_node>add_menu( $value );array(
                } 
    'parent' => $menu_id,
           // Blogs Sub-submenus
       'id'     => $menu_id . '-my-p',
   else 
                 {
'title'  => self::$title,
                 // Add the Old'href' Node
  => admin_url( self::$plugin_page ),
               $wp_admin_bar->add_node( $value) );
 
            }
        // Creation of the New Sub-submenu Item
   
            restore_current_blog();
      $id = 'blog-'.$pos.'-p';}
    }    

    /**
     *  
   $args = array(*/
    private function remove_items( $wp_admin_bar, $items, $menu_id )
    {
        if( in_array( 'id''newpost', =>$items $id) )
            $wp_admin_bar->remove_menu( $menu_id . '-n' ); // New Post

     ,   'parent'if( =>in_array( $key'comments', $items ) )
            $wp_admin_bar->remove_menu( $menu_id . '-c' ); // Comments
  ,   'title' => 'My Custom URL'   
        if( in_array( 'visit', $items ) )
       ,   'href' => $site_url.'edit$wp_admin_bar->remove_menu( $menu_id .php?post_type=example'
 '-v' ); // Visit Site
             
   );
     if( in_array( 'dashboard', $items ) )
            $wp_admin_bar->add_node>remove_menu($args $menu_id . '-d' ); // Dashboard
    }


    /**
     * WP function }uses is_plugin_active_for_network 
     * which doesn't work in frontend
  }   */
    private function is_plugin_active( $plugin })
    }{
   
}

$brsfl_55724_MenuItem = new Brsfl_55724_MenuItem  return in_array( $plugin, (array) get_option( 'active_plugins', array() ) );
    }
}

Haven't found a way of adding the new item, so am rebuilding the menu.

enter image description here

The only caveat is not being able to position the new menu item at the end.

/*
Plugin Name: The Menu Item
Plugin URI: http://wordpress.stackexchange.com/questions/55724/
Description: Add Menu Items to Admin Bar
Version: 1.1
Author: brasofilo
*/

class Brsfl_55724_MenuItem
{

    public function __construct()
    {       
        add_action( 'wp_before_admin_bar_render', array( $this, 'toolbar_link_to_mypage' ), 9999 );
    }

    // http://stackoverflow.com/a/6228630/1287812
    private function contains( $string, array $search, $caseInsensitive=false )
    {
        $exp = '/'.implode('|',array_map('preg_quote',$search)).($caseInsensitive?'/i':'/');
        return preg_match($exp, $string)?true:false;
    }
    
    public function toolbar_link_to_mypage( $wp_admin_bar ) 
    {
        global $wp_admin_bar;
        $the_admin_bar = $wp_admin_bar->get_nodes();
        
        foreach( $the_admin_bar as $key=>$value )
        {
            // Get Only the Blog Nodes
            if( strpos( $key, 'blog-' ) !== false ) 
             {
                // Get Blog ID
                $pos = substr( $key, strpos( $key,'-' ) + 1 );
                
                // Blog Menu Items
                $search_sub_menus = array( '-d','-n','-c','-v' );
                
                // Blog Dashboar Address
                $search_dashboard_url = array( '-d' );
                
                // Remove Each Blog Node and Subnodes
                $wp_admin_bar->remove_node( $key );
                
                 // Grab the Blog Address
                if( $this->contains( $key, $search_dashboard_url ) )
                    $site_url = $value->href;
                    
                // Main Blog Submenu
                if( $this->contains( $key, $search_sub_menus ) ) 
                {
                    $wp_admin_bar->add_node( $value );
                } 
                 // Blogs Sub-submenus
                else 
                 {
                    // Add the Old Node
                    $wp_admin_bar->add_node( $value );
 
                    // Creation of the New Sub-submenu Item
                    $id = 'blog-'.$pos.'-p';
                    $args = array(
                        'id' => $id
                    ,   'parent' => $key
                    ,   'title' => 'My Custom URL'
                    ,   'href' => $site_url.'edit.php?post_type=example'
                    );
                    $wp_admin_bar->add_node($args);
                }
            }
        }
    }   
}

$brsfl_55724_MenuItem = new Brsfl_55724_MenuItem();

Answer remade and now working as per the Question specifications.

To add a menu item to each site of the network if an specific plugin is active in that site, I'm using a Must Use plugin. It only runs if is_multisite() and if is_admin_bar_showing(). There's no user capability checking.

In this example, the plugin is instantiated with the following values that check for the plugin Analytics360º and remove all default menu items:

  • Menu item title: Analytics360
  • Plugin to check for: analytics360/analytics360.php
  • Menu item URL: index.php?page=analytics360.php
  • Remove defaults: array( 'newpost', 'comments', 'visit', 'dashboard' )

plugin in action

plugin in action

<?php
/**
 * Plugin Name: Modify My Sites admin menu
 * Plugin URI: http://wordpress.stackexchange.com/q/55724/12615
 * Description: Add an admin menu link to specific plugin, if it is active in the site. Can remove default items as well.
 * Author: Rodolfo Buaiz
 * Author URI: http://wordpress.stackexchange.com/users/12615/brasofilo
 * License: GPLv3
 *  
 * Class based in http://wordpress.stackexchange.com/a/77236/12615
 **/

 
WPSE_55724_My_Sites_Extra_Items::init(  
    'Analytics360', // Menu item title
    'analytics360/analytics360.php', // Plugin to check for
    'index.php?page=analytics360.php', // Menu item URL
    array( 'newpost', 'comments', 'visit', 'dashboard' ) // Remove defaults
); 

class WPSE_55724_My_Sites_Extra_Items
{
    private static $ins = null;

    private static $title = null;
    private static $plugin_file = null;
    private static $plugin_page = null;
    private static $remove_defaults = null;

    public static function instance()
    {
        is_null( self::$ins ) && self::$ins = new self;
        return self::$ins;
    }

    public static function init( $title, $plugin_file, $plugin_page, $remove_defaults )
    {
        self::$title = $title;
        self::$plugin_file = $plugin_file;
        self::$plugin_page = $plugin_page;
        self::$remove_defaults = $remove_defaults;
        
        if( is_multisite() )
            add_action( 'plugins_loaded', array( self::instance(), '_setup' ) );
    }

    public function _setup()
    {
        if( is_admin_bar_showing() )
            add_action( 'admin_bar_menu', array( $this, 'add_items' ), 9999 );           
    }

    /**
     * The global var is passed by reference in a do_action_ref_array
     */
    public function add_items( $wp_admin_bar )  
    {
        foreach ( (array) $wp_admin_bar->user->blogs as $blog ) 
        {
            switch_to_blog( $blog->userblog_id );
             
            // Current menu ID
            $menu_id  = 'blog-' . $blog->userblog_id;
            
            // Add URL to current-site/wp-admin/plugins.php
            $wp_admin_bar->add_menu( array(
                'parent' => $menu_id,
                'id'     => $menu_id . '-p',
                'title'  => __( 'Plugins' ),
                'href'   => admin_url( 'plugins.php' ),
            ) );
             
            // Remove default menu items
            $this->remove_items(  
                &$wp_admin_bar,  
                self::$remove_defaults,
                $menu_id
            );
                    
            // Add custom menu for chosen Plugin it active in current site  
            if( current_user_can( 'manage_options' ) && $this->is_plugin_active( self::$plugin_file ))
            {
                $wp_admin_bar->add_menu( array(
                    'parent' => $menu_id,
                    'id'     => $menu_id . '-my-p',
                    'title'  => self::$title,
                    'href'   => admin_url( self::$plugin_page ),
                ) );
            }
                 
            restore_current_blog();
        }
    }    

    /**
     *  
     */
    private function remove_items( $wp_admin_bar, $items, $menu_id )
    {
        if( in_array( 'newpost', $items ) )
            $wp_admin_bar->remove_menu( $menu_id . '-n' ); // New Post

        if( in_array( 'comments', $items ) )
            $wp_admin_bar->remove_menu( $menu_id . '-c' ); // Comments
            
        if( in_array( 'visit', $items ) )
            $wp_admin_bar->remove_menu( $menu_id . '-v' ); // Visit Site
             
        if( in_array( 'dashboard', $items ) )
            $wp_admin_bar->remove_menu( $menu_id . '-d' ); // Dashboard
    }


    /**
     * WP function uses is_plugin_active_for_network 
     * which doesn't work in frontend
     */
    private function is_plugin_active( $plugin )
    {
        return in_array( $plugin, (array) get_option( 'active_plugins', array() ) );
    }
}
Bounty Ended with 50 reputation awarded by Ünsal Korkmaz
New code as per comments clarification.
Source Link
brasofilo
  • 22.1k
  • 8
  • 69
  • 264

If I understood your question correctly1, wrapping the action call insideHaven't found a Class avoidsway of adding the new item, so am rebuilding the menu.

enter image description here

The only caveat is not being displayed in all sites ofable to position the networknew menu item at the end.

1
its only showing when i am in that blog => I understand that you mean: "it is not only showing..."
When i am in blogid:3 or blogid:4, i cant see that menu in blogid:2's list => I understand that you mean: "I can see the menu of blogid:2..."

/*
Plugin Name: The Menu Item
Plugin URI: http://wordpress.stackexchange.com/questions/55724/
Description: Add Menu Items to Admin Bar
Version: 1.01
Author: wordpress.stackexchangebrasofilo
*/

class Brsfl_55724_MenuItem
{

    public function __construct()
    {       
        add_action( 'admin_bar_menu''wp_before_admin_bar_render', array( $this, 'toolbar_link_to_mypage' ), 9999999 );
    }

    // http://stackoverflow.com/a/6228630/1287812
    private function contains( $string, array $search, $caseInsensitive=false )
    {
        $exp = '/'.implode('|',array_map('preg_quote',$search)).($caseInsensitive?'/i':'/');
        return preg_match($exp, $string)?true:false;
    }
    
    public function toolbar_link_to_mypage( $wp_admin_bar ) 
    {
        $menu_idglobal $wp_admin_bar;
        $the_admin_bar = 'my_page';$wp_admin_bar->get_nodes();
        
        $argsforeach( =$the_admin_bar array(as $key=>$value )
        {
            'id'// Get Only the =>Blog $menu_idNodes
            if( strpos( $key, 'blog-' ) !== false ) 
            'title'{
 => 'The Menu Item',            // Get Blog ID
            'href'  => '#' $pos = substr( $key, strpos( $key,'-' ) + 1 );
            'meta'  =>  
                // Blog Menu Items
                $search_sub_menus = array('class' => 'my'-toolbard','-page'n','-c','-v' );
        );        
                // Blog Dashboar Address
        $args2 =       $search_dashboard_url = array( '-d' );
            'parent' => $menu_id,  
            'id'    // =>Remove $menu_idEach .Blog '-example',Node and Subnodes
            'title'  => __ $wp_admin_bar->remove_node( 'Example$key Menu);
 Item', 'example' ),             
            'href'   => get_admin_url// Grab the Blog Address
                if( $blog$this->userblog_id>contains( $key, 'edit.php?post_type=example'$search_dashboard_url ), )
            'meta'   => array('class' => 'my  $site_url = $value-toolbar>href;
                    
                // Main Blog Submenu
                if( $this-page'>contains( $key, $search_sub_menus ) ) 
                {
                    $wp_admin_bar->add_node( $value );
                } 
                // Blogs Sub-submenus
                else 
                {
                    // Add the Old Node
                    $wp_admin_bar->add_node( $value );

                    // Creation of the New Sub-submenu Item
                    $id = 'blog-'.$pos.'-p';
                    $args = array(
                        'id' => $id
                    ,   'parent' => $key
                    ,   'title' => 'My Custom URL'
                    ,   'href' => $site_url.'edit.php?post_type=example'
                    );
                    $wp_admin_bar->add_node($args2$args);
                }
            }
        }
    }   
}

$brsfl_55724_MenuItem = new Brsfl_55724_MenuItem();

If I understood your question correctly1, wrapping the action call inside a Class avoids the menu being displayed in all sites of the network.

1
its only showing when i am in that blog => I understand that you mean: "it is not only showing..."
When i am in blogid:3 or blogid:4, i cant see that menu in blogid:2's list => I understand that you mean: "I can see the menu of blogid:2..."

/*
Plugin Name: The Menu Item
Plugin URI: http://wordpress.stackexchange.com/questions/55724/
Description: Add Menu Items to Admin Bar
Version: 1.0
Author: wordpress.stackexchange
*/

class Brsfl_55724_MenuItem
{

    public function __construct()
    {       
        add_action( 'admin_bar_menu', array($this, 'toolbar_link_to_mypage'), 999 );
    }

    function toolbar_link_to_mypage( $wp_admin_bar ) 
    {
        $menu_id = 'my_page';
        
        $args = array(
            'id'    => $menu_id,
            'title' => 'The Menu Item',
            'href'  => '#',
            'meta'  => array('class' => 'my-toolbar-page')
        );
        
        $args2 =  array(
            'parent' => $menu_id,
            'id'     => $menu_id . '-example',
            'title'  => __( 'Example Menu Item', 'example' ),
            'href'   => get_admin_url( $blog->userblog_id, 'edit.php?post_type=example' ),
            'meta'   => array('class' => 'my-toolbar-page')
        );
        
        $wp_admin_bar->add_node($args);
        $wp_admin_bar->add_node($args2);
    }
    
}
$brsfl_55724_MenuItem = new Brsfl_55724_MenuItem();

Haven't found a way of adding the new item, so am rebuilding the menu.

enter image description here

The only caveat is not being able to position the new menu item at the end.

/*
Plugin Name: The Menu Item
Plugin URI: http://wordpress.stackexchange.com/questions/55724/
Description: Add Menu Items to Admin Bar
Version: 1.1
Author: brasofilo
*/

class Brsfl_55724_MenuItem
{

    public function __construct()
    {       
        add_action( 'wp_before_admin_bar_render', array( $this, 'toolbar_link_to_mypage' ), 9999 );
    }

    // http://stackoverflow.com/a/6228630/1287812
    private function contains( $string, array $search, $caseInsensitive=false )
    {
        $exp = '/'.implode('|',array_map('preg_quote',$search)).($caseInsensitive?'/i':'/');
        return preg_match($exp, $string)?true:false;
    }
    
    public function toolbar_link_to_mypage( $wp_admin_bar ) 
    {
        global $wp_admin_bar;
        $the_admin_bar = $wp_admin_bar->get_nodes();
        
        foreach( $the_admin_bar as $key=>$value )
        {
            // Get Only the Blog Nodes
            if( strpos( $key, 'blog-' ) !== false ) 
            {
                // Get Blog ID
                $pos = substr( $key, strpos( $key,'-' ) + 1 );
                
                // Blog Menu Items
                $search_sub_menus = array( '-d','-n','-c','-v' );
                
                // Blog Dashboar Address
                $search_dashboard_url = array( '-d' );
                
                // Remove Each Blog Node and Subnodes
                $wp_admin_bar->remove_node( $key );
                
                // Grab the Blog Address
                if( $this->contains( $key, $search_dashboard_url ) )
                    $site_url = $value->href;
                    
                // Main Blog Submenu
                if( $this->contains( $key, $search_sub_menus ) ) 
                {
                    $wp_admin_bar->add_node( $value );
                } 
                // Blogs Sub-submenus
                else 
                {
                    // Add the Old Node
                    $wp_admin_bar->add_node( $value );

                    // Creation of the New Sub-submenu Item
                    $id = 'blog-'.$pos.'-p';
                    $args = array(
                        'id' => $id
                    ,   'parent' => $key
                    ,   'title' => 'My Custom URL'
                    ,   'href' => $site_url.'edit.php?post_type=example'
                    );
                    $wp_admin_bar->add_node($args);
                }
            }
        }
    }   
}

$brsfl_55724_MenuItem = new Brsfl_55724_MenuItem();
Source Link
brasofilo
  • 22.1k
  • 8
  • 69
  • 264
Loading