Guest User

Untitled

a guest
Nov 16th, 2018
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.77 KB | None | 0 0
  1. /** add featured img url to wp rest api **
  2. * Created 11.15.18
  3. * to test, add this code to functions.php under the current wp theme "../wp-content/themes/themeName/functions.php"
  4. * then check out the api: "../wp-json/wp/v2/posts" and inspect the json,
  5. * under post data object, you should see a new key: 'ftd_img_sm', with value: 'URL of image'
  6. **/
  7.  
  8. function prepare_rest($data, $post, $request){
  9. $_data = $data ->data;
  10.  
  11. $thumbnail_id = get_post_thumbnail_id( $post->ID );
  12. $thumbnail_sm = wp_get_attachment_image_src( $thumbnail_id, 'small' ); #get img url
  13.  
  14. $_data['ftd_img_sm'] = $thumbnail_sm[0]; #name of new data attribute
  15. $data->data = $_data; #add url to api data
  16.  
  17. return $data;
  18.  
  19. }
  20. add_filter('rest_prepare_post', 'prepare_rest', 10, 3);
Add Comment
Please, Sign In to add comment