In most cases when you’re working with WordPress REST API, there is something called a callback function. WordPress doesn’t have a very clear way of how to use things like webhooks, so we’ve had different plugins come in.
I have realised that we 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.
Looking at the REST API on my particular domain, I have my domain going to /wp-json and that’s where all our namespaces, endpoints are registered.
Note: You can get a WordPress REST API to work with.
If you want to know more about the REST API, get more descriptions about it, see how to do different things about it, you can watch the playlist below.
In this tutorial, we shall cover the following;
- Posting content to a WordPress REST API
- Returnning data from a WordPress REST API
Here is a full video tutorial if you prefer watching.
WordPress REST API review
Note that we have a number of namespaces, this was wp version 1, 2 and 3 that you see is from WooCommerce. Then we have the wp version2 which is from WordPress itself.
Just like WooCommerce, we’re 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 the information as a method as well.
Posting content to our WordPress REST API
We’re going to post content from another website into our own website and we shall store that into custom post type. For example, we can use the callback. In this instance, we shall record payments that have been done and know if they were successful or not.
Create the Plugin
Go inside your site and create a new folder in the plugin section. I will call mine techiepress-callback-url-functions.
Create a new file inside it. I will call mine techiepress-callback-url-functions.php and let’s start editing it to add our code.
Open up the php tags and we’re shall use an action hook and this is for the REST API.
Code snippet showing rest_api_init action.
add_action( 'rest_api_init', 'techiepress_add_callback_url_endpoint' );
When we initialize the RESTAPI, we shall get this function or method inside to work. We shall call it techiepress_add_callback_url_endpoint so that’s what we shall use as our function.
Inside the action hook that we have used, there is a method that is registered in there called register_rest_route and it expects three parameters.
The first parameter is the namespace, and I have techiepress as my namespace and we shall have this as version 1 because later in the lifespan of our software we might decide to have version 2.
'techiepress/v1/', // Namespace
The next thing it will require is the endpoint and we shall call it receive-callback
'receive-callback', // Endpoint
The next argument that it requires is an array of arguments. so inside this array is where we are going to register the method o the endpoint. For example here, it will be use-methods which is required. the method will be POST because we’re going to be receiving information from an API and we want to save it or use it in our own system.
the next thing we shall record is a callback that will be used by our endpoint. I have called mine techiepress_receive_callback
array(
'methods' => 'POST',
'callback' => 'techiepress_receive_callback'
)
That is what we need to add to our new functionality. So we shall add a function techiepress_receive_callback() and pass in some data.
Returning data from the WordPress REST API
We’re going to return data from the REST API
Before we return that data, we need to define it. we shall wrap it as an array so that we can always chain on different things altogether.
so the first thing we shall add to it is a message.
$data['message'] = 'You have reached the server';
we can also add another thing like the status which will be OK.
$data['status'] = 'OK';
Save that and go to your endpoint and see whether this is actually active.
When you go to my API you will notice that we have techiepressv1 in the namespaces and if you check inside the routes we have our receive-callback there. You can see that the method we have is a POST.
And we have only one endpoint that receives posts. we can see the links of that endpoint. when you click that, you will see that it says, ‘ this does not exist ‘. That’s how WordPress reacts, you’re supposed to post information and we have not posted anything yet so it automatically protects you.
Fixing the issue
Let’s write some code to fix our issue.
We shall pass an object called request_data and it is basically using the REST API class wp rest request that has a number of members added to it. For example, you can get the method, you can set parameters, you can get the parameters and so on
we shall get the parameters that are passed on from the request data object. for example if we’re expecting a name, we’re shall have a variable of name that we’ll expect to get from the parameters, a parameter which is passed in as a name.
We can get a number of other things, even passwords.
When we make a post to this API, we’re going to get those parameters stored up inside small variables and then we shall pass in a data status OK. we’re going to receive that information and also pass in the message
We can add some conditions to return particular statements.
The following code snippet shows conditions for returning response statements
function techiepress_receive_callback( $request_data ) {
$data = array();
$parameters = $request_data->get_params();
$name = $parameters['name'];
$password = $parameters['password'];
if ( isset($name) && isset($password) ) {
$data['status'] = 'OK';
$data['received_data'] = array(
'name' => $name,
'password' => $password,
);
$data['message'] = 'You have reached the server';
} else {
$data['status'] = 'Failed';
$data['message'] = 'Parameters Missing!';
}
return $data;
}
Inside our function, we have passed in a name, password and that’s what we’re expecting. When both of them are set we shall have a proper welcome message and if they are not passed, then we have shall have an error.
Save that and open up postman to help you post that information. Use an available POST endpoint of your choice. Get postman from here if you do not have it installed.
Go to the body and pass in a name, the password you will receive data with status OK.
If you make a mistake in the body, you will receive an error message. ‘parameters missing’
Conclusion
With our API, we’re able to receive information from another API and still get back a proper message and also just utilize the information the way we want it.
So in the next article, we’re going to 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. 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 or we can use the data in whatever way we choose.
Find all the code here on Github
Leave a Reply