UPDATE: All this is great, but unnecessary. It turns out that all you need to do is pass a URL parameter in the WP API request _embed=1 and most of the data will be returned. I always thought it was odd that the featured image was not available… now I know why (to minimize bandwidth unless you want it). Thanks to David Danedo and this wonderful video.
I am playing with the new API feature from Bricks, and I realized that the featured image data is not part of the standard WordPress API. Fortunately, I have learned that you can modify the API’s output to provide what you need. Turning to ChatGPT, I asked “
How do I ensure that the featured image is included in the wp api for all post types that support it?” In a matter of seconds, I had a viable solution that provided more data than I expected. Unfortunately, Bricks doesn’t seem to like the ALT text value and currently doesn’t output it.
Here is the code that needs to be added to your source WordPress website. This will only work on post types where you have enabled thumbnail support and set ‘show_in_rest’ to true.
<?php
/**
* Expose featured image data on all post types that support it.
* Adds:
* - featured_image_url (string)
* - featured_image (object with alt, caption, sizes, srcset, etc.)
*/
add_action( 'init', function () {
// Make sure thumbnails are enabled somewhere in your stack.
add_theme_support( 'post-thumbnails' );
} );
add_action( 'rest_api_init', function () {
// Get every post type that supports thumbnails.
$types = get_post_types_by_support( [ 'thumbnail' ] );
foreach ( $types as $type ) {
// Simple URL convenience field.
register_rest_field(
$type,
'featured_image_url',
[
'get_callback' => function ( $object ) {
$thumb_id = get_post_thumbnail_id( $object['id'] );
return $thumb_id ? wp_get_attachment_url( $thumb_id ) : null;
},
'schema' => [
'description' => 'Full-size featured image URL',
'type' => [ 'string', 'null' ],
'context' => [ 'view', 'edit' ],
],
]
);
// Rich featured image object.
register_rest_field(
$type,
'featured_image',
[
'get_callback' => function ( $object ) {
$attachment_id = get_post_thumbnail_id( $object['id'] );
if ( ! $attachment_id ) {
return null;
}
$full = wp_get_attachment_image_src( $attachment_id, 'full' );
$alt = get_post_meta( $attachment_id, '_wp_attachment_image_alt', true );
$caption = wp_get_attachment_caption( $attachment_id );
$srcset = wp_get_attachment_image_srcset( $attachment_id, 'full' );
$sizes = wp_get_attachment_image_sizes( $attachment_id, 'full' );
$meta = wp_get_attachment_metadata( $attachment_id );
$mime = get_post_mime_type( $attachment_id );
$source = $full ? $full[0] : wp_get_attachment_url( $attachment_id );
$width = $full ? (int) $full[1] : null;
$height = $full ? (int) $full[2] : null;
return [
'id' => $attachment_id,
'source_url' => $source,
'width' => $width,
'height' => $height,
'alt' => (string) $alt,
'caption' => (string) $caption,
'mime_type' => (string) $mime,
'srcset' => $srcset ?: '',
'sizes' => $sizes ?: '',
'media_details' => is_array( $meta ) ? $meta : new stdClass(),
];
},
'schema' => [
'description' => 'Featured image object with common fields',
'type' => [ 'object', 'null' ],
'context' => [ 'view', 'edit' ],
],
]
);
}
} );Add this to your functions.php and snippet manager. You may need to wait five minutes for Bricks to refresh the data, as Bricks has some built-in caching (another item that needs to be adjustable).
Note: If you are using Jetpack’s optimization features, the images may already be available to you using the jetpack_featured_media_url value.







