# Featured Image URL in the REST API

> Adds a featured_image_url field to posts and pages in REST API responses, so headless front ends do not need a second request.

- Type: PHP
- Runs: Run everywhere
- Category: [Developer](https://wpide.com/library/category/developer/)
- Tags: developer, rest-api, headless, featured-image
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/featured-image-url-in-rest/

## Code

```php
add_action( 'rest_api_init', function () {
    register_rest_field( array( 'post', 'page' ), 'featured_image_url', array(
        'get_callback' => function ( $object ) {
            $id = isset( $object['featured_media'] ) ? (int) $object['featured_media'] : 0;

            return $id ? wp_get_attachment_image_url( $id, 'large' ) : null;
        },
        'schema'       => array(
            'description' => 'URL of the featured image in the large size.',
            'type'        => array( 'string', 'null' ),
            'context'     => array( 'view', 'edit', 'embed' ),
            'readonly'    => true,
        ),
    ) );
} );
```

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Featured Image URL in the REST API" in its snippet library and add it in one click; or create a new PHP snippet in WPIDE (or any code snippets plugin) and paste the code above. More snippets: https://wpide.com/library/
