As we saw in Create and Explore a Single WordPress Post Through Gutenberg and the REST API, there is a lot of data available to you. This data might not be configured exactly how you'd like it. For instance, maybe you want comments to load with the initial call to
/posts/#ID without having to make additional calls for Author and Comments.
In this lesson, you'll see how to extend a behavior by adding an action in the
functions.php file that lives in
/wp-content/themes/twentytwenty. We will use
register_rest_field to add Comments to the request for a post.
Instructor: [0:00] When I do a GET request for version2 post social number eight and get a lot of information about my post, ID, there are lots of stuff that about to render, the content, the excerpt, lots of things. Our author and our replies, to be able to get that information, I need to carry out a second query.
[0:18] WordPress lets us extend the API infinitely. Let's add in the comment details to this call. When local instigate is not for me, it puts it in local hosts headless Egghead. I'm going to go into app/public/wp-content. Everything else, wp-admin, WP includes all these files should rarely be touched. All changes we make to our WordPress instance, it happened in wp-content.
[0:41] We can update WordPress without having any issues with overwriting changes that we've made. In WP content specifically, we're looking at our themes. Now, there are three themes that come by default with our install, so let's check which one has been installed. I'm going to go to appearance and themes, and we see the active theme is 2020. I don't want these other themes, so I'm going to delete those directories.
[1:16] What we are going to be looking at is the functions.php file. This is the functions definitions for our theme. We're going to go to the bottom of this file. We're going to add in any custom content that we want.
[1:28] I'm going to add an action. This action is going to take place when the REST API is initialized. When that happens, I want this callback function to be executed. I want to use a reds to REST fields and it takes three arguments, type I want to associate this field to, post, the name of this field, comments, and an array that contains any of the callback functions and the schema.
[1:50] I only want this field to be added when we get on the API. I'm going to add, get callback. That get callback is going to be a function that's going to take the post array. I'm going to create an array of arguments. It just has one argument, it's a post ID, which I'm getting from the post array and specifically from its ID field.
[2:10] With that, I'm going to go to comments. I'm going to use the get comments functions and pass it my arguments array. I'm going to return those comments, type them as an array and pass them back as well as my callback. I'm going to pass the scheme, and that's an array. I'm going to have the description, the comments, and I'm going to have the type array.
[2:32] Now, when I call to my API, I scroll down. I can see I now have a comments field. In that comments field I have all the comments that are attached to that post. To confirm that, I'll view the post and I'll add another comment. Now there are two comments, "This is an amazing post."
[2:46] Another comment, now I check I've got a comment. I've got one comment, and two comments. I have that two comments appeared. I've extended my POST REST API to include the comments in that single call.