How to add extra fields to the WordPress Rest Api
While working on a different article about using the WordPress Rest Api to show post on another WordPress site I ran into the problem that the default Rest Api endpoints didn’t include al the data that I needed.
Lucky for me/us the Rest Api is very extendable. Meaning we can add all the additional fields to the endpoints we need. And in this article I’m gonna show you how we can do so.
Adding a Acf field to the WordPress Rest Api
As a first example I want to show you how we can add an Acf field to the /post endpoint. You could use a plugin for this, but I personally dislike using generic plugins for things that can be solved with a couple of lines of code.
In de below example we will add a “related posts” Acf field to the /posts endpoint.
The first step is to register the field with the Rest Api. For this task we create a new function called register_related_posts_api_field. Within this function we use the register_rest_field function to register the field. The register_rest_field function accepts the following parameters:
- $object_type: The type of WordPress objects the field must be registered for. In our case we want to add a field to the /posts endpoint so we pass post as the object type. (You could also pass an array of objects)
- $attribute: A string representing the name we want our Json key to have. In this example we want to add a “related posts” field so we pass related-posts.
- $args[‘get_callback‘]: A callback function that will be responsible for retrieving the data for this field.
- $args[‘update_callback‘]: A callback function that will be responsible for updating the data of this field. We don’t need it in this example.
- $args[‘schema‘]: The callback function used to create the schema for this field. In this example we don’t need any so we pass null.
With this code we registerd our field we now need to create the get_related_posts_api_field function that will retrieve the data from the Acf field..
Creating the callback function
Above we create the get_related_posts_api_field function that accepts one argument called $post. Inside this function we use the Acf function get_field to get the value by passing related_posts as the Acf field name and we get the id from the $post parameter to pass as the second parameter.
If we did everything correctly we could now test things out by pointing our browser to yourdomain.com/wp-json/wp/v2/posts/ and get a result like shown in the image below
Add featured image to the WordPress Rest Api.
Another use case is to add the featured image to the Rest Api. You could get the featured image from the Rest Api by default but this is a pretty cumbersome process. An easier way is to add it as a custom field like shown below.
The code above is almost identical as the previous example. We changed the function name to register_featured_image_api_field, the fieldname to featured-image and the callback function name to get_featured_image_api_field. Lastly we updated the callback function name inside the add_action function.
Now we need to create the get_featured_image_api_field callback function.
Inside our new get_featured_image_api_field callback function we now use the get_the_post_thumbnail_url function and pass it the post id and any valid registered thumbnail size to get the full Url to the post’s featured image.
Testing our yourdomain.com/wp-json/wp/v2/posts/ endoint again we should now have a featured-image key/value pair like shown in the image below.
IIf you want to leave a comment, please do so under the copy of this article on Dev.to so i can get back to you.
Thanks for reading