Skip to content

Instantly share code, notes, and snippets.

@wuletawwonte
Created November 24, 2021 17:33
Show Gist options
  • Save wuletawwonte/c250fd114ed22970d6303e8f0dbdb40e to your computer and use it in GitHub Desktop.
Save wuletawwonte/c250fd114ed22970d6303e8f0dbdb40e to your computer and use it in GitHub Desktop.
A php function to be added to functions.php to enable Wordpress json api give featured image source url
function post_featured_image_json( $data, $post, $context ) {
$featured_image_id = $data->data['featured_media']; // get featured image id
$featured_image_url = wp_get_attachment_image_src( $featured_image_id, 'origi>
if( $featured_image_url ) {
$data->data['featured_image_url'] = $featured_image_url[0];
}
return $data;
}
add_filter( 'rest_prepare_post', 'post_featured_image_json', 10, 3 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment