【问题标题】:Creating a editor for multiple sites in wordpress在 wordpress 中为多个站点创建编辑器
【发布时间】:2014-06-30 16:32:46
【问题描述】:

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

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

谁能指导我如何创建这样的用户?

【问题讨论】:

    标签: php wordpress roles user-accounts


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

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

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

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

      重要提示:

      • noundo功能,更新设置后,您只能逐个站点删除/更改用户。李>
      <?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 );
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-09-30
        • 1970-01-01
        • 2017-09-10
        • 2012-03-18
        • 1970-01-01
        • 1970-01-01
        • 2019-04-20
        • 1970-01-01
        相关资源
        最近更新 更多