Skip to content

Instantly share code, notes, and snippets.

@petenelson
Created July 9, 2015 22:29
Show Gist options
  • Save petenelson/6e3d8bb43f726e45e97b to your computer and use it in GitHub Desktop.
Save petenelson/6e3d8bb43f726e45e97b to your computer and use it in GitHub Desktop.
WordPress REST API: Add featured image link to posts
<?php
add_filter( 'rest_prepare_post', array( $this, 'add_featured_image_link' ), 10, 3 );
public function add_featured_image_link( $data, $post, $request ) {
if ( has_post_thumbnail( $post->ID ) ) {
$featured_image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'full' );
$data->add_link( 'featured_image', $featured_image[0], array( 'width' => absint( $featured_image[1] ), 'height' => absint( $featured_image[2] ) ) );
}
return $data;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment