Last active
September 14, 2022 21:02
-
-
Save yoren/c8c98dc7c701bb87a844 to your computer and use it in GitHub Desktop.
Add Featured Image Thumbnail URL to wp-json/wp/v2/posts Endpoint
This file contains 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 | |
function my_rest_prepare_post( $data, $post, $request ) { | |
$_data = $data->data; | |
$thumbnail_id = get_post_thumbnail_id( $post->ID ); | |
$thumbnail = wp_get_attachment_image_src( $thumbnail_id ); | |
$_data['featured_image_thumbnail_url'] = $thumbnail[0]; | |
$data->data = $_data; | |
return $data; | |
} | |
add_filter( 'rest_prepare_post', 'my_rest_prepare_post', 10, 3 ); |
This file contains 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 | |
function my_rest_prepare_post( $data, $post, $request ) { | |
$_data = $data->data; | |
$thumbnail_id = get_post_thumbnail_id( $post->ID ); | |
$thumbnail = wp_get_attachment_image_src( $thumbnail_id ); | |
$_data['featured_image_thumbnail_url'] = $thumbnail[0]; | |
unset($_data['featured_image']); | |
$data->data = $_data; | |
return $data; | |
} | |
add_filter( 'rest_prepare_post', 'my_rest_prepare_post', 10, 3 ); |
This file contains 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 | |
function my_rest_prepare_post( $data, $post, $request ) { | |
$_data = $data->data; | |
$thumbnail_id = get_post_thumbnail_id( $post->ID ); | |
$thumbnail = wp_get_attachment_image_src( $thumbnail_id ); | |
$_data['featured_image'] = $thumbnail[0]; | |
$data->data = $_data; | |
return $data; | |
} | |
add_filter( 'rest_prepare_post', 'my_rest_prepare_post', 10, 3 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment