WP_REST_Posts_Controller::handle_featured_media() WordPress Method

The WP_REST_Posts_Controller::handle_featured_media() method is responsible for handling the setting of a post's featured image via the WordPress REST API. This method accepts an array of data containing the post ID and the image ID to be set as the featured image. The method then updates the post in the WordPress database with the new featured image.

WP_REST_Posts_Controller::handle_featured_media( int $featured_media, int $post_id ) #

Determines the featured media based on a request param.


Parameters

$featured_media

(int)(Required)Featured Media ID.

$post_id

(int)(Required)Post ID.


Top ↑

Return

(bool|WP_Error) Whether the post thumbnail was successfully deleted, otherwise WP_Error.


Top ↑

Source

File: wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php

	protected function handle_featured_media( $featured_media, $post_id ) {

		$featured_media = (int) $featured_media;
		if ( $featured_media ) {
			$result = set_post_thumbnail( $post_id, $featured_media );
			if ( $result ) {
				return true;
			} else {
				return new WP_Error(
					'rest_invalid_featured_media',
					__( 'Invalid featured media ID.' ),
					array( 'status' => 400 )
				);
			}
		} else {
			return delete_post_thumbnail( $post_id );
		}

	}


Top ↑

Changelog

Changelog
VersionDescription
4.7.0Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by theĀ Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.