edit_user example

check_admin_referer( 'create-user', '_wpnonce_create-user' );

    if ( ! current_user_can( 'create_users' ) ) {
        wp_die(
            '<h1>' . __( 'You need a higher level of permission.' ) . '</h1>' .
            '<p>' . __( 'Sorry, you are not allowed to create users.' ) . '</p>',
            403
        );
    }

    if ( ! is_multisite() ) {
        $user_id = edit_user();

        if ( is_wp_error( $user_id ) ) {
            $add_user_errors = $user_id;
        } else {
            if ( current_user_can( 'list_users' ) ) {
                $redirect = 'users.php?update=add&id=' . $user_id;
            } else {
                $redirect = add_query_arg( 'update', 'add', 'user-new.php' );
            }
            wp_redirect( $redirect );
            die();
        }


/** * Creates a new user from the "Users" form using $_POST information. * * @since 2.0.0 * * @return int|WP_Error WP_Error or User ID. */
function add_user() {
    return edit_user();
}

/** * Edit user settings based on contents of $_POST * * Used on user-edit.php and profile.php to manage and process user options, passwords etc. * * @since 2.0.0 * * @param int $user_id Optional. User ID. * @return int|WP_Error User ID of the updated user or WP_Error on failure. */
function wp_ajax_add_user( $action ) {
    if ( empty( $action ) ) {
        $action = 'add-user';
    }

    check_ajax_referer( $action );

    if ( ! current_user_can( 'create_users' ) ) {
        wp_die( -1 );
    }

    $user_id = edit_user();

    if ( ! $user_id ) {
        wp_die( 0 );
    } elseif ( is_wp_error( $user_id ) ) {
        $x = new WP_Ajax_Response(
            array(
                'what' => 'user',
                'id'   => $user_id,
            )
        );
        $x->send();
    }
// Update the email address in signups, if present.         if ( is_multisite() ) {
            $user = get_userdata( $user_id );

            if ( $user->user_login && isset( $_POST['email'] ) && is_email( $_POST['email'] ) && $wpdb->get_var( $wpdb->prepare( "SELECT user_login FROM {$wpdb->signups} WHERE user_login = %s", $user->user_login ) ) ) {
                $wpdb->query( $wpdb->prepare( "UPDATE {$wpdb->signups} SET user_email = %s WHERE user_login = %s", $_POST['email']$user_login ) );
            }
        }

        // Update the user.         $errors = edit_user( $user_id );

        // Grant or revoke super admin status if requested.         if ( is_multisite() && is_network_admin()
            && ! IS_PROFILE_PAGE && current_user_can( 'manage_network_options' )
            && ! isset( $super_admins ) && empty( $_POST['super_admin'] ) === is_super_admin( $user_id )
        ) {
            empty( $_POST['super_admin'] ) ? revoke_super_admin( $user_id ) : grant_super_admin( $user_id );
        }

        if ( ! is_wp_error( $errors ) ) {
            $redirect = add_query_arg( 'updated', true, get_edit_user_link( $user_id ) );
            
Home | Imprint | This part of the site doesn't use cookies.