Multisite Admin Roles

时间:2015-09-01 作者:user1752759

我有一个multisite 在我的functions.php 文件:

add_role(\'test\', \'Test Priv\', array(
    \'activate_plugins\' => true,
    \'delete_others_pages\' => true,
    \'delete_others_posts\' => true,
    \'delete_pages\' => true,
    \'delete_posts\' => true,
    \'delete_private_pages\' => true,
    \'delete_private_posts\' => true,
    \'delete_published_pages\' => true,
    \'delete_published_posts\' => true,
    \'edit_dashboard\' => true,
    \'edit_others_pages\' => true,
    \'edit_others_posts\' => true,
    \'edit_pages\' => true,
    \'edit_posts\' => true,
    \'edit_private_pages\' => true,
    \'edit_private_posts\' => true,
    \'edit_published_pages\' => true,
    \'edit_published_posts\' => true,
    \'edit_theme_options\' => true,
    \'export\' => true,
    \'import\' => true,
    \'list_users\' => true,
    \'manage_categories\' => true,
    \'manage_links\' => true,
    \'manage_options\' => true,
    \'moderate_comments\' => true,
    \'promote_users\' => true,
    \'publish_pages\' => true,
    \'publish_posts\' => true,
    \'read_private_pages\' => true,
    \'read_private_posts\' => true,
    \'read\' => true,
    \'remove_users\' => true,
    \'switch_themes\' => true,
    \'upload_files\' => true
));
这将创建new/custom user role 称为Test Priv。

我对PHP不是很熟悉,但希望将上述设置添加到default administrators role (不是超级管理员),而不是创建新的/自定义角色。

特别是,如何更新多站点上的默认管理员用户角色,以便他们能够添加/创建/删除/删除用户?

1 个回复
SO网友:bueltge

您可以编写一个小型自定义函数,用于循环网络,并在网络的每个站点中添加新角色。参见下面的源als示例add_role. 如果要更新或更改现有角色,请从add_roleadd_cap.

function fb_change_role_network_wide( $role ) {
    global $wpdb;

    $args = $args = array(
        \'network_id\' => $wpdb->siteid,
        \'public\'     => null,
        \'archived\'   => null,
        \'mature\'     => null,
        \'spam\'       => null,
        \'deleted\'    => null,
        \'limit\'      => 100,
        \'offset\'     => 0,
    );
    $sites = wp_get_sites( $args );

    $role         = \'contributor\';
    $display_name = \'Test Priv\';
    $capabilities = array( ... );

    foreach( $sites as $key => $blog ) {

        switch_to_blog( $blog[ \'blog_id\' ] );

        add_role(  $role, $display_name, $capabilities );

        restore_current_blog();
    }
}
在网络的每个站点中添加对象的重要部分是循环和核心功能:

        switch_to_blog( $blog[ \'blog_id\' ] );

        ...

        restore_current_blog();
在这个循环中,使用参数添加您的需求,它将在每个站点中执行此操作。

增强,也是一个添加用户的小示例,通过用户ID识别网络的每个站点。

function fb_add_roles( $user_id ) {
    global $wpdb;

    $args = $args = array(
        \'network_id\' => $wpdb->siteid,
        \'public\'     => null,
        \'archived\'   => null,
        \'mature\'     => null,
        \'spam\'       => null,
        \'deleted\'    => null,
        \'limit\'      => 100,
        \'offset\'     => 0,
    );
    $sites = wp_get_sites( $args );

    $role = \'contributor\';

    foreach( $sites as $key => $blog ) {

        if ( is_user_member_of_blog( $user_id, $blog[ \'blog_id\' ] ) ) {
            continue;
        }

        switch_to_blog( $blog[ \'blog_id\' ] );

        add_user_to_blog( $blog[ \'blog_id\' ], $user_id, $role );

        restore_current_blog();
    }
}

相关推荐

在发布POST-Functions.php一段时间后设置对象术语。

几天后,我不得不将一个类别中已发布的帖子更改为另一个类别。我已经为函数创建了以下代码,并将其添加到函数中。子主题中的php文件。但是,我不知道错误是什么。下面的数据显示了基于循环计算的post列表。唯一缺少的函数是wp\\u set\\u object\\u terms。请帮我解决这个问题。function set_expired_job_categories() { $current_time = time();// define timestamp