WP_REST_Attachments_Controller::handle_featured_media()protectedWP 6.5.0

Determines the featured media based on a request param.

Method of the class: WP_REST_Attachments_Controller{}

No Hooks.

Return

true|false|WP_Error. Whether the post thumbnail was successfully deleted, otherwise WP_Error.

Usage

// protected - for code of main (parent) or child class
$result = $this->handle_featured_media( $featured_media, $post_id );
$featured_media(int) (required)
Featured Media ID.
$post_id(int) (required)
Post ID.

Changelog

Since 6.5.0 Introduced.

WP_REST_Attachments_Controller::handle_featured_media() code WP 6.7.1

protected function handle_featured_media( $featured_media, $post_id ) {
	$post_type         = get_post_type( $post_id );
	$thumbnail_support = current_theme_supports( 'post-thumbnails', $post_type ) && post_type_supports( $post_type, 'thumbnail' );

	// Similar check as in wp_insert_post().
	if ( ! $thumbnail_support && get_post_mime_type( $post_id ) ) {
		if ( wp_attachment_is( 'audio', $post_id ) ) {
			$thumbnail_support = post_type_supports( 'attachment:audio', 'thumbnail' ) || current_theme_supports( 'post-thumbnails', 'attachment:audio' );
		} elseif ( wp_attachment_is( 'video', $post_id ) ) {
			$thumbnail_support = post_type_supports( 'attachment:video', 'thumbnail' ) || current_theme_supports( 'post-thumbnails', 'attachment:video' );
		}
	}

	if ( $thumbnail_support ) {
		return parent::handle_featured_media( $featured_media, $post_id );
	}

	return new WP_Error(
		'rest_no_featured_media',
		sprintf(
			/* translators: %s: attachment mime type */
			__( 'This site does not support post thumbnails on attachments with MIME type %s.' ),
			get_post_mime_type( $post_id )
		),
		array( 'status' => 400 )
	);
}