8

I want to show recent posts in my Android application and I'm using this end point to get the list of posts https://www.geekdashboard.com/wp-json/wp/v2/posts

How can I get the complete URL of the featured image instead of its id?

"featured_media": 39913,

I don't want to use any plugins and is it possible to it using functions.php?

1
  • you can get featured media url link by adding "_embed" to end of base url that you are using to get post .or you can use "&_embed" if there is other filter for base url
    – saed
    Commented Mar 27, 2019 at 14:00

2 Answers 2

8

You can modifiy REST API responses in themes functions.php like this.

function ws_register_images_field() {
    register_rest_field( 
        'post',
        'images',
        array(
            'get_callback'    => 'ws_get_images_urls',
            'update_callback' => null,
            'schema'          => null,
        )
    );
}

add_action( 'rest_api_init', 'ws_register_images_field' );

function ws_get_images_urls( $object, $field_name, $request ) {
    $medium = wp_get_attachment_image_src( get_post_thumbnail_id( $object->id ), 'medium' );
    $medium_url = $medium['0'];

    $large = wp_get_attachment_image_src( get_post_thumbnail_id( $object->id ), 'large' );
    $large_url = $large['0'];

    return array(
        'medium' => $medium_url,
        'large'  => $large_url,
    );
}

If you can't modify the REST API response, you can request the media info like this curl http://your-site.com/wp-json/wp/v2/media/<id>

2
  • It should be register_rest_field instead of register_api_field. It is removed in v2. After changing it to register_rest_field, your code worked. I edited your answer. Please accept it. Commented Jul 1, 2017 at 13:57
  • New plugin have http://mahditajik.ir/wp-json/wp/v2/media/<id> but it has many extra data that make response slow so how I can customize REST api respond dto?
    – Mahdi
    Commented Apr 11, 2018 at 15:19
0

modifying @ville6000 example, i was able to solve my own problem,

    add_action( 'rest_api_init', 'ws_register_images_field' );
function ws_register_images_field() {
    register_rest_field( 
        'post', //custom post name
        'attachment_url_images', //array name of your choice
        array(
            'get_callback'    => 'ws_get_images_urls',
            'update_callback' => null,
            'schema'          => null,
        )
    );
}


function ws_get_images_urls( $object, $field_name, $request ) {

  $custom_fields = get_post_custom($object['id']);
  $main_image = $custom_fields['your_custom_images_array_name'];
  $image_urls = array();

  foreach ( $main_image as $key => $value ) {
      $imagesID = explode(',' ,$value);
      foreach ($imagesID as $id => $value) {
        $image_urls[ $key ] = wp_get_attachment_url($value);
        //$custom_fields[ $id ] = $image_urls;
      }
  };

     return $image_urls;
   //return count($main_image);
}

Add to themes functions.php Happy Coding...

Not the answer you're looking for? Browse other questions tagged or ask your own question.