在wordpress中为多个网站创建编辑器
问题描述:
在Wordpress多站点平台上,通常如果我创建一个“编辑器”用户帐户,该用户只能对正在创建的网站执行职责。在wordpress中为多个网站创建编辑器
但是我wiling创建谁可以在所有网站执行主编职务专门的编辑器帐户(多点)
任何人都可以指导我如何创建这样的用户?
答
我正在检查Multisite用户管理插件的代码,从Felipe's answer,并决定做一个概念验证。
以下内容仅网络插件添加了以下选项:
当我们选择下拉用户和更新设置,用户中的所有站点添加为编辑网络。
创建新网站时,该用户也将被添加。
重要:
- 有没有
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);
}
}