 Welcome back to the channel. In today's video, I'm going to show you how you can extend the REST API and that is basically getting the endpoints that are currently existing, see the information that's available, and then if you see that something is missing that you want to have from the endpoint, you add it using WordPress. If this is something that you're interested in seeing or doing, then let's jump into the video together. There's so much more that you can learn by looking through the video description and consider subscribing to the channel if you haven't done that before. Otherwise, leave me a comment about how you plan to use this or leave me a question in the comment section. But don't forget to like the video if at the end you enjoy it. Otherwise, let's dive into what we're going to learn today. So when you come to your WordPress website and you've added all your content and all of this, it's very hard to imagine that it actually has an API or a REST API that you can extend. Now, WordPress has developed that so that it can be able to do a number of things. For example, using things like Gutenberg. That is the new editor that is in WordPress currently. Now, if you go to your domain, assuming this was your domain and you do slash wp-json, you are able to see all this data that's available in here. Now, you have a number of endpoints on routes, depending on your school of pronunciation. We have a number of routes or routes that are available on the REST API. Now, if I take the posts route, for example, it has a number of things like the endpoints and the links that get you there. So I'm going to go to the link and when I'll click on the endpoint for the post, you'll see that you get back 10 items in this JSON pack. And if I collapse one, you'll be able to see that you have the ID, you have the dates, you have the dates of modification, you have things like the title, you have the content, you have an excerpt, you have a featured image that is zero. This is telling us that there is no featured image on that post. You have different information available for you on the endpoint and there is so much that is also missing. This is usually used by people who use WordPress as a decoupled application. And that's basically using the administrative side of WordPress to make it easy for people to edit and add new content to the particular posts and then serve that to a single page application that's using something like JavaScript, using Vue, React or whatever other platform you want to do this. It could also be a mobile app at this particular junction. So what I'm going to do is use a plugin on our side. And after setting up that plugin, we shall be able to extend the post endpoint right here to add a couple of things. For example, I don't want to go levels deep just to find the attached image or the featured image. I would also like to add the comments to the individual posts so that I don't have to go and look for them on the comments API. I want to be able to use one endpoint and then export all that information. Now, that's the beauty of using something like GraphQL. There is a plugin called WP GraphQL. Now, that's the beauty of using something like WP. Now, that's the beauty of using GraphQL. There's a plugin in WordPress called WP GraphQL. And I have a couple of videos on this channel that show you that and it allows for you to query the different information without having to go through all this trouble. All right, so let's jump into the code. I'll open up my editor and then I'm going to go inside my plugins folder, which is right inside my root folder. And then I'll open up this. I'll add this directory, which I am calling extend rest API endpoints. And right now it's empty. I'll copy this, drag and drop it in my editor. And then I'll start off the plugin by using the same name and adding the PHP to the end of the line. Now that we have this here, I'll open up by writing PHP. I'll use my start plugin snippet and it will allow me to add this really quickly. So I'll add a name extend the rest API posts. And then I'll do all the other things that need to be done quickly. I'll add on my GitHub. And then, of course, I'll add in my name. And then I'll just say this is what this plugin does. This plugin extends the rest API posts. So I'll change the license to JPL2 or later. Then I'll just use this extension right here. Copy this, paste this here, and then I'll change the text domain to this. Now this code will be available for you to download later. Or if you want to get it from GitHub on this very link that you have right here. Now, first things first, I'll add some basic security. And I'll use the defined. I'll try to find out is the app's path variable already defined. And if it's not defined, I'll just say die. And of course, I'll pass in a message. I'll say unauthorized access as the message. And then now we are ready to go. The first thing that I'm going to do is come back to my plugins in here, reload this and then activate the extend the rest API posts plugin right here. I'll move this here. Now, when we go back to our endpoint, of course, we have nothing showing up here that's changed. But for now, I'm going to just add a very little key like what we have here for the title or the link and so on. So what I'm going to do is first come here, look for the add action. And in add action, it needs the tag. The tag that we're going to use here is called rest API in it. So when the rest API is initialized, I'll need to remove this here. I am going to just add a function and say the function will be take a press add posts fields. And that's what I'm going to do. The priority will not matter. And the arguments, I don't need to add any other. So I'm going to copy this function here, come down here. And then I'll open up this function so that nothing is broken at the end of the day. So to add a field, I would need to use the register rest field. And that register rest field has three arguments that it expects. Number one, it expects the name of the post type or the post object. So we're going to be looking for the post. I'll add a comma. The next thing we need to do is add the name of what we want to add as the information. So in short, I'll call it featured image URL, just for short. And then lastly, we need to add an array. And this array has optional items in it. For example, we can add a get callback. So get callback, that's going to be the name of the function that we're going to use to query for our featured image. So I'll call that get rest featured image. And that's the name of the function. I'll copy this and then just open, add it here. And then we'll be able to move on ahead. The next thing that I'm going to do, of course, is add a comma. And here we can also duplicate this to have the update callback, which is needed. And in this case, we're just going to add now because we don't have anything to add. And then finally, we can add a schema, which I'm just going to leave as now because we don't have any of that kind. I'll add a semicolon right here to this so that our function closes out. Save this. And then what I'm going to do here is actually just a for something back. So I'm just going to return something very simple. And I'll use techie press. So we'll save this. Let's go back to our endpoint. You can notice right here, we don't have anything like techie press or featured image URL, I'll reload this. And then you're going to see now we have something called FIMG underscore URL, and it has techie press. And if we go down to this object as well, you're going to see we have the same thing over and over. Now, what we need to do is provide for dynamic content to appear here. And that's what I'm going to do. So go back into our code right here. We're going to return an image. And so I need to describe what this image is going to equal to. I know I made a little typo here. I need to change this to F so that it can be easily be readable. So for our image, what we're going to do is we're going to use the WP get attachment image source. The WP get attachment image source has three. This is of information it will require from us. It requires the attachment ID in this case. And this is available for us in this very function. So in here, we have what we call the object. I made a typo. We have what we call the object in here. We have what we call the field name. Available. And we also have the request given to us. You can look up all of this on the WordPress developer guide. And that is by just going into this link, register rest field in here. And you'll see that you have the object available and you have all this information for you. There are some examples that you can always use even at the bottom right here. So what I'm going to do is just look for the object ID and which is that's going to be practically image ID. So I'm going to change this right here. And then I'm going to look for the featured media. So in the object that we have here, we're going to look for the featured media. And then I'm just going to look for size that's available. I could choose the medium or thumbnail or full image if I want to. And that will be available for me. The rest is optional. That's why I'm removing it. Now after getting this IMG, let me just serve this. And then you'll see what will be available on the end. I'll go back to our endpoint reload. If I open one of these, you're going to look at this and it's going to say false. And that is because right here we have an array of information that's coming out. And that is all the information about this particular image. The size, the name, the URL and what we need to do, we need to go for the first item inside this array object. So when I save that, come back here, reload this, open up zero. You're going to see that we have null right here. If I open up any of these others, we have null. All these do not have a particular URL. So I'm going to go to this item, which is titled block image. So I'll go back inside my posts. I'll go to block image right here. And then what I'm going to do is come to the featured image, select this, and I'll add this little image in there. I'll update this after updating. I'll go back to the endpoint. I'll reload this. When we look inside array zero, we'll see that we have null showing up here. It says null right here. But we see that we have a featured media with a number 137 here. So I need to go back inside my code and I'll look at this and I'll see that I made a typo. This should be media. So I'll save this. I'll come back here, reload here. Look at this object. Now we can see that we actually have a link to an image. When I click on this, you'll be able to see that this is the image that we added inside our post. So we need to just be careful about the typos, but eventually we get all the information that we want. So I've just added the featured image on the endpoint of our REST API. So we are going to add another field so that we can see how to use WordPress again. And I'm going to register a field here. I'll change the name and call it the author. So we want to get the author name. I'll just say author name right here. I'll change this callback that we have. I'll say get REST post author name. And then I'll copy this right here. Then bring it and start up the new function so that we can write up some code. Now in here, the first thing that I'll return is the author. And of course I have to declare this author and say that's an empty string from the start. But what I need to do now is just say the author is actually equal to get the author. And this is a function in WordPress. Our return has a dollar sign, which shouldn't be the case. So I'll correct that. Save this. Let's come back here and reload. And you'll see that we have an author name that is directly attached here. Now we can also choose to find out the author description. So I'll duplicate this function and say let's get the author description. That's going to be the name of our function. I'll come back to register fields, duplicate this, change this function, change this to author description. Of course, cut this out. And then at the end of the day, what we need to do is also say get the author description. I'll save this, come back, reload, look at our endpoint. We have author description and this is empty. Now let's go back inside our users. Go to the users, go to my profile, edit this. And then I'm just going to go to my bio, which is a description. And I'm going to add some lorem if some is not the best description text ever, text ever. Of course, with some typos, which are correct, I'll update my profile, come back here, reload. Go back to that and you will see that we have our author description coming in from here. So you can be able to maximize the power that the REST API is already giving you by adding additional fields to it. So if you've enjoyed this, please don't forget to give it a thumbs up, explore it and find out what other things you can do with the REST API and the available functions that you have in WordPress. Otherwise, enjoy your day.