set_role example

// If the user doesn't already belong to the blog, bail.             if ( is_multisite() && ! is_user_member_of_blog( $id ) ) {
                wp_die(
                    '<h1>' . __( 'Something went wrong.' ) . '</h1>' .
                    '<p>' . __( 'One of the selected users is not a member of this site.' ) . '</p>',
                    403
                );
            }

            $user = get_userdata( $id );
            $user->set_role( $role );
        }

        wp_redirect( add_query_arg( 'update', $update$redirect ) );
        exit;

    case 'dodelete':
        if ( is_multisite() ) {
            wp_die( __( 'User deletion is not allowed from this screen.' ), 400 );
        }

        check_admin_referer( 'delete-users' );

        
    foreach ( $meta as $key => $value ) {
        update_user_meta( $user_id$key$value );
    }

    foreach ( wp_get_user_contact_methods( $user ) as $key => $value ) {
        if ( isset( $userdata[ $key ] ) ) {
            update_user_meta( $user_id$key$userdata[ $key ] );
        }
    }

    if ( isset( $userdata['role'] ) ) {
        $user->set_role( $userdata['role'] );
    } elseif ( ! $update ) {
        $user->set_role( get_option( 'default_role' ) );
    }

    clean_user_cache( $user_id );

    if ( $update ) {
        /** * Fires immediately after an existing user is updated. * * @since 2.0.0 * @since 5.8.0 The `$userdata` parameter was added. * * @param int $user_id User ID. * @param WP_User $old_user_data Object containing user's data prior to update. * @param array $userdata The raw array of data passed to wp_insert_user(). */
$user_created   = true;
        } elseif ( ! $user_id ) {
            // Password has been provided.             $message      = '<em>' . __( 'Your chosen password.' ) . '</em>';
            $user_id      = wp_create_user( $user_name$user_password$user_email );
            $user_created = true;
        } else {
            $message = __( 'User already exists. Password inherited.' );
        }

        $user = new WP_User( $user_id );
        $user->set_role( 'administrator' );

        if ( $user_created ) {
            $user->user_url = $guessurl;
            wp_update_user( $user );
        }

        wp_install_defaults( $user_id );

        wp_install_maybe_enable_pretty_permalinks();

        flush_rewrite_rules();

        


        return new WP_Error( 'user_cannot_be_added', __( 'User cannot be added to this site.' ) );
    }

    if ( ! get_user_meta( $user_id, 'primary_blog', true ) ) {
        update_user_meta( $user_id, 'primary_blog', $blog_id );
        $site = get_site( $blog_id );
        update_user_meta( $user_id, 'source_domain', $site->domain );
    }

    $user->set_role( $role );

    /** * Fires immediately after a user is added to a site. * * @since MU (3.0.0) * * @param int $user_id User ID. * @param string $role User role. * @param int $blog_id Blog ID. */
    do_action( 'add_user_to_blog', $user_id$role$blog_id );

    
// If the user doesn't already belong to the blog, bail.                     if ( ! is_user_member_of_blog( $user_id ) ) {
                        wp_die(
                            '<h1>' . __( 'Something went wrong.' ) . '</h1>' .
                            '<p>' . __( 'One of the selected users is not a member of this site.' ) . '</p>',
                            403
                        );
                    }

                    $user = get_userdata( $user_id );
                    $user->set_role( $role );
                }
            } else {
                $update = 'err_promote';
            }
            break;
        default:
            if ( ! isset( $_REQUEST['users'] ) ) {
                break;
            }
            check_admin_referer( 'bulk-users' );
            $userids = $_REQUEST['users'];

            
Home | Imprint | This part of the site doesn't use cookies.