Get Image URL instead of Attachment Id in Rest API

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?

2 s
2

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>

Leave a Comment