0

I have a multisite with the following lines of code in my functions.php file:

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
));

This creates a new/custom user role called Test Priv.

I'm not very familiar with PHP, but would like to add the above settings to the default administrators role (not the super admin), rather than creating a new/custom role.

In particularly, how can I update the default administrators user role on a multisite so that they are able to add/create/delete/remove users?

  • The roles are in each site of the network a own object. If you change the role, than doing this in each site of network. You can doing this with a loop about all sites of the network and inside this loop doing your requirements for the roles. – bueltge Sep 1 '15 at 11:22
  • That's fine if the user who is set to have an 'admin' role can modify other sites within my network. Could you please provide this solution with the necessary code to paste in to my functions.php file, I'll test it out and if all works well..I can mark this question as answered. Thanks. – user1752759 Sep 1 '15 at 12:26
  • See the answer, should help you. – bueltge Sep 2 '15 at 17:37
0

You can write a small custom function, that loop about the network and add in each site of the network the new role. See the source below als example to add_role. If you like update or change a existing role, then switch from add_role to add_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();
    }
}

The important part to add a object in each site of the network is the loop and the core functions:

        switch_to_blog( $blog[ 'blog_id' ] );

        ...

        restore_current_blog();

Inside this loop add your requirements with the parameters and it will doing this in each site.

Enhanced, also a small example to add users, identify via User-ID to each site of the network.

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();
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.