granit nebiu logo

How to Add Author and Featured Image to WordPress REST API Response

cmsgranit

Author:

cmsgranit

How to Add Author and Featured Image to WordPress REST API Response

Enhancing the WordPress REST API: Adding Author and Featured Image to Post Responses.

Are you working with the WordPress REST API and need to include the author and featured image of your posts in the response? By default, the WordPress REST API doesn’t include the author or featured image fields in the post response. But don’t worry, with a few lines of code, you can easily add these fields to the API response.

First, add the following code to your site’s functions.php file:

// Register the author_avatar, author_name, and featured_media_url fields for the post type
function add_post_fields() {
    register_rest_field( 'post', 'author_avatar', array(
        'get_callback'    => 'get_post_author_avatar',
        'update_callback' => null,
        'schema'          => null,
    ) );
    register_rest_field( 'post', 'author_name', array(
        'get_callback'    => 'get_post_author_name',
        'update_callback' => null,
        'schema'          => null,
    ) );
    register_rest_field( 'post', 'featured_media_url', array(
        'get_callback'    => 'get_post_featured_media_url',
        'update_callback' => null,
        'schema'          => null,
    ) );
}

// Define the get_callback function to retrieve the author avatar URL
function get_post_author_avatar( $object ) {
    $author_id = $object['author'];
    $avatar_url = get_avatar_url( $author_id );
    return $avatar_url;
}

// Define the get_callback function to retrieve the author name
function get_post_author_name( $object ) {
    $author_id = $object['author'];
    $author = get_userdata( $author_id );
    $author_name = $author->display_name;
    return $author_name;
}

// Define the get_callback function to retrieve the featured media URL
function get_post_featured_media_url( $object ) {
    $featured_media_id = $object['featured_media'];
    $featured_media_url = '';
    if ( $featured_media_id ) {
        $featured_media = wp_get_attachment_image_src( $featured_media_id, 'full' );
        $featured_media_url = $featured_media[0];
    }
    return $featured_media_url;
}

add_action( 'rest_api_init', 'add_post_fields' );
PHP

PHP

This code adds three fields to the posts REST API response: author_avatarauthor_name, and featured_media_url.

Now, when you make a GET request to the posts REST API endpoint, the response will include these fields. For example, if you make a request to https://example.com/wp-json/wp/v2/posts, you will see a response like this for each post:

{
    "id": 1,
    "date": "2022-02-16T10:54:13",
    "title": {
        "rendered": "Hello world!"
    },
    "author_avatar": "https://secure.gravatar.com/avatar/8d9e9c12e1e0f38b7758d546df33bc27?s=96&d=mm&r=g",
    "author_name": "John Doe",
    "featured_media_url": "https://example.com/wp-content/uploads/2022/02/featured-image.jpg"
    // other post fields...
}
JSON

That’s it! With just a few lines of code, you can easily add the author and featured image fields to your WordPress REST API response.

You might also enjoy

ECMAScript 2025: New Features That Make JavaScript More Powerful

ECMAScript 2025: New Features ...

July 2025 – ECMAScript 2025 has officially become a stand...

The Best Headless CMS Solutions for WordPress in 2025

The Best Headless CMS Solution...

Selecting a headless CMS for WordPress isn’t just about d...

Baseline digest: modern web features you can rely on

Baseline digest: modern web fe...

April 2025 was a busy month for the Baseline project. In ...

Comments (0)

No comments yet!

Speak up and share your thoughts! Your comments are valuable to us and help us improve.writeComment