Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.
    $wp_admin_bar->add_menu( 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' ),
        ) );

Lets say i have a plugin that add a new menu item for an example custom post type to blog menu. Problem is if i have lots of blogs but this plugin activated on only 1 blog, its only showing when i am in that blog and adding example menu to every blog menu group even that plugin is not activated in those blogs..

Example: I activate plugin in blogid:2 but didnt activate it in blogid:3 or blogid:4. When i am in blogid:2 plugin adding that menu to all blogs. When i am in blogid:3 or blogid:4, i cant see that menu in blogid:2's list What is proper way to fix this?

Update: I am trying to add those links for each blog activated that plugin in "My Sites" list

share|improve this question
    
Where are you hooking this action? Also, take a look at the Note in the Codex: codex.wordpress.org/Function_Reference/add_menu –  brasofilo Jun 22 '12 at 21:28
    
wp_before_admin_bar_render –  Ünsal Korkmaz Jun 22 '12 at 21:55

1 Answer 1

up vote 1 down vote accepted
+50

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().

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 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;
    }
}
share|improve this answer
    
$menu_id = 'blog-' . $blog->userblog_id; wp-includes/admin-bar.php is where real action happening. Check functions after foreach ( (array) $wp_admin_bar->user->blogs as $blog ) { –  Ünsal Korkmaz Jun 22 '12 at 23:06
    
i am trying to add those links for each blog in "My Sites" list –  Ünsal Korkmaz Jun 22 '12 at 23:11
    
nope.. it will be activate on some blogs.. not all of them –  Ünsal Korkmaz Jun 23 '12 at 0:00
    
i am in a bit busy situation and i found a custom solution for this problem. i will update this question later if i think its good for every1. I couldnt try your solution but accepting as answer since bounty time is ending. Thanks a lot for your time –  Ünsal Korkmaz Jun 29 '12 at 23:38
    
@ÜnsalKorkmaz, I've remade the Answer and cleaned my previous comments. –  brasofilo Apr 29 '13 at 15:10

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.