public function update_item( $request ) {
		$id = (int) $request['id'];

		$user = get_userdata( $id );
		if ( ! $user ) {
			return new WP_Error( 'rest_user_invalid_id', __( 'Invalid resource id.' ), array( 'status' => 404 ) );
		}

		if ( email_exists( $request['email'] ) && $request['email'] !== $user->user_email ) {
			return new WP_Error( 'rest_user_invalid_email', __( 'Email address is invalid.' ), array( 'status' => 400 ) );
		}

		if ( ! empty( $request['username'] ) && $request['username'] !== $user->user_login ) {
			return new WP_Error( 'rest_user_invalid_argument', __( "Username isn't editable." ), array( 'status' => 400 ) );
		}

		if ( ! empty( $request['slug'] ) && $request['slug'] !== $user->user_nicename && get_user_by( 'slug', $request['slug'] ) ) {
			return new WP_Error( 'rest_user_invalid_slug', __( 'Slug is invalid.' ), array( 'status' => 400 ) );
		}

		if ( ! empty( $request['roles'] ) ) {
			$check_permission = $this->check_role_update( $id, $request['roles'] );
			if ( is_wp_error( $check_permission ) ) {
				return $check_permission;
			}
		}

		$user = $this->prepare_item_for_database( $request );

		// Ensure we're operating on the same user we already checked
		$user->ID = $id;

		$user_id = wp_update_user( $user );
		if ( is_wp_error( $user_id ) ) {
			return $user_id;
		}

		$user = get_user_by( 'id', $id );
		if ( ! empty( $request['roles'] ) ) {
			array_map( array( $user, 'add_role' ), $request['roles'] );
		}

		$schema = $this->get_item_schema();
		if ( ! empty( $schema['properties']['meta'] ) && isset( $request['meta'] ) ) {
			$meta_update = $this->meta->update_value( $request['meta'], $id );
			if ( is_wp_error( $meta_update ) ) {
				return $meta_update;
			}
		}

		$fields_update = $this->update_additional_fields_for_object( $user, $request );
		if ( is_wp_error( $fields_update ) ) {
			return $fields_update;
		}

		/* This action is documented in lib/endpoints/class-wp-rest-users-controller.php */
		do_action( 'rest_insert_user', $user, $request, false );

		$request->set_param( 'context', 'edit' );
		$response = $this->prepare_item_for_response( $user, $request );
		$response = rest_ensure_response( $response );
		return $response;
	}
public function update_item( $request ) {
		$id = (int) $request['id'];

		$user = get_userdata( $id );
		if ( ! $user ) {
			return new WP_Error( 'rest_user_invalid_id', __( 'Invalid resource id.' ), array( 'status' => 404 ) );
		}

		if ( email_exists( $request['email'] ) && $request['email'] !== $user->user_email ) {
			return new WP_Error( 'rest_user_invalid_email', __( 'Email address is invalid.' ), array( 'status' => 400 ) );
		}

		if ( ! empty( $request['username'] ) && $request['username'] !== $user->user_login ) {
			return new WP_Error( 'rest_user_invalid_argument', __( "Username isn't editable." ), array( 'status' => 400 ) );
		}

		if ( ! empty( $request['slug'] ) && $request['slug'] !== $user->user_nicename && get_user_by( 'slug', $request['slug'] ) ) {
			return new WP_Error( 'rest_user_invalid_slug', __( 'Slug is invalid.' ), array( 'status' => 400 ) );
		}

		if ( ! empty( $request['roles'] ) ) {
			$check_permission = $this->check_role_update( $id, $request['roles'] );
			if ( is_wp_error( $check_permission ) ) {
				return $check_permission;
			}
		}

		$user = $this->prepare_item_for_database( $request );

		// Ensure we're operating on the same user we already checked
		$user->ID = $id;

		$user_id = wp_update_user( $user );
		if ( is_wp_error( $user_id ) ) {
			return $user_id;
		}

		$user = get_user_by( 'id', $id );
		if ( ! empty( $request['roles'] ) ) {
			array_map( array( $user, 'add_role' ), $request['roles'] );
		}

		$schema = $this->get_item_schema();
		if ( ! empty( $schema['properties']['meta'] ) && isset( $request['meta'] ) ) {
			$meta_update = $this->meta->update_value( $request['meta'], $id );
			if ( is_wp_error( $meta_update ) ) {
				return $meta_update;
			}
		}

		$fields_update = $this->update_additional_fields_for_object( $user, $request );
		if ( is_wp_error( $fields_update ) ) {
			return $fields_update;
		}

		/* This action is documented in lib/endpoints/class-wp-rest-users-controller.php */
		do_action( 'rest_insert_user', $user, $request, false );

		$request->set_param( 'context', 'edit' );
		$response = $this->prepare_item_for_response( $user, $request );
		$response = rest_ensure_response( $response );
		return $response;
	}
public function update_item ( $ request ) { $ id = ( int ) $ request [ 'id' ] ; $ user = get_userdata ( $ id ) ; if ( ! $ user ) { return new WP_Error ( 'rest_user_invalid_id' , __ ( 'Invalid resource id.' ) , array ( 'status' => 404 ) ) ; } if ( email_exists ( $ request [ 'email' ] ) && $ request [ 'email' ] !== $ user -> user_email ) { return new WP_Error ( 'rest_user_invalid_email' , __ ( 'Email address is invalid.' ) , array ( 'status' => 400 ) ) ; } if ( ! empty ( $ request [ 'username' ] ) && $ request [ 'username' ] !== $ user -> user_login ) { return new WP_Error ( 'rest_user_invalid_argument' , __ ( "Username isn't editable." ) , array ( 'status' => 400 ) ) ; } if ( ! empty ( $ request [ 'slug' ] ) && $ request [ 'slug' ] !== $ user -> user_nicename && get_user_by ( 'slug' , $ request [ 'slug' ] ) ) { return new WP_Error ( 'rest_user_invalid_slug' , __ ( 'Slug is invalid.' ) , array ( 'status' => 400 ) ) ; } if ( ! empty ( $ request [ 'roles' ] ) ) { $ check_permission = $ this -> check_role_update ( $ id , $ request [ 'roles' ] ) ; if ( is_wp_error ( $ check_permission ) ) { return $ check_permission ; } } $ user = $ this -> prepare_item_for_database ( $ request ) ; // Ensure we're operating on the same user we already checked $ user -> ID = $ id ; $ user_id = wp_update_user ( $ user ) ; if ( is_wp_error ( $ user_id ) ) { return $ user_id ; } $ user = get_user_by ( 'id' , $ id ) ; if ( ! empty ( $ request [ 'roles' ] ) ) { array_map ( array ( $ user , 'add_role' ) , $ request [ 'roles' ] ) ; } $ schema = $ this -> get_item_schema ( ) ; if ( ! empty ( $ schema [ 'properties' ] [ 'meta' ] ) && isset ( $ request [ 'meta' ] ) ) { $ meta_update = $ this -> meta -> update_value ( $ request [ 'meta' ] , $ id ) ; if ( is_wp_error ( $ meta_update ) ) { return $ meta_update ; } } $ fields_update = $ this -> update_additional_fields_for_object ( $ user , $ request ) ; if ( is_wp_error ( $ fields_update ) ) { return $ fields_update ; } /* This action is documented in lib/endpoints/class-wp-rest-users-controller.php */ do_action ( 'rest_insert_user' , $ user , $ request , false ) ; $ request -> set_param ( 'context' , 'edit' ) ; $ response = $ this -> prepare_item_for_response ( $ user , $ request ) ; $ response = rest_ensure_response ( $ response ) ; return $ response ; }
Update a single user

@param WP_REST_Request $request Full details about the request.
@return WP_Error|WP_REST_Response
Update a single user