在wordpress中为多个站点创建编辑器

时间:2014-05-13 06:04:32

标签: php wordpress wordpress-plugin roles user-accounts

在Wordpress多站点平台上,通常如果我创建一个"编辑器"用户帐户,该用户只能对正在创建的网站执行职责。

但是我想创建一个特殊的编辑帐户,可以在所有网站(多个网站)上执行编辑职责

有人可以指导我如何创建这样的用户吗?

2 个答案:

答案 0 :(得分:2)

我认为您可以使用插件Multisite user management执行所需操作。这应该允许您将用户和角色复制到所需的站点...或者您可以创建一个新的管理员帐户。

答案 1 :(得分:1)

我正在检查来自Felipe's answer的多站点用户管理插件的代码,并决定进行概念验证。

以下仅限网络的插件添加以下选项:

option in /wp-admin/network/settings.php

当我们在下拉列表中选择用户并更新设置时,用户在网络的所有站点中添加为编辑
创建新网站时,也会添加此用户。

重要:

  • 没有undo 功能,更新设置后,您只能删除/更改网站中的用户基础即可。
<?php
/*
Plugin Name: (SO) Multisite Super Editor
Plugin URI: https://stackoverflow.com/q/23623835/1287812
Description: Add a user as Editor in all sites of the network
Author: brasofilo
Network: true
Version: 1.0
*/
B5F_Multisite_Super_Editor::init();

class B5F_Multisite_Super_Editor 
{
    static $option = 'super_editor';

    static function init() 
    {
        add_action( 'wpmu_new_blog', array( __CLASS__, 'new_site'), 10, 6 );
        add_action( 'wpmu_options', array( __CLASS__, 'options_network' ) );
        add_action( 'update_wpmu_options', array( __CLASS__, 'options_update' ) );
    }

    /**
     * Add Super Editor to newly created blogs
     */
    static function new_site( $blog_id, $user_id, $domain, $path, $site_id, $meta )
    {
        $saved =  get_site_option( self::$option );
        add_user_to_blog( $blog_id, $saved, 'editor' );
    }

    /**
     * Outputs the user selection on the 'Network Admin | Settings' page. 
     */
    static function options_network()
    {
        echo '<h3>' . __( 'Super Editor' ). '</h3>';
        $users = get_users();
        $supers = get_site_option( 'site_admins', array('admin') );
        $saved =  get_site_option( self::$option );
        $selected = $saved ? $saved : '';
        $exclude = array();

        // site_admins only has the user_login, hence this loop to get the IDs
        foreach( $users as $user )
            if( in_array( $user->data->user_login, $supers ) )
                $exclude[] = $user->data->ID;

        wp_dropdown_users( array( 
            'blog_id' => 0, // Default is current blog
            'exclude' => $exclude, 
            'name' => 'post_author', 
            'multi' => true, 
            'show_option_none' => __('None'), 
            'name' => 'b5f_default_user_role', 
            'selected' => $selected 
        ));
    }

    /**
     * UPDATE Super Editor option and APPLY the role in all sites
     */
    static function options_update()
    {
        if( !isset( $_POST[ 'b5f_default_user_role' ] ) )
            return;

        $user_id = $_POST[ 'b5f_default_user_role' ];
        $saved =  get_site_option( self::$option );
        if( $saved == $user_id )
            return;

        update_site_option( self::$option, $user_id );  
        foreach( self::get_blogs( 0, 'all' ) as $key => $blog ) 
            add_user_to_blog( $blog[ 'blog_id' ], $user_id, 'editor' );
    }

    /**
     * Based on the deprecated WPMU get_blog_list function. 
     * 
     * Except this function gets all blogs, even if they are marked as mature and private.
     */
    static function get_blogs( $start = 0, $num = 10 ) 
    {
        global $wpdb;

        $blogs = $wpdb->get_results( $wpdb->prepare( "SELECT blog_id, domain, path FROM $wpdb->blogs WHERE site_id = %d AND archived = '0' AND spam = '0' AND deleted = '0' ORDER BY registered DESC", $wpdb->siteid ), ARRAY_A );

        foreach ( (array) $blogs as $details ) {
            $blog_list[ $details[ 'blog_id' ] ] = $details;
            $blog_list[ $details[ 'blog_id' ] ]['postcount'] = $wpdb->get_var( "SELECT COUNT(ID) FROM " . $wpdb->get_blog_prefix( $details['blog_id'] ). "posts WHERE post_status='publish' AND post_type='post'" );
        }
        unset( $blogs );
        $blogs = $blog_list;

        if ( false == is_array( $blogs ) )
            return array();

        if ( $num == 'all' )
            return array_slice( $blogs, $start, count( $blogs ) );
        else
            return array_slice( $blogs, $start, $num );
    }
}