Last active
October 20, 2021 17:31
-
-
Save lenivene/c170cd677014f72fb4433d5293953052 to your computer and use it in GitHub Desktop.
Add key `thumbnail_url` in WordPress REST 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
<?php | |
add_action('rest_api_init', 'register_rest_images' ); | |
function register_rest_images(){ | |
register_rest_field( array('post'), | |
'thumbnail_url', | |
array( | |
'get_callback' => 'get_rest_featured_image', | |
'update_callback' => null, | |
'schema' => null, | |
) | |
); | |
} | |
function get_rest_featured_image( $object, $field_name, $request ) { | |
if( $object['featured_media'] ){ | |
$img = wp_get_attachment_image_src( $object['featured_media'], 'app-thumb' ); | |
return $img[0]; | |
} | |
return null; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment