How to get the Featured Image URL in WP-API V2

Published:

Formerly as seen below you could use a function to adjust the end point so it would contain a new field for the ‘featured-image‘ url.  The Rest-API v2 has now been updated so you can add ‘?_embed‘ to the parameters in your query and you’ll get exactly what you need.

or

You can use the following to just add the URL itself to your feed and skip over the rest of the data you may not need

function prepare_rest_fi($data, $post, $request){
    $_data = $data->data;

	$thumbnail_id = get_post_thumbnail_id( $post->ID );
    $thumbnailFull = wp_get_attachment_image_src( $thumbnail_id, 'full' );

    $_data['fi_full'] = $thumbnailFull[0];
    $data->data = $_data;

    return $data;
}
add_filter('rest_prepare_<post-type>', 'prepare_rest_fi', 10, 3);

 

////////BELOW IS DEPRECATED BUT STILL GREAT FOR REFERENCE//////

In V2 of of the WP-API the featured_image currently returns the ID, and refrences the exactly slug for the ID and its endpoint in _links

To add the featured image URL directly back to the response for that post/page/type you’ll want to drop the following into your functions.php inside your theme. (this is the from the plugin : ‘Better REST API Featured Images‘). Now you can just use this as opposed to adding another plugin to your website:

 

add_action( 'rest_api_init', 'better_rest_api_featured_images_init', 12 );

function better_rest_api_featured_images_init() {

  $post_types = get_post_types( array( 'public' => true ), 'objects' );

  foreach ( $post_types as $post_type ) {

    $post_type_name     = $post_type->name;
    $show_in_rest       = ( isset( $post_type->show_in_rest ) && $post_type->show_in_rest ) ? true : false;
    $supports_thumbnail = post_type_supports( $post_type_name, 'thumbnail' );

    // Only proceed if the post type is set to be accessible over the REST API
    // and supports featured images.
    if ( $show_in_rest && $supports_thumbnail ) {

      // Compatibility with the REST API v2 beta 9+
      if ( function_exists( 'register_rest_field' ) ) {
        register_rest_field( $post_type_name,
          'better_featured_image',
          array(
            'get_callback' => 'better_rest_api_featured_images_get_field',
            'schema'       => null,
          )
        );
      } elseif ( function_exists( 'register_api_field' ) ) {
        register_rest_field( $post_type_name,
          'better_featured_image',
          array(
            'get_callback' => 'better_rest_api_featured_images_get_field',
            'schema'       => null,
          )
        );
      }
    }
  }
}

/**
 * Return the better_featured_image field.
 *
 */
function better_rest_api_featured_images_get_field( $object, $field_name, $request ) {

  // Only proceed if the post has a featured image.
  if ( $object['featured_image'] ) {
    $image_id = (int)$object['featured_image'];
  } else {
    return null;
  }

  $image = get_post( $image_id );

  if ( ! $image ) {
    return null;
  }

  // This is taken from WP_REST_Attachments_Controller::prepare_item_for_response().
  $featured_image['id']            = $image_id;
  $featured_image['alt_text']      = get_post_meta( $image_id, '_wp_attachment_image_alt', true );
  $featured_image['caption']       = $image->post_excerpt;
  $featured_image['description']   = $image->post_content;
  $featured_image['media_type']    = wp_attachment_is_image( $image_id ) ? 'image' : 'file';
  $featured_image['media_details'] = wp_get_attachment_metadata( $image_id );
  $featured_image['post']          = ! empty( $image->post_parent ) ? (int) $image->post_parent : null;
  $featured_image['source_url']    = wp_get_attachment_url( $image_id );

  if ( empty( $featured_image['media_details'] ) ) {
    $featured_image['media_details'] = new stdClass;
  } elseif ( ! empty( $featured_image['media_details']['sizes'] ) ) {
    $img_url_basename = wp_basename( $featured_image['source_url'] );
    foreach ( $featured_image['media_details']['sizes'] as $size => &$size_data ) {
      $image_src = wp_get_attachment_image_src( $image_id, $size );
      if ( ! $image_src ) {
        continue;
      }
      $size_data['source_url'] = $image_src[0];
    }
  } else {
    $featured_image['media_details']['sizes'] = new stdClass;
  }

  return $featured_image;
}