Add featured and thumb image to WordPress REST API

Adding featured image to your WordPress website json respond is not something what require plugin.

  • Custom WP

Like all custom modification on WordPress you need just access to functions.php file.

Simple copy and paste this code to your functions.php file, and you will get two type of images in your json respond, medium and large size.

/*
* Register images rest fields function
*/
function customwp_register_images_field() {
    register_rest_field( 
        'post',
        'images',
        array(
            'get_callback'    => 'customwp_get_images_urls', // Callback function to create images to json respond 
            'update_callback' => null,
            'schema'          => null,
        )
    );
}

add_action( 'rest_api_init', 'customwp_register_images_field' );

/*
* Function to create  different image size in REST API call
*/
function customwp_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,
    );
}

Rest API WordPress WordPress Rest API
WordPress Rest API