Skip to content

Instantly share code, notes, and snippets.

@unigazer
Created February 20, 2019 17:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save unigazer/9f1c95c57e8b70f4e862e82ab609aa90 to your computer and use it in GitHub Desktop.
Save unigazer/9f1c95c57e8b70f4e862e82ab609aa90 to your computer and use it in GitHub Desktop.
Get featured image for WordPress RESTful API
// Add to theme functions.php (Tested on WordPress 5.0.3)
<?php
function post_fetured_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, 'original' ); // get url of the original size
if( $featured_image_url ) {
$data->data['featured_image_url'] = $featured_image_url[0];
}
return $data;
}
add_filter( 'rest_prepare_post', 'post_fetured_image_json', 10, 3 );
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment