Put this to REST: How to Channel the Potential of the 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 Website Development.

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.

WordPress API default stucture

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

GET METHOD

<?php

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, “https:/colorwhistle.com/wp-json/wp/v2/posts/”);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

curl_setopt($ch, CURLOPT_HEADER, 1);

$result = curl_exec($ch);

echo($result);

curl_close($ch);

?>

The below links represent examples for additional query API GET requests,

  • http://yoursite.com/wp-json/wp/v2/posts/?status=publish
  • http://yoursite.com/wp-json/wp/v2/posts/?per_page=1

PUT METHOD

<?php 

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, “https://colorwhistle.com/wp-json/wp/v2/posts/997”);

curl_setopt($ch, CURLOPT_HTTPHEADER, array(‘Content-Type: application/json’, ‘Authorization: Basic ‘.base64_encode( ‘ admin:APIToken ‘ )));

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

curl_setopt($ch, CURLOPT_CUSTOMREQUEST, “PUT”);

curl_setopt($ch, CURLOPT_POST, true);

curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode(

array( 

“status” => “publish”

)

) );

$result = curl_exec($ch);

echo($result);

curl_close($ch);

?>

The below links represent examples for additional query API PUT requests,

  • http://yoursite.com/wp-json/wp/v2/posts/?status=%E2%80%9Ddraft%E2%80%9D
  • http://yoursite.com/wp-json/wp/v2/posts/567

POST METHOD

<?php 

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, “https://colorwhistle.com/wp-json/wp/v2/posts/997”);

curl_setopt($ch, CURLOPT_HTTPHEADER, array(‘Content-Type: application/json’, ‘Authorization: Basic ‘.base64_encode( ‘ admin:APIToken ‘ )));

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

curl_setopt($ch, CURLOPT_CUSTOMREQUEST, “POST”);

curl_setopt($ch, CURLOPT_POST, true);

curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode(

array( 

“title” => “Testing”,

)

) );

$result = curl_exec($ch);

echo($result);

curl_close($ch);

?>

The below links represent examples for additional query API POST requests,

  • http://yoursite.com/wp-json/wp/v2/posts/

DELETE METHOD

<?php 

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, “https://colorwhistle.com/wp-json/wp/v2/posts/997”);

curl_setopt($ch, CURLOPT_HTTPHEADER, array(‘Content-Type: application/json’, ‘Authorization: Basic ‘.base64_encode( ‘admin:APIToken’ )));

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

curl_setopt($ch, CURLOPT_CUSTOMREQUEST, “DELETE”);

$result = curl_exec($ch);

echo($result);

curl_close($ch);

?>

The below links represent examples for additional query API DELETE requests,

  • http://yoursite.com/wp-json/wp/v2/posts/567
  • http://yoursite.com/wp-json/wp/v2/posts/567?force=true

How to Disable the WordPress REST API

  • add_filter( ‘json_enabled’, ‘__return_false’ );
  • add_filter( ‘json_jsonp_enabled’, ‘__return_false’ );

For custom post type to enable REST API

  • “show_in_rest” = true;

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

Looking for WordPress Development Services?

Seize and experience the transformative impact of WordPress solutions with ColorWhistle.

Conclusion:

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

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

Wordpress-Development-advertisement-Colorwhistle
Sankarnarayan. R
About the Author - Sankarnarayan. R

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.

Leave a Reply

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

Ready to get started?

Let’s craft your next digital story

Our Expertise Certifications - ColorWhistle
Go to top
Close Popup

Let's Talk

    Sure thing, leave us your details and one of our representatives will be happy to call you back!

    Eg: John Doe

    Eg: United States

    More the details, speeder the process :)