Put this to REST: how to channel the potential of the REST API in WordPress

February 20, 2019
Potential of REST API in WordPress

Introduction:

With the latest version of WordPress featuring REST API, the web developer sphere has exploded in conversation about the possibilities, and how it will or will not enhance the WordPress API development experience.

Before looking at how REST API potentially changes the way WordPress developers design, we’ll look at some of the basics.

We’ll also see a simple implementation that further illustrates the potential of the REST API in WordPress.

What is an WordPress API?

API(Application Programming Interface) is a set of code that acts as an interface between two applications for sharing information.

One application sends an authentication request to the other and when the authentication is completed, the applications are ready for sharing the information.

For example, we can consider embedding Google Maps in a website.

The basic concept of Google Maps is to search for different places and get directions.

In order to embed this into your website, you will first need to create a maps API key by logging in with your Google Account.

Once you get the API key  and maps embed code from Google, you will place the embed code into the site.

This code first sends an authentication request to the Maps API with the API key you created and when authentication is success, another request is sent to the API for retrieving the directions for the address that you given.

So with just two API calls from your website, you can show the directions of any address with the help of Google API.

REST API:

If we attempt to explain Representational State Transfer or REST in a simple manner, it is basically a set of principles that tell you how a well-designed web application behaves.

A well-designed web application does the following:

  • Application is a set of network web pages
  • User selects links to progress (state transitions)
  • Next page (next state of application) is delivered to user for working on

The REST principles, developed by Roy Thomas Fielding in 2000, use the HTTP protocol of the World Wide Web to post/read/update/delete data between two sources on the web.

The two sources may have been created using any programming language and would be completely independent of each other.

The simplest way of looking at REST is that it is a set of architectural principles that make your web applications faster by letting discrete systems interact with each other without burdening either with translation.

The important thing to remember is that REST is not a programming language, it is a driving force for web development.

Simple example for API connectivity:

To connect to a REST API, we will use the following HTTP methods PUT, GET, POST and DELETE.

The URL structure for the http request will be in the following format “https://colorwhistle.com/teachers/john where teacher and john are sub directories.

For data exchange, the two main formats used will be XML and JSON. Mostly for all APIs, the output data will be in XML or JSON format.

Let us see some examples for PUT, GET,  POST and DELETE methods using  PHP Curl function.

GET Method

// ID$ID = 2;

// access token

$accesstoken = ‘dgdfopppfllllfooofooocllllc’;

// set up the curl

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, “https://colorwhistle.com/school/$ID?access_token=$accesstoken”);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);

// execute the request

$result = curl_exec($ch);

// output the school information – includes the header

echo($result);

// close curl resource to free up system resources

curl_close($ch);

GET Method Response

HTTP/1.1 200 OK
Date: Thu, 01 Sep 2016 12:30:26 IST
Server: Apache
Transfer-Encoding: chunked
Content-Type: application/json
{
“ID”: “2”,
“fields”: {
“School”: “National Model”,
“Address”: “142 Durai Sami Nagar”,
“ZipCode”: “641664”,
“City”: “Coimbatore”,
“Country”: “India”
},
“created”: “2016-09-01 12:33:30”
}

POST Method

// ID$ID = 1;
$schooldata = array (
“School” => “National Model”,
“City” => “Coimbatore”,
“Country”=>”India”
);// json encode data$data = json_encode($schooldata);// access token

$accesstoken = ‘dgdfopppfllllfooofooocllllc’;

// set up the curl

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, “https://colorwhistle.com/school/$ID/details?access_token=$accesstoken”);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, “POST”);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
‘Content-Type: application/json’,
‘Content-Length: ‘ . strlen($data)
));

// execute the request

$result = curl_exec($ch);

// output the school information

echo($result);

// close curl resource to free up system resources

curl_close($ch);

POST Method Response

HTTP/1.1 200 OK
Date: Thu, 01 Sep 2016 12:30:26 IST
Server: Apache
Transfer-Encoding: chunked
Content-Type: application/json

PUT Method

// ID$ID = 1;

// Find the school named National model in coimbatore

$schooldata = array (
“School” => “National Model”,
“City” => “Coimbatore”
);

// Update the country name for the school to Britan

$modify = array(
‘Country’ => ‘Britan’
);

// json encode data

$data = json_encode($modify);

// access token

$accesstoken = ‘dgdfopppfllllfooofooocllllc’;

// set up the curl

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, “https://colorwhistle.com/school/$ID/details?access_token=$accesstoken”);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, “PUT”);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
‘Content-Type: application/json’,
‘Content-Length: ‘ . strlen($data)
));

// execute the request

$result = curl_exec($ch);

// output the school information

echo($result);

// close curl resource to free up system resources

curl_close($ch);

DELETE Method

// ID$ID = 1;
// access token$accesstoken = ‘dgdfopppfllllfooofooocllllc’;// set up the curl$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, “https://colorwhistle.com/school/$ID/details?access_token=$accesstoken”);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, “DELETE”);

// execute the request

$result = curl_exec($ch);

// output the school information

echo($result);

// close curl resource to free up system resources

curl_close($ch);

DELETE Method Response

HTTP/1.1 200 OK
Date: Thu, 01 Sep 2016 12:30:26 IST
Server: Apache
X-Powered-By: PHP/5.3.10-1ubuntu3.9
X-Deleted: school 1
Content-Length: 0
Content-Type: application/json

WordPress and REST API: the potential

Why is there so much conversation about REST API in WordPress?

One reason is that WordPress custom websites are fast becoming the norm for most business owners.

And WordPress API development experts want to harness the potential of this principle to provide better user experience for their clients’ websites.

For a business owner who wants to get online with a website, custom WordPress websites are the ideal way to create a unique identity for their brand.

WordPress already had several plug-in features which allow it to seamlessly interface with several applications on the web.

By using the REST API in conjunction with JSON (JavaScript Object Notation), a WordPress REST API website expert can develop some really powerful applications.

JSON is a text based format for storing data and is used for exchanging data between platforms.

What the REST API does is that it will expose some of the WordPress functionality to the web.

Other websites will now be able to interact with it.

The external website can create, remove, edit and update content on the WordPress website.

This level of interactivity makes the website truly engaging for the potential customer.

Advantages of WordPress Rest API:

For WordPress API development, it means that developers of plug-ins and themes can deliver their output faster since they do not need to know the connectivity details.

They can even create web applications using Backbone model and AngularJS that aid in manipulating data on the site.

The WordPress REST API is already being implemented in:

  • Single Page Applications
  • Creation of new admin interfaces for WordPress
  • Mobile Apps
  • Integrating other server-side platforms

Conclusion:

The REST API in WordPress certainly has a lot of potential for the future of web development.

For a WordPress REST API website expert it provides several avenues for making the user experience of their client’s websites truly memorable.

Liked This Article?

By entering your email, you agree to our Terms and Conditions and Privacy Policy.
About the Author - Sankar

The founder and mastermind behind ColorWhistle is Sankarnarayan, a professional with over fourteen years of experience and a passion for website design services and digital marketing services. At ColorWhistle, our team has a wide range of skills and expertise and we always put our clients’ satisfaction first. This is what sets us apart from the competition – an eye for detail and the best website development services from the start to the completion of your project.

Share your thoughts

Your email address will not be published. Required fields are marked *