Building a RESTful API with WordPress

Building a RESTful API with WordPress has to be the most difficult process ever conceive. However, once you figure it out it then becomes the greatest and more powerful tool in your arsenal.

Before I start telling you all about my experiences with creating this API, keep in mind that the point of your API is to help you achieve something you couldn’t without it. For my application, I need to pass data remotely to my database from my games and other applications. Now that we went over this let talk about why we use this.

What is REST?

REST is Representational state transfer which allows applications to talk to one another over the Internet. Let’s say you need to save some data from your game application to a database that is connected to your website you would need to send the data from your app to the RESTful web service to do just that. Which for the backend code you could use a range of programming languages, I prefer PHP for that. Then poof your code is there in the database.

Don’t I Need a Special Plugin for this?

When working with a RESTful API and WordPress yes would need a plugin that the WordPress team developed themselves to get this done. I will briefly go through this process with you but if you would like a deeper dive I wrote a post about this topic here. The name of the plugin is WordPress Rest API (the link for the plugin is provided) and all that is needed is to install it onto your existing theme. Once this is done then let start working on getting the RESTful API up and running for you to start using it!

Where to start?

The question that is the most asked is where do I start with this plugin. I ask them first what is this web service trying to accomplish? After that, it’s coding time (queue in the Power Ranger’s music)! Now the first time that is need is the action for the API which looks like this:

add_action('rest_api_init', 'my_routes');

What this is doing is allowing us to declare our routes that are needed for our calls to this web service. Think about it as enabling our API for our usage. Now that we declared it, it’s time to create our routes in the function we declared in the “my_routes” function. For these routes, you can do many things but for this purpose, I will show you have to write a GET and POST method for your service. The GET allows you to retrieve data without sending data and POST allow you send data.

function my_routes(){

//This is our GET method

register_rest_route(‘my_api/v1’, ‘/logout/’,

array(

‘methods’ => ‘GET’,

‘callback’ => ‘logout’

)

);

//This is our POST method

register_rest_route(‘my_api/v1’, ‘/login/’,

array(

‘methods’ => ‘POST’,

‘callback’ => ‘login’

)

);

}

What this GET method is doing is getting a just a true or false state from the logout function which tells us if we have logged out from the site. The most important this about this is the register_rest_route function that we must use to as it states to register the route for the API to recognize it as usable. The params for this function are simple and complex if this is the first time working with this type of thing. The ‘my_api/v1’ is the beginning of our URL to allow the system know that we are calling our API and if you’re wondering this are all placeholder names. So name them as you like. The next piece of the puzzle is the last part of our URL. I generally keep it the same name as my function which makes it easier for me to know what I need it for and plus it just make everything easier for later. Now let’s talk about the arrays that we have set us here. Methods are the same that we talked about early with the GET and POST. The callback is just the function that we are using for the route. After that you have these parts set up we must talk about the functions themselves.

Let’s Talk Functions

The most annoying part of this process is the POST function as you just can’t use the $_POST[”] variable that is built into PHP. You would need to use $_REQUEST[”] would allow all data to be passed safely to your back end. Let’s see an example of this:

function login(){

//request part

$user_name = $_REQUEST[‘name’];

return $user_name;

}

What is happening here is that I’m getting the ‘name’ data from my front end code to be able to check that name in my database. This looks simple but if you didn’t know to use $_REQUEST[”] then you will be greeted with errors and frustration like I was for about week and a half. For GET all you would need is just the return part of the code which makes it simple. Keep in mind that with GET if hackers want your code and you use GET they will see all the results whether you like it or not. I suggest using POST even for small things. The best way to use post is by sending the user’s id or token. This will add a level of security to your system. Now we are done and you can start creating your web service using RESTful services!

So now that you understand the concept of the RESTful API and what it’s used for. What will you build with it? 

 

[the_ad_placement id="ghs_side_ads"]

Related Post

Ultimate Guide to WordPress SEO – Yoast

When it comes to WordPress and SEO the only name that usually gets brought up is Yoast. Yoast is an all-in-one SEO plugin that allows seeing how the post ranks and how keyword-heavy your article is. Meaning that you can reach more people looking for your articles or website.   The power of Yoast comes from...

Read More

Why social matters for game development

As a developer one of the most valuable assets at our fingertips is visibility. The more eyes we have on our projects we can start to gauge the excitement and plan accordantly. This is why we must start mapping out how we want to tackle our social branding, marketing, and reach from day one. Without...

Read More

Preventing Burnout as a Software Engineer

As software engineers experience burnout from many things. So today we need to discuss how burnout happens and what can you do to prevent and avoid getting burnout. Before we go over these tips and preventative measures, we need to keep in mind that burnout happens to everyone.

Read More