How to Add Author and Featured Image to WordPress REST API Response
Author:
cmsgranit
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' );
PHPThis code adds three fields to the posts REST API response: author_avatar
, author_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...
}
JSONThat’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
How to Add Author and Featured...
Enhancing the WordPress REST API: Adding Author and Featu...
How to Truncate dangerouslySet...
When working with React, you may encounter situations whe...
Building a Multilingual Next.j...
Polylang is a popular WordPress plugin that allows users ...
Comments (0)
No comments yet!
Speak up and share your thoughts! Your comments are valuable to us and help us improve.writeComment