Skip to content

Instantly share code, notes, and snippets.

@lenivene
Last active October 20, 2021 17:31
Show Gist options
  • Save lenivene/c170cd677014f72fb4433d5293953052 to your computer and use it in GitHub Desktop.
Save lenivene/c170cd677014f72fb4433d5293953052 to your computer and use it in GitHub Desktop.
Add key `thumbnail_url` in WordPress REST API
<?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