在多站点中添加插件菜单

时间:2012-06-19 作者:Ünsal Korkmaz

    $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\' ),
        ) );
假设我有一个插件,可以为博客菜单添加一个新的菜单项,例如自定义帖子类型。问题是,如果我有很多博客,但这个插件只在一个博客上激活,它只在我在那个博客上时显示,并向每个博客菜单组添加示例菜单,即使这些博客中没有激活该插件。。

Example:我在blogid:2中激活了插件,但在blogid:3或blogid:4中没有激活它。当我在blogid:2插件中时,会将该菜单添加到所有博客中。当我在blogid:3或blogid:4中时,我在blogid:2的列表中看不到该菜单。解决这个问题的正确方法是什么?

Update: 我正在尝试在“我的网站”列表中为每个激活该插件的博客添加这些链接

1 个回复
最合适的回答,由SO网友:brasofilo 整理而成

回答重新制作,现在按照问题规范进行操作

如果某个特定插件在网络的每个站点中处于活动状态,则要向该站点添加菜单项,我使用Must Use 插件。仅在以下情况下运行is_multisite() 如果is_admin_bar_showing().

在本例中,使用以下值实例化插件,以检查插件分析360º并删除所有默认菜单项:

菜单项标题:Analytics360analytics360/analytics360.php菜单项URL:index.php?page=analytics360.php删除默认值: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;
    }
}