Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /** add featured img url to wp rest api **
- * Created 11.15.18
- * to test, add this code to functions.php under the current wp theme "../wp-content/themes/themeName/functions.php"
- * then check out the api: "../wp-json/wp/v2/posts" and inspect the json,
- * under post data object, you should see a new key: 'ftd_img_sm', with value: 'URL of image'
- **/
- function prepare_rest($data, $post, $request){
- $_data = $data ->data;
- $thumbnail_id = get_post_thumbnail_id( $post->ID );
- $thumbnail_sm = wp_get_attachment_image_src( $thumbnail_id, 'small' ); #get img url
- $_data['ftd_img_sm'] = $thumbnail_sm[0]; #name of new data attribute
- $data->data = $_data; #add url to api data
- return $data;
- }
- add_filter('rest_prepare_post', 'prepare_rest', 10, 3);
Add Comment
Please, Sign In to add comment