# Author Name and Avatar in REST API Posts

> Adds an author_details field with the author's display name, profile link and avatar URL to every post in the REST API, saving apps a second request or a heavy _embed call.

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

## Code

```php
add_action( 'rest_api_init', function () {
    register_rest_field( 'post', 'author_details', array(
        'get_callback' => function ( $object ) {
            $author_id = (int) get_post_field( 'post_author', $object['id'] );
            if ( ! $author_id ) {
                return null;
            }

            return array(
                'id'     => $author_id,
                'name'   => get_the_author_meta( 'display_name', $author_id ),
                'url'    => get_author_posts_url( $author_id ),
                'avatar' => get_avatar_url( $author_id, array( 'size' => 96 ) ),
            );
        },
        'schema'       => array(
            'type'     => array( 'object', 'null' ),
            'context'  => array( 'view', 'embed' ),
            'readonly' => true,
        ),
    ) );
} );
```

## Notes

Only public profile data is exposed; the author's email and login are never included.

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Author Name and Avatar in REST API Posts" 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/
