4

I want to show recent posts in my Android application and I'm suing 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 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?

  • 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 Mar 27 '19 at 14:00
4

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>

  • 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. – Amar Ilindra Jul 1 '17 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 Apr 11 '18 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...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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