Created
February 20, 2019 17:42
-
-
Save unigazer/9f1c95c57e8b70f4e862e82ab609aa90 to your computer and use it in GitHub Desktop.
Get featured image for WordPress RESTful API
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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