{"id":3615,"date":"2024-04-01T00:43:58","date_gmt":"2024-03-31T19:13:58","guid":{"rendered":"https:\/\/www.wpoven.com\/blog\/?p=3615"},"modified":"2024-05-06T17:47:48","modified_gmt":"2024-05-06T12:17:48","slug":"the-wordpress-rest-api-getting-started-guide","status":"publish","type":"post","link":"https:\/\/www.wpoven.com\/blog\/the-wordpress-rest-api-getting-started-guide\/","title":{"rendered":"The WordPress REST API : Getting Started Guide"},"content":{"rendered":"<p style=\"text-align: justify;\">You mush have heard the term API if you work in a technical domain. It can be a little confusing, In this guide, we have tried to clear all your doubts so that you can easily talk about API with your team and colleagues.<\/p>\n\n<p>&nbsp;<\/p>\n<h2 id=\"What is API?\"><strong>What is API?<\/strong><\/h2>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">Web Applications are no more a standalone entity. They need to communicate with third-party entities for better outcomes and instant real-time data. In order to communicate with third-party components, they need a medium and protocol to do that.<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">Such defined protocols are known as API, or Application Programming Interface. Through this <strong><a href=\"https:\/\/en.wikipedia.org\/wiki\/Application_programming_interface\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">interface<\/a><\/strong>, two independent entities communicate with each other and deliver the desired outcome.<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">Let us understand this with an example from the travel domain. There are hundreds of independent airline websites allowing their customers to book tickets in real-time. But there are websites like Skyscanner where results from all the different websites are collected, compared, and displayed to the user. In this case, Skyscanner collects data from these hundreds or thousands of websites using a defined component, which is knowns as its Application Programming Interface or API.<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">Similarly, there are weather apps on a smartphone that gets the data from local servers from various cities across the world. The whole of this process is handled by one or the other API.<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">It acts as a strong tool for application developers to interact with another software program.<\/span><\/p>\n<h2 id=\"What is REST API?\" style=\"text-align: justify;\"><b>What is REST API?<\/b><\/h2>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><b>Representational State Transfer API<\/b><span style=\"font-weight: 400;\"> is also popularly known as REST API. It is one of the architectural styles used in web programming. One of the other architectural styles is the <\/span><b><i>Simple Object Access Protocol<\/i><\/b><span style=\"font-weight: 400;\">, but REST is more popular because it consumes less bandwidth and hence responds faster in case of the web application. It is compatible with ASP, PHP, Java, Ruby, Android, etc.<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">All the popular browsers use REST and that makes it suitable for Cloud-based web applications. REST API breaks down the whole process into various transactional modules making it easy for developers.<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">As discussed with the weather app example in the previous section when applications send a data request to various servers, the servers will respond accordingly to send the requested data in the prescribed format (XML or JSON). But processing and sending XML or JSON files is time-consuming and not as real-time as it should be.<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">To overcome this server creates an object and converts that object into a <\/span><b>representational state<\/b><span style=\"font-weight: 400;\">, and the state is being transferred to the client (application) in the form of data.<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">There are 4 stages of resource processing that any web application goes through (also known as CRUD operations). Create, Read, Update and Delete, or there HTTP equivalents like POST, GET, PUT AND DELETE.<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">The two popular methods of implementing RESTful APIs are Jersey and Sparing.<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">The most popular usage of REST APIs is in Mobile\/cloud applications, social networking websites, and automated business process applications. The APIs are cache-friendly so they are more mobile-friendly owing to speed and efficiency.<\/span><\/p>\n<h2 id=\"REST API in WordPress\"><b>REST API in WordPress<\/b><\/h2>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">WordPress has evolved from a simple blogging platform to a complete Web Application Development platform. Like any other application development tool, it also needs an Application Programming Interface (API) to deliver desired results.<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">WordPress Representational State Transfer (REST) API was started as a separate plugin. It was first developed in June 2013 by Ryan McCue and Rachel Baker and was included in the core WP update in 2015. It gained huge popularity and hundreds of developers came forward to improve it further, and that is when it completely became part of the core WordPress since version 4.7 (in 2016). This development supported the aim of WordPress to become a complete framework package for web developers.<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">The inclusion of REST API within the core framework enabled WordPress to communicate, integrate, and exchange data with other external applications and servers while keeping the data processing operations separate from the front-end interface, thus making it fast. Later its JavaScript\/JSON (JavaScript Object Notation) compatible version was also developed and it is known as JSON REST API, which makes it even easier to communicate with applications built in other languages.<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">The core <\/span><b>advantage of WordPress REST API<\/b><span style=\"font-weight: 400;\"> is that developers don\u2019t have to code the applications from scratch in PHP, they can get their WordPress program to interact with any other program that can make HTTP requests and interpret JSON. The other advantage is making the programs more fast and responsive for end-users as WordPress REST API simplifies AJAX calls hence fetching the data faster than before.<\/span><\/p>\n<p style=\"text-align: justify;\"><b>Here are some popular applications that can be integrated using the REST API in WordPress:<\/b><\/p>\n<h2>Applications for integrating REST API in WordPress<\/h2>\n<p>&nbsp;<\/p>\n<ul style=\"text-align: justify;\">\n<li style=\"font-weight: 400;\">\n<h2><b>Zapier:<\/b><\/h2>\n<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<p><img decoding=\"async\" class=\"aligncenter size-full wp-image-5628\" src=\"https:\/\/www.wpoven.com\/blog\/wp-content\/uploads\/2019\/02\/1200px-Zapier_logo.svg_.png\" alt=\"Zapier_logo\" width=\"1200\" height=\"550\" srcset=\"https:\/\/www.wpoven.com\/blog\/wp-content\/uploads\/2019\/02\/1200px-Zapier_logo.svg_.png 1200w, https:\/\/www.wpoven.com\/blog\/wp-content\/uploads\/2019\/02\/1200px-Zapier_logo.svg_-300x138.png 300w, https:\/\/www.wpoven.com\/blog\/wp-content\/uploads\/2019\/02\/1200px-Zapier_logo.svg_-1024x469.png 1024w, https:\/\/www.wpoven.com\/blog\/wp-content\/uploads\/2019\/02\/1200px-Zapier_logo.svg_-768x352.png 768w\" sizes=\"(max-width: 767px) 89vw, (max-width: 1000px) 54vw, (max-width: 1071px) 543px, 580px\" title=\"\"><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\"><a href=\"https:\/\/zapier.com\/\" target=\"_blank\" rel=\"\u201dnoopener noopener noreferrer\"><strong>Zapier<\/strong><\/a> in itself is a platform that interacts with hundreds of applications using Rest API that includes, Google Docs, MailChimp, Evernote, Buffer, Slack, Trello, Twitter, Facebook, Salesforce, and almost any other popularly used web application. Using this you can power up your simple WordPress website to interact and exchange data with any of the listed apps, the developer just needs to define the triggers and actions. For example, if you want data from a form to be directly added to one of your Google Spreadsheets, you can do it by simply adding API key from Zapier into your WordPress and defining the process in Zapier and all your data from a simple contact form will be stored in the selected spreadsheet. How cool is that?<\/span><\/p>\n<ul style=\"text-align: justify;\">\n<li style=\"font-weight: 400;\">\n<h2><b>Google Maps:<\/b><\/h2>\n<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<p><img decoding=\"async\" class=\"aligncenter size-full wp-image-5629\" src=\"https:\/\/www.wpoven.com\/blog\/wp-content\/uploads\/2019\/02\/google_maps_2020_logo_before_after.png\" alt=\"google map logo\" width=\"2000\" height=\"832\" srcset=\"https:\/\/www.wpoven.com\/blog\/wp-content\/uploads\/2019\/02\/google_maps_2020_logo_before_after.png 2000w, https:\/\/www.wpoven.com\/blog\/wp-content\/uploads\/2019\/02\/google_maps_2020_logo_before_after-300x125.png 300w, https:\/\/www.wpoven.com\/blog\/wp-content\/uploads\/2019\/02\/google_maps_2020_logo_before_after-1024x426.png 1024w, https:\/\/www.wpoven.com\/blog\/wp-content\/uploads\/2019\/02\/google_maps_2020_logo_before_after-768x319.png 768w, https:\/\/www.wpoven.com\/blog\/wp-content\/uploads\/2019\/02\/google_maps_2020_logo_before_after-1536x639.png 1536w\" sizes=\"(max-width: 767px) 89vw, (max-width: 1000px) 54vw, (max-width: 1071px) 543px, 580px\" title=\"\"><\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"font-weight: 400;\">Using an API key from Google you can get to show the map on your website frontend. Previously it was done by embedding an iframe code into your pages.<\/span><\/p>\n<p>&nbsp;<\/p>\n<ul>\n<li>\n<h2><b>Simmer:<\/b><\/h2>\n<\/li>\n<\/ul>\n<p><img decoding=\"async\" class=\"aligncenter size-full wp-image-5630\" src=\"https:\/\/www.wpoven.com\/blog\/wp-content\/uploads\/2019\/02\/simmer-wp.jpg\" alt=\"simmer-wp\" width=\"600\" height=\"400\" srcset=\"https:\/\/www.wpoven.com\/blog\/wp-content\/uploads\/2019\/02\/simmer-wp.jpg 600w, https:\/\/www.wpoven.com\/blog\/wp-content\/uploads\/2019\/02\/simmer-wp-300x200.jpg 300w\" sizes=\"(max-width: 600px) 100vw, 600px\" title=\"\"><\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"font-weight: 400;\"><a href=\"https:\/\/simmerwp.com\/\" target=\"_blank\" rel=\"\u201dnoopener noopener noreferrer\"><strong>Simmer<\/strong><\/a> is a recipe and cookbook platform. Using this developers can source the recipes from it and display it within their desired UI on their own website and mobile websites.<\/span><\/p>\n<ul>\n<li>\n<h2><b>Social Media Feed:<\/b><\/h2>\n<\/li>\n<\/ul>\n<p><span style=\"font-weight: 400;\">Using a REST API you can now show your social media feed from networks like Facebook, YouTube, Instagram, and Twitter on your website. <\/span><\/p>\n<h3 style=\"text-align: justify;\"><b>WordPress Theme and Plugin use REST API for Maintenance, Support, and Update<\/b><\/h3>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">If you are into WordPress development you surely might have come across inserting the API Key option in theme options and plugin settings. Once an API key is inserted and saved the WordPress installation will now be able to communicate with the original developer server. Once API key is authenticated by the developer server it will send the update theme or plugin files to the WordPress installation.<\/span><\/p>\n<h2 style=\"text-align: justify;\"><b>Data Sharing through Plugins using REST API<\/b><\/h2>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">If it is a plugin, the API will sync the data between your WordPress website and the third-party application.<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">For example, if you use a Mailchimp Plugin and insert an authenticated API key you can directly send the opt-in emails to Mailchimp and it will store it in the selected Mailchimp list in your account.<\/span><\/p>\n<h2 style=\"text-align: justify;\"><b>How REST API works in WordPress<\/b><\/h2>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">Like other REST APIs, in WordPress too it works in four core HTML operations of GET, POST, PUT, and DELETE along with an additional command to instruct the API on what data is required and what to do with that data. You can understand this by going through the sections below.<\/span><\/p>\n<h2 style=\"text-align: justify;\"><b>Components of WordPress REST API:<\/b><\/h2>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">The Representational State Transfer (REST) API in WordPress comprises of 5 key components:<\/span><\/p>\n<p>&nbsp;<\/p>\n<ul style=\"text-align: justify;\">\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Requests<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Routes\/Endpoints<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Responses<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Schema<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Controller Classes<\/span><\/li>\n<\/ul>\n<h3 style=\"text-align: justify;\"><b>Understanding Requests:<\/b><\/h3>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">Requests are communication in the form of getting operations between a client and the server implemented using the class WP_REST_Request submitted via HTTP. The requests are done to the registered routes.<\/span><\/p>\n<h3 style=\"text-align: justify;\"><b>Understanding Routes and Endpoints:<\/b><\/h3>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">Each client-server request follows a certain route and reaches various endpoints within the route to get the desired output. In this case, Routes are effectively the URLs. When a request is made<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">When a request is made, API shows the available routes and each of the endpoints available within that route. It will then compare the GET request with the available endpoints and return the response accordingly.<\/span><\/p>\n<h3 style=\"text-align: justify;\"><b>Responses:<\/b><\/h3>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">As it is a self-explanatory response is desired data that API returns against the request made. The response class is defined by WP_REST_Response.<\/span><\/p>\n<h3 style=\"text-align: justify;\"><b>Schema:<\/b><\/h3>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">As web developers already understand that fetching data is done within a defined structure. This structure is known as a schema. A well-defined schema has an index of input parameters it accepts and an index of all the data attributes it can return. The schema also acts as a security cover for API as it only validates the requests adhering to its structure.<\/span><\/p>\n<h3 style=\"text-align: justify;\"><b>Controller Classes:<\/b><\/h3>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">The controller coordinates between all the above elements and manages the complete process to get the desired output and optimize the complete process.<\/span><\/p>\n<h2 id=\"How to create your first REST API functionality:\" style=\"text-align: justify;\"><b>How to create your first REST API functionality:<\/b><\/h2>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">You have already learned about the 5 components of WordPress REST API, so the next step is how to implement these APIs in your project. To start with you must learn about the most popular Endpoints in a WP REST API. <\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><b>They are as follows:<\/b><\/p>\n<p>&nbsp;<\/p>\n<table>\n<tbody>\n<tr>\n<td><b>RESOURCES<\/b><\/td>\n<td><b>BASE ROUTE<\/b><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">Posts<\/span><\/td>\n<td><span style=\"font-weight: 400;\">\/wp\/v2\/posts<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">Post Revisions<\/span><\/td>\n<td><span style=\"font-weight: 400;\">\/wp\/v2\/revisions<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">Categories<\/span><\/td>\n<td><span style=\"font-weight: 400;\">\/wp\/v2\/categories<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">Tags<\/span><\/td>\n<td><span style=\"font-weight: 400;\">\/wp\/v2\/tags<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">Pages<\/span><\/td>\n<td><span style=\"font-weight: 400;\">\/wp\/v2\/pages<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">Comments<\/span><\/td>\n<td><span style=\"font-weight: 400;\">\/wp\/v2\/comments<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">Taxonomies<\/span><\/td>\n<td><span style=\"font-weight: 400;\">\/wp\/v2\/taxonomies<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">Media<\/span><\/td>\n<td><span style=\"font-weight: 400;\">\/wp\/v2\/media<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">Users<\/span><\/td>\n<td><span style=\"font-weight: 400;\">\/wp\/v2\/users<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">Post Types<\/span><\/td>\n<td><span style=\"font-weight: 400;\">\/wp\/v2\/types<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">Post Statuses<\/span><\/td>\n<td><span style=\"font-weight: 400;\">\/wp\/v2\/statuses<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">Settings<\/span><\/td>\n<td><span style=\"font-weight: 400;\">\/wp\/v2\/settings<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">You are familiar with the endpoints, now you need to understand how to create an HTTP call, which always starts with your domain URL, for example myxyzdomain.com, and is followed by one of the base routes mentioned in the above table.<\/span><\/p>\n<h2 id=\"REST API Authentication\" style=\"text-align: justify;\"><b>REST API Authentication<\/b><\/h2>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">There are two types of functions in API that can be done without authentication, while others need authentication.<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">In WordPress REST API authentication can be done through any of the three methods depending on the purpose.<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">The 3 methods are:<\/span><\/p>\n<p>&nbsp;<\/p>\n<table>\n<tbody>\n<tr>\n<td><b>Method of Authentication<\/b><\/td>\n<td><b>Purpose<\/b><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">Cookie Authentication<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Plugins\/themes running on the same application<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">OAuth<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Integrate with 3rd party apps, for example &#8211; Mailchimp, Instagram, Twitter, etc.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">Basic Authentication (through a plugin)<\/span><\/td>\n<td><span style=\"font-weight: 400;\">For development and testing only<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">Cookie authentication basically works well with the default login functionality of WordPress. The REST API works on this technique called \u201cnumber used once\u201d or \u201cnoonce\u201d which simply is a WordPress security token with a limited lifetime and is specific to the user. It is used to protect your application from being exploited by external sources and interfering with your program. This type of interference is commonly known as Cross-site request forgery (CSRF) or one-click attack or session riding.<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">For REST API Authentication inbuilt and automatic javascript API can be used, or alternatively, manual AJAX authentication can also be used. If using Ajax, a noonce authentication request will have to be sent along with every request. In the absence of noonce the API will consider it as an unauthenticated request.<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">For example if REST API authentication<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><i><span style=\"font-weight: 400;\">As an example, this is how the built-in Javascript client creates the nonce:<\/span><\/i><\/p>\n<p>&nbsp;<\/p>\n<pre class=\"lang:default decode:true\">&lt;?php\r\nwp_localize_script( 'wp-api', 'wpApiSettings', array(\r\n'root' =&gt; esc_url_raw( rest_url() ),\r\n'nonce' =&gt; wp_create_nonce( 'wp_rest' )\r\n) );<\/pre>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><i><span style=\"font-weight: 400;\">This is then used in the base model:<\/span><\/i><\/p>\n<p style=\"text-align: justify;\"><i><span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0<\/span><\/i><\/p>\n<pre class=\"lang:default decode:true\">options.beforeSend = function(xhr) {\r\nxhr.setRequestHeader('X-WP-Nonce', wpApiSettings.nonce);\r\nif (beforeSend) {\r\n\u00a0\u00a0\u00a0 return beforeSend.apply(this, arguments);\r\n}\r\n};<\/pre>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><i><span style=\"font-weight: 400;\">Here is an example of editing the title of a post, using jQuery AJAX:<\/span><\/i><\/p>\n<p style=\"text-align: justify;\"><i><span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0<\/span><\/i><\/p>\n<pre class=\"lang:default decode:true\">$.ajax( {\r\nurl: wpApiSettings.root + 'wp\/v2\/posts\/1',\r\nmethod: 'POST',\r\nbeforeSend: function ( xhr ) {\r\n\u00a0\u00a0\u00a0 xhr.setRequestHeader( 'X-WP-Nonce', wpApiSettings.nonce );\r\n},\r\ndata:{\r\n\u00a0\u00a0\u00a0 'title' : 'Hello Moon'\r\n}\r\n} ).done( function ( response ) {\r\nconsole.log( response );\r\n} );<\/pre>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">The other way of REST API authentication is using a plugin, but it is only recommended to be used during development and not on a live application.<\/span><\/p>\n<h2 style=\"text-align: justify;\"><b>REST API Global Parameters<\/b><\/h2>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">WordPress Representational State Transfer API has some pre-defined parameters, also known as meta parameters. They are as follows:<\/span><\/p>\n<ul style=\"text-align: justify;\">\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">_jsonp: <\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">This metaparameter adds a JavaScript callback function before the data.<\/span><\/li>\n<\/ul>\n<p style=\"text-align: justify;\"><i><span style=\"font-weight: 400;\">Example:<\/span><\/i><span style=\"font-weight: 400;\"><br \/>\n<\/span><\/p>\n<p>&nbsp;<\/p>\n<pre class=\"lang:default decode:true\">&lt;script&gt;\r\nfunction receiveData( data ) {\r\n\u00a0\/\/ Do something with the data here.\r\n\u00a0\/\/ For demonstration purposes, we'll simply log it.\r\n\u00a0console.log( data );\r\n}\r\n&lt;\/script&gt;\r\n&lt;script src=\"https:\/\/demo.wp-api.org\/wp-json\/?_jsonp=receiveData\"&gt;&lt;\/script&gt;<\/pre>\n<ul style=\"text-align: justify;\">\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">_method (or X-HTTP-Method-Override header)<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">A method override parameter is used to make the API function compatible with all types of servers and clients, even the ones that don\u2019t process the HTTP methods correctly.<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">_envelope<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">An envelope parameter collates all the response data within a body and then sends it to the requesting client. An added advantage of using this parameter is that it provides access to data to servers and clients that do not support accessing the full response data.<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><i><span style=\"font-weight: 400;\">Example:<\/span><\/i><i><span style=\"font-weight: 400;\"><br \/>\n<\/span><\/i><\/li>\n<\/ul>\n<pre class=\"lang:default decode:true\">HTTP\/1.1 200 OK\r\n{\r\n\u00a0\"status\": 302,\r\n\u00a0\"headers\": {\r\n\"Location\": \"http:\/\/example.com\/wp-json\/wp\/v2\/users\/42\"\r\n\u00a0},\r\n\u00a0\"body\": {\r\n\"id\": 42\r\n\u00a0}\r\n}<\/pre>\n<ul>\n<li style=\"font-weight: 400; text-align: justify;\"><span style=\"font-weight: 400;\">_embed<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">In WordPress or any other development environment, any resource never works in isolation, it is always linked to other related resources. For example, an eCommerce product is linked to one or more categories, a featured image, product reviews, etc. Using an embed meta parameter the API directs the server to send all the parameters embedded in its response. It will minimize the number of HTTP requests made by the client. An embed parameter is passed within the GET parameter.<\/span><\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<h2 style=\"text-align: justify;\"><b>How REST API Initiates Communication between Client and Server<\/b><\/h2>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">To start the communication between two entities (server and client) it is important for the client to know whether a compatible API exists on a server that allows the communication. To initiate the communication and discover the client (in this case WordPress web application) send a request from HEADER using a route link.<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">This is a two-step process. In 1st step, the web application identifies and discovers the API. In the second step, the client or web application should identify what all functions and data can be processed through it.<\/span><\/p>\n<h4 style=\"text-align: justify;\"><b>Discovery of API<\/b><\/h4>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><i><span style=\"font-weight: 400;\">Example:<\/span><\/i><\/p>\n<p style=\"text-align: justify;\"><i><span style=\"font-weight: 400;\">HTML:<\/span><\/i><\/p>\n<pre class=\"lang:default decode:true\">&lt;link rel='https:\/\/api.w.org\/' href='http:\/\/example.com\/wp-json\/' \/&gt;<\/pre>\n<p style=\"text-align: justify;\"><i><span style=\"font-weight: 400;\">Javascript:<\/span><\/i><\/p>\n<pre class=\"lang:default decode:true\">\/\/ jQuery method\r\nvar $link = jQuery( 'link[rel=\"https:\/\/api.w.org\/\"]' );\r\nvar api_root = $link.attr( 'href' );\r\n\/\/ Native method\r\nvar links = document.getElementsByTagName( 'link' );\r\nvar link = Array.prototype.filter.call( links, function ( item ) {\r\nreturn ( item.rel === 'https:\/\/api.w.org\/' );\r\n} );\r\nvar api_root = link[0].href;<\/pre>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><i><span style=\"font-weight: 400;\">XML <\/span><\/i><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><i><span style=\"font-weight: 400;\">XML uses Really Simple Discovery method. It comprises of a 2-step process:<\/span><\/i><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><i><span style=\"font-weight: 400;\">Step 1 &#8211; Find RSD endpoint using Link Element :<\/span><\/i><\/p>\n<pre class=\"lang:default decode:true\">&lt;link rel=\"EditURI\" type=\"application\/rsd+xml\" title=\"RSD\" href=\"http:\/\/example.com\/xmlrpc.php?rsd\" \/&gt;\r\n\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><i><span style=\"font-weight: 400;\">Step 2 &#8211; Fetch RSD endpoint:<\/span><\/i><\/p>\n<pre class=\"lang:default decode:true\">&lt;?xml version=\"1.0\" encoding=\"utf-8\"?&gt;\r\n&lt;rsd version=\"1.0\" xmlns=\"http:\/\/archipelago.phrasewise.com\/rsd\"&gt;\r\n\u00a0&lt;service&gt;\r\n&lt;engineName&gt;WordPress&lt;\/engineName&gt;\r\n&lt;engineLink&gt;https:\/\/wordpress.org\/&lt;\/engineLink&gt;\r\n&lt;homePageLink&gt;http:\/\/example.com\/&lt;\/homePageLink&gt;\r\n&lt;apis&gt;\r\n\u00a0 &lt;api name=\"WordPress\" blogID=\"1\" preferred=\"true\" apiLink=\"http:\/\/example.com\/xmlrpc.php\" \/&gt;\r\n\u00a0 &lt;!-- ... --&gt;\r\n\u00a0 &lt;api name=\"WP-API\" blogID=\"1\" preferred=\"false\" apiLink=\"http:\/\/example.com\/wp-json\/\" \/&gt;\r\n&lt;\/apis&gt;\r\n\u00a0&lt;\/service&gt;\r\n&lt;\/rsd&gt;<\/pre>\n<h3><\/h3>\n<h3 style=\"text-align: justify;\"><b>Identify API Capability:<\/b><\/h3>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">In this step, the web application checks the configuration and sees what functions can be performed using the provided API, for example, sync data, update theme files, identify plugin support subscriptions, etc.<\/span><\/p>\n<pre class=\"lang:default decode:true\">{\r\n\"name\": \"Example WordPress Site\",\r\n\"namespaces\": [\r\n\u00a0\u00a0\u00a0 \"wp\/v2\",\r\n\u00a0\u00a0\u00a0 \"oembed\/1.0\/\"\r\n]\r\n}<\/pre>\n<h2 style=\"text-align: justify;\"><b>Conclusion:<\/b><\/h2>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">Representational State Transfer API holds immense potential for the future, and it has forced WordPress Stakeholders to start using it. REST API is already being included in the WordPress Core, and now Theme and Plugin developers have started to use API for theme and plugin maintenance, data integration, and other application requests.<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">The REST API has transformed WordPress from a simple CMS to a fully equipped web application development platform. It is still in its nascent stage but the dedicated development community is surely going to harness its full potential. It is a vast concept and you need to practice it to understand it completely, and what you can do with it solely depends on your imagination.<\/span><\/p>\n<p><script type=\"application\/ld+json\">\n    {\n        \"@context\": \"https:\/\/schema.org\",\n        \"@type\": \"FAQPage\",\n        \"mainEntity\": [\n                    {\n                \"@type\": \"Question\",\n                \"name\": \"What is API?\",\n                \"acceptedAnswer\": {\n                    \"@type\": \"Answer\",\n                    \"text\": \"Web Applications are no more a standalone entity. They need to communicate with third-party entities for better outcomes and instant real-time data. In order to communicate with third-party components, they need a medium and protocol to do that.\nSuch defined protocols are known as API, or Application Programming Interface. Through this interface, two independent entities communicate with each other and deliver the desired outcome.\nLet us understand this with an example from the travel domain. There are hundreds of independent airline websites allowing their customers to book tickets in real-time. But there are websites like Skyscanner where results from all the different websites are collected, compared, and displayed to the user. In this case, Skyscanner collects data from these hundreds or thousands of websites using a defined component, which is knowns as its Application Programming Interface or API.\nSimilarly, there are weather apps on a smartphone that gets the data from local servers from various cities across the world. The whole of this process is handled by one or the other API\nIt acts as a strong tool for application developers to interact with another software program.\"\n                                    }\n            }\n            ,\t            {\n                \"@type\": \"Question\",\n                \"name\": \"What is REST API?\",\n                \"acceptedAnswer\": {\n                    \"@type\": \"Answer\",\n                    \"text\": \"Representational State Transfer API is also popularly known as REST API. It is one of the architectural styles used in web programming. One of the other architectural styles is the Simple Object Access Protocol, but REST is more popular because it consumes less bandwidth and hence responds faster in case of the web application. It is compatible with ASP, PHP, Java, Ruby, Android, etc.\nAll the popular browsers use REST and that makes it suitable for Cloud-based web applications. REST API breaks down the whole process into various transactional modules making it easy for developers.\nAs discussed with the weather app example in the previous section when applications send a data request to various servers, the servers will respond accordingly to send the requested data in the prescribed format (XML or JSON). But processing and sending XML or JSON files is time-consuming and not as real-time as it should be.\nTo overcome this server creates an object and converts that object into a representational state, and the state is being transferred to the client (application) in the form of data.\nThere are 4 stages of resource processing that any web application goes through (also known as CRUD operations). Create, Read, Update and Delete, or there HTTP equivalents like POST, GET, PUT AND DELETE.\nThe two popular methods of implementing RESTful APIs are Jersey and Sparing.\nThe most popular usage of REST APIs is in Mobile\/cloud applications, social networking websites, and automated business process applications. The APIs are cache-friendly so they are more mobile-friendly owing to speed and efficiency.<\/p>\n<p>\"\n                                    }\n            }\n             , {\n                \"@type\": \"Question\",\n                \"name\": \"What is REST API in WordPress?\",\n                \"acceptedAnswer\": {\n                    \"@type\": \"Answer\",\n                    \"text\": \"WordPress Representational State Transfer (REST) API was started as a separate plugin. It was first developed in June 2013 by Ryan McCue and Rachel Baker and was included in the core WP update in 2015. It gains huge popularity and hundreds of developers came forward to improve it further, and that is when it completely became part of the core WordPress since version 4.7 (in 2016). This development supported the aim of WordPress to become a complete framework package for web developers.\nInclusion of REST API within the core framework enabled WordPress to communicate, integrate and exchange data with other external applications and servers while keeping the data processing operations separate from the front end interface, and thus making it fast. Later it\u2019s JavaScript\/JSON (JavaScript Object Notation) compatible version was also developed and it is known as JSON REST API, which makes it even easier to communicate with applications built in other languages.\nThe core advantage of WordPress REST API is that developers don\u2019t have to code the applications from scratch in PHP, they can get their WordPress program to interact with any other program that can make HTTP requests and interpret JSON. The other advantage is making the programs more fast and responsive for end-user as WordPress REST API simplifies AJAX calls hence fetching the data faster than before.\n\"\n                                    }\n            }           \t        ]\n    }\n<\/script><\/p>\n","protected":false},"excerpt":{"rendered":"<p>You mush have heard the term API if you work in a technical domain. It can be a little confusing, In this guide, we have tried to clear all your doubts so that you can easily talk about API with your team and colleagues.<br \/>\n&nbsp;<br \/>\nWhat is API?<br \/>\nWeb Applications are no more a standalone entity. They need to communicate with third-party entities for better outcomes and instant real-time data. In order to communicate with third-party components, &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.wpoven.com\/blog\/the-wordpress-rest-api-getting-started-guide\/\" class=\"more-link\">Read More <i class=\"fa fa-angle-double-right\" aria-hidden=\"true\"><\/i><span class=\"screen-reader-text\"> &#8220;The WordPress REST API : Getting Started Guide&#8221;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":5627,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"ub_ctt_via":"","footnotes":""},"categories":[13],"acf":[],"featured_image_src":"https:\/\/www.wpoven.com\/blog\/wp-content\/uploads\/2019\/02\/The-WordPress-REST-API-Getting-started-Guide1.jpg","author_info":{"display_name":"Vikrant Datta","author_link":"https:\/\/www.wpoven.com\/blog\/author\/vikrant\/"},"_links":{"self":[{"href":"https:\/\/www.wpoven.com\/blog\/wp-json\/wp\/v2\/posts\/3615"}],"collection":[{"href":"https:\/\/www.wpoven.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wpoven.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wpoven.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wpoven.com\/blog\/wp-json\/wp\/v2\/comments?post=3615"}],"version-history":[{"count":4,"href":"https:\/\/www.wpoven.com\/blog\/wp-json\/wp\/v2\/posts\/3615\/revisions"}],"predecessor-version":[{"id":25985,"href":"https:\/\/www.wpoven.com\/blog\/wp-json\/wp\/v2\/posts\/3615\/revisions\/25985"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wpoven.com\/blog\/wp-json\/wp\/v2\/media\/5627"}],"wp:attachment":[{"href":"https:\/\/www.wpoven.com\/blog\/wp-json\/wp\/v2\/media?parent=3615"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wpoven.com\/blog\/wp-json\/wp\/v2\/categories?post=3615"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}