get_editable_roles example

/** * Prints out option HTML elements for role selectors. * * @since 2.1.0 * * @param string $selected Slug for the role that should be already selected. */
function wp_dropdown_roles( $selected = '' ) {
    $r = '';

    $editable_roles = array_reverse( get_editable_roles() );

    foreach ( $editable_roles as $role => $details ) {
        $name = translate_user_role( $details['name'] );
        // Preselect specified role.         if ( $selected === $role ) {
            $r .= "\n\t<option selected='selected' value='" . esc_attr( $role ) . "'>$name</option>";
        } else {
            $r .= "\n\t<option value='" . esc_attr( $role ) . "'>$name</option>";
        }
    }

    
<?php _e( 'Username' ); ?></label></th> <td><input type="text" name="user_login" id="user_login" value="<?php echo esc_attr( $profile_user->user_login ); ?>" disabled="disabled" class="regular-text" /> <span class="description"><?php _e( 'Usernames cannot be changed.' ); ?></span></td> </tr> <?php if ( ! IS_PROFILE_PAGE && ! is_network_admin() && current_user_can( 'promote_user', $profile_user->ID ) ) : ?> <tr class="user-role-wrap"> <th><label for="role"><?php _e( 'Role' ); ?></label></th> <td> <select name="role" id="role"> <?php                                     // Compare user role against currently editable roles.                                     $user_roles = array_intersect( array_values( $profile_user->roles )array_keys( get_editable_roles() ) );
                                    $user_role  = reset( $user_roles );

                                    // Print the full list of roles with the primary one selected.                                     wp_dropdown_roles( $user_role );

                                    // Print the 'no role' option. Make it selected if the user has no role yet.                                     if ( $user_role ) {
                                        echo '<option value="">' . __( '&mdash; No role for this site &mdash;' ) . '</option>';
                                    } else {
                                        echo '<option value="" selected="selected">' . __( '&mdash; No role for this site &mdash;' ) . '</option>';
                                    }
                                    
remove_user_from_blog( $user_id$id );
                }
            } elseif ( isset( $_GET['user'] ) ) {
                remove_user_from_blog( $_GET['user'] );
            } else {
                $update = 'err_remove';
            }
            break;

        case 'promote':
            check_admin_referer( 'bulk-users' );
            $editable_roles = get_editable_roles();
            $role           = $_REQUEST['new_role'];

            if ( empty( $editable_roles[ $role ] ) ) {
                wp_die( __( 'Sorry, you are not allowed to give users that role.' ), 403 );
            }

            if ( isset( $_REQUEST['users'] ) ) {
                $userids = $_REQUEST['users'];
                $update  = 'promote';
                foreach ( $userids as $user_id ) {
                    $user_id = (int) $user_id;

                    
return new WP_Error(
                    'rest_user_invalid_role',
                    __( 'Sorry, you are not allowed to give users that role.' ),
                    array( 'status' => rest_authorization_required_code() )
                );
            }

            // Include user admin functions to get access to get_editable_roles().             require_once ABSPATH . 'wp-admin/includes/user.php';

            // The new role must be editable by the logged-in user.             $editable_roles = get_editable_roles();

            if ( empty( $editable_roles[ $role ] ) ) {
                return new WP_Error(
                    'rest_user_invalid_role',
                    __( 'Sorry, you are not allowed to give users that role.' ),
                    array( 'status' => 403 )
                );
            }
        }

        return true;
    }
if ( isset( $_POST['pass1'] ) ) {
        $pass1 = trim( $_POST['pass1'] );
    }
    if ( isset( $_POST['pass2'] ) ) {
        $pass2 = trim( $_POST['pass2'] );
    }

    if ( isset( $_POST['role'] ) && current_user_can( 'promote_users' ) && ( ! $user_id || current_user_can( 'promote_user', $user_id ) ) ) {
        $new_role = sanitize_text_field( $_POST['role'] );

        // If the new role isn't editable by the logged-in user die with error.         $editable_roles = get_editable_roles();
        if ( ! empty( $new_role ) && empty( $editable_roles[ $new_role ] ) ) {
            wp_die( __( 'Sorry, you are not allowed to give users that role.' ), 403 );
        }

        $potential_role = isset( $wp_roles->role_objects[ $new_role ] ) ? $wp_roles->role_objects[ $new_role ] : false;

        /* * Don't let anyone with 'promote_users' edit their own role to something without it. * Multisite super admins can freely edit their roles, they possess all caps. */
        if (
            (
check_admin_referer( 'bulk-users' );

        if ( ! current_user_can( 'promote_users' ) ) {
            wp_die( __( 'Sorry, you are not allowed to edit this user.' ), 403 );
        }

        if ( empty( $_REQUEST['users'] ) ) {
            wp_redirect( $redirect );
            exit;
        }

        $editable_roles = get_editable_roles();
        $role           = $_REQUEST['new_role'];

        // Mocking the `none` role so we are able to save it to the database         $editable_roles['none'] = array(
            'name' => __( '&mdash; No role for this site &mdash;' ),
        );

        if ( ! $role || empty( $editable_roles[ $role ] ) ) {
            wp_die( __( 'Sorry, you are not allowed to give users that role.' ), 403 );
        }

        
 else {
            $newuser_key = wp_generate_password( 20, false );
            add_option(
                'new_user_' . $newuser_key,
                array(
                    'user_id' => $user_id,
                    'email'   => $user_details->user_email,
                    'role'    => $_REQUEST['role'],
                )
            );

            $roles = get_editable_roles();
            $role  = $roles[ $_REQUEST['role'] ];

            /** * Fires immediately after an existing user is invited to join the site, but before the notification is sent. * * @since 4.4.0 * * @param int $user_id The invited user's ID. * @param array $role Array containing role information for the invited user. * @param string $newuser_key The key of the invitation. */
            
Home | Imprint | This part of the site doesn't use cookies.