W3cubDocs

/WordPress

WP_REST_Attachments_Controller::handle_featured_media( int $featured_media, int $post_id ): bool|WP_Error

Determines the featured media based on a request param.

Parameters

$featured_mediaintrequired
Featured Media ID.
$post_idintrequired
Post ID.

Return

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

Source

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 )
	);
}

Changelog

Version Description
6.5.0 Introduced.

© 2003–2024 WordPress Foundation
Licensed under the GNU GPLv2+ License.
https://developer.wordpress.org/reference/classes/wp_rest_attachments_controller/handle_featured_media