Skip to content

Instantly share code, notes, and snippets.

@BretCameron
Last active November 14, 2022 18:58
Show Gist options
  • Save BretCameron/b69e4a44e6c2a243223d76232fd23790 to your computer and use it in GitHub Desktop.
Save BretCameron/b69e4a44e6c2a243223d76232fd23790 to your computer and use it in GitHub Desktop.
Make featured image URLs available as JSON in WordPress's REST API
<?php // Add 'featured_image_url' to REST data
function post_featured_image_json( $data, $post, $context ) {
$featured_image_id = $data->data['featured_media'];
$featured_image_url = wp_get_attachment_image_src( $featured_image_id, 'original' );
if( $featured_image_url ) {
$data->data['featured_image_url'] = $featured_image_url[0];
}
return $data;
}
add_filter( 'rest_prepare_movies', 'post_featured_image_json', 10, 3 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment