 Welcome back to the channel. Today, I'm going to show you something that's a little bit different. In most cases when you're working with APIs, there is something that's called a callback function. WordPress has not had a very, very clear way or we've not thought about it in the traditional way of how to use things like webbooks. So you've had different plugins to come in but I've come to a place where I've realized you know what, you can actually just use the REST API to handle your callbacks and do a number of things just inside WordPress itself. So that's the beauty of it. So here what we are looking at is I'm looking at the REST API on my particular domain. So I have my domain and I've gone to the slash wp-json and that's where all our namespaces, that's where all our endpoints are registered. If you want to know more about the REST API, get more descriptions about it, see how to do different things with it. You can see the videos that I've linked in the descriptions below. You can see here that we have a namespace and we have a number of namespaces. This wc version 1, 2 and 3 what you see is from WooCommerce. And then we have the wp version 2 which of course is from WordPress itself. So just like WooCommerce, we are able to create our own namespaces and do a number of things Remember the REST API can get information as a method and can post information as a method as well. So what we're going to do with our REST API this time is we're going to post content from another website into our own website and we shall store that into custom post types and see how we can use that information for other things. For example, the callback we've talked about, we can use the callback like in this instance, we are going to record payments that have been done, were they successful, were they done nicely. So we're going to get that information from somewhere else and then just post them and save them in our custom post type and we can use them to do anything else in that regard. So let's jump into the code. We're going to go inside my site on the manager, we're going to create a new folder in our plugin section and we shall call this a take a press callback URL functions. And of course after creating the folder we're going to create a new file inside it. So create the folder first, go inside the function and then we're going to create a new file that we shall give the same name and just add a PHP extension to it. So once we're done with that we're going to start editing it to add our code. So the first thing we'll do is of course open up our PHP tags in the file and we're going to use an action hook and this is for the REST API. So the action hook that we're going to use is called REST API init. So when the REST API is initialized basically what we need to do is get this function or method inside to work. So we shall call it a take a press add callback URL endpoint. So that's what we're going to use as our function. So I will copy this and then come to function, add the method and then we are going to start adding what we need there. So inside the action hook that we've used we have a method registered in there and it's called a register REST root. I don't know how you say that word whether you say route or root that's entirely up to you. So inside this method it expects three expectations. It expects three parameters. The first parameter is what we are going to have as the namespace. So we'll have take a press as our namespace and we shall have this as version one because later in the lifespan of our software we might decide that we actually want to have a version two. So in order to advance our software we don't have to remove version one. We can still leave it and then advance to version two. So you're planning in the future. The next thing that it requires is going to be, it's going to require the endpoint. So the endpoint we shall call it receive callback. So this will be our endpoint. Let me just comment this so that you can be able to follow through. That's the endpoint and this is the namespace. Now the next argument that it requires, of course I don't need to forget the comma here. The next argument that it requires is an array of arguments. So inside this array is where we're going to register the method of the endpoint. For example, here the method is going to be we use methods which is required. The method will be post. Of course you have the option of having get, put, delete and so on. But in our case we're going to be receiving information from another API and we want to save it or use it in our own system. So we use the post. So we'll have our methods there. The next thing that we're going to record is a callback. So we need to also have a callback that will be used by our endpoint. So the callback we shall give it a name and we are going, I'm just going to use part of this. I'll call this take a press receive callback. This is what we'll use to add our new functionality. So I'll add a function with this name and I will pass in some data. So what I'm going to do here is I'm going to return data. Now, if I return that data, I need to define it and I'll say my data is going to put an array. So I'll wrap this as an empty array. I'll wrap it as an array so that I can always chain on different things all together. So the first thing that I'm going to chain on is a message to it. So I'll say message. The message is going to equal to because the semicolon you have reached a server. And that will be our message to it. And we can also add another thing altogether and I would choose to say, let this be the status. And we shall say the status is going to be OK. So with this in place, we're able to save this. Now let's go on our endpoint and see whether this is actually active. So I'm going to go for HTTP. HTTP JSON. So we'll just go to our WP JSON. And when we come to the namespaces, we're going to see that we have our name space. Take your press version one. And if we go inside the roots or routes, depending what you say, we have our receive callback as a root here. And we can go and see that the methods we have is a post. And we have only one endpoint that receives posts. So we can see the links of that endpoint as this. So when I click this, you'll see that it says this does not exist. And this is how WordPress reacts. It's supposed to protect you. You're supposed to be posting information. You've not posted anything. So it automatically protects you. So we're going to go back to our code and we're going to write some. We're going to write some codes to help us fix our issue. So I'm going to pass in an object which I'm going to call request data. It can be any name here. And this is basically using the class of the REST API class. So the class that we are looking at is actually the WP REST class here that has a number of methods that are changed to it. For example, you can get a method. You can set the method. You can set parameters. You can get the parameters. And those are some of the methods we're going to use to capture our details. So back in our code, we've passed in this object. This object is of this class, the WP REST request. And we're going to say the REST that object is going to equal to parameters. Now these parameters is going to get params as the method that we are going to chain onto this. So sorry, I replaced this. This is supposed to be here. So we're going to say our parameters that we're getting from our method is we're going to get the params that are passed on from the request data object. So what kind of parameters can we get? Let's say, for example, we're expecting the parameters of, let's say, you're going to post your name. So we'll expect the name to be passed in. So we're going to say parameters name. So we're going to have a variable of name that we'll expect to get from the parameters, a parameter which is passed in as name. So I'm going to copy this and I'm going to add another object inside our data. And I'm going to say, I'm going to call this a received data. So received data is going to have an array. And inside this array, we're going to pass in the name. So we have the name passed inside here. And the name that we shall have passed in is going to be equal to name, for example, here. So we can get a number of other things. Let's say even password. So we'll get password here. Copy this. And we're going to just duplicate this and pass in password. And then we'll pass this in also as password. So when we save, when we make a posting to this API to this endpoint, we're going to get those parameters stored up inside small variables like this. And then we're going to pass in a data status of OK. We're going to receive this information and we are going to pass in also this message. So let me save this and then I'll open up my postman to help me to post that information. So can make some conditioners to return a particular message. For example, if I want to make sure that someone has passed in the name and the password, I can do an if statement here and do an if statement and say if the name and password are true. So if name is true. So in what place we write in the reverse. If this is true and true. The password is also true. So we'll copy this. If both of them are true or if they are not empty or if is set. That is another way you can say it. If is set and if the password is also set. Close this properly. If both of them are set then we are ready to say everything is OK status is OK. The message is OK. We'll cut this and paste it here. So let me just stop this to make it data readable. Otherwise, if everything is not OK, we can pass in a message and we're going to pass it on to our data. And we just say the data status is failed. So it's failed and we shall pass in a message and say parameters. Sorry, this is the message still. We'll say parameters missing. Aram, it has missing. So I'm going to save this. I'll just remove this extra space and save this. So inside our function, we'll pass in a name, pass in a password. That's what we're expecting. And then when both of them are set, we shall have a proper welcome message. And if they are not passed, then we'll have an error. I'll save this and I have a type of here. I think my editor for letting me know. So we'll save this. So when I reload, we still have this issue here. But if I come to my postman and use this same callback here, as you see in the browser, I'm going to go to the body and I'm going to pass in a couple of things. So I'm going to pass in a name and that name will be Lawrence, for example. And then I'm going to pass in a password. This should be in double quotes because this is JSON. So we're going to have a password. And that password will be it's me, 2020, let's say. So we have our JSON right up there. So we're going to ping this on our servers. I'll send this and you're going to see we still get an error of this showing up. So let's go back and check what's happening. So if I go to our postman and make sure that the endpoint is right and hit send, passing in our name and our password, you're actually going to see that we get back a status of OK. Later received, we have the name and password. So meaning you can pass back the same information. And then we'll have a message from the server telling us so. But imagine we didn't pass in the user, the name and the password. And then I just hit send. What will we get back? We get back a status of failed and we get back a message of parameters is missing. So with our API, we are able to receive information from another API and still give back a proper message and also just utilize the information the way we want it. So in the next video, we are going to actually just save the data that we get from the API into a custom post type. We can also manipulate the same data to do a couple of functions. So for example, in the WooCommerce payment gateway, we could have set the callback endpoint given it to the other server and then choose to receive information. Whenever information comes back from that callback, it triggers a particular action inside our WooCommerce order payment so that that particular order is actually cleared. For example, or we can use the letter in whatever we choose to please. So if you want to see how we do this, please subscribe to the channel. Give the video a like if you found it impressive. And let me know in the comment section whether you have a question or you want to say something. Just leave it in the comment section. Don't forget to see the other video series in the chat. Don't forget to see the other videos about the rest API in the video description below. So enjoy your day.