Join Morten Rand-Hendriksen for an in-depth discussion in this video Alter the API response, part 2, part of WordPress: REST API.
- [Instructor] Adding new fields to the REST API response is done using the the function register REST field. The problem is the code reference for the register REST field function in the WordPress dot org developer handbook is really thin, so a better place to find an example is to go to v2 dot wp dash api dot org. Now this is the documentation for the REST API when it was still in a plugin format, but some of these examples are still valid. And here you see an example of how this is done.
You add an action to the REST API init hook, this is when the REST API is initialized, and then you create a function that you call back into that hook. In this example, the function is called slug register starship and here we see the function register rest field. The function first takes the object type you want to add a field to, in this case post, then it takes the name of the new field, so this is what would actually display as the key in the REST API output, and then it takes an array of options.
Get callback, so this is the callback where you would send in the new information, Update callback, if you want to push information to a field, and schema if you want to pass a certain schema. So in this example, slug gets starship, grabs the information about the current object, and then just returns it to the function. We're going to do something very similar to this using this code example. In the exercise files for this movie, 0305, you'll find the 2017 child theme folder with one new file called functions dot php.
And inside functions dot php, I've built out three new functions based on what we did in the previous movie. Here you see tschild get previous post ID grabs get previous post and ID and returns it tschild get previous post title grabs get previous post, and then the post title and finally, tschild get previous post link grabs the get previous post ID and passes it through get permalink to get the permalink to the post. Now we want to add this information to our REST API.
To do so, I'm going to copy out the code here and paste it in at the top of functions dot php. Now we need to make a couple of changes to this and we'll run through it again. So first, we create a new action on the REST API init hook and here we want to point at a new function so I'm not going to call it register starship, I'm going to call this one tschild register fields. This callback is the function directly below. So here we have tschild register fields. Then we need to set up the name of the field we want to register so let's start with the post ID.
The new field will just be called previous post ID. Then the callback within the function points at the function that returns the previous post ID. We'll put that here and leave everything else the same. Then I'll save functions dot php, go to Postman and call a single post. Now when I scroll down in the return here, you'll see there's a new field. Previous post ID 3526, which is the ID for the previous post. Now we can do the same with these two other functions.
So I'll just copy register rest field, paste it in, change it from previous post ID to previous post title and then change the callback to title as well. This is the second function down here. Save that. Go and make a new request to the API, scroll down, now we have previous post ID and previous post title. And finally we'll do it one more time for previous post link. What I hope you see here is that adding new fields to the REST API response is really straight forward and because of the structure of this function, register rest field, you can pretty much callback any information you want into those fields.
That means you can grab any information from WordPress in your custom function and return it into the callback. And if you look at the example provided on the REST API documentation, you can also grab information from the current objects, so here you see we're actually grabbing the object, the field name, and the request. So this is the object, the field name, and the request and returning it back in. So you can create really advanced fields that interact with WordPress to produce the data you want. And remember, you can append these new fields to any object within the API.
Right now we've been working with posts, but you can append it to anything else in the API. So anything available through the API can be changed using register rest field. One final thing to note, here I've demonstrated this using a single post, but if I now jump back a level, so you can see all my posts and scroll down, you'll see that these new fields also apply to the posts being indexed in the post index. So when you make a change to an object, you're also making that change when that object is displayed in a different context, for instance in an archive.
- Using a REST client to communicate with a REST API
- Reviewing available routes, endpoints, and arguments
- Adding custom post types and taxonomies
- Altering the API response
- Using PHP for resiliency
- Passing info using wp_localize_script()
- Setting up a REST API request through AJAX
- Outputting selected post data to the front end
- Formatting post data to match a theme
- Triggering AJAX for an infinite scroll effect
- Using AJAX to load subsequent responses