Sie sind auf Seite 1von 29

An Introduction to

WordPress Rest API

Prakash Singh
Wordpress Developer

@PrakashSingh_09 WordCamp Kolkata, 2019


Discussion Points
1. What is REST API?
2. Routes and Endpoints.
3. WordPress Rest API Authentication.
4. WordPress Predefined API Endpoints.
5. Create your own API Endpoints.

@PrakashSingh_09 WordCamp Kolkata, 2019


What is REST API?
● WP API
● WordPress JSON API
● WordPress REST API
● WordPress JSON REST API

The WP API is a RESTful API that can be accessed through simple HTTP
request to access the site data in JSON format.

The WP API is a new way of accessing the data WordPress provides without
having to go through a theme,RSS feed, or XML RPC.

@PrakashSingh_09 WordCamp Kolkata, 2019


Rest
REST is an acronym for REprentational State Transfer

REST is the software architecture of the web. A RESTfull application is typically


one that uses the standard verbs GET,POST,PUT and DELETE to retrieve and
send data from and to remote server.

Resource

RESTfull application use resources --essentially URIs -- to interface with


external system such as remote servers, and uses four verbs to perform
operations on the servers.

@PrakashSingh_09 WordCamp Kolkata, 2019


API
API is an acronym for the Application Programming
interface

An API is a set of routines,protocols,and tools for building


applications and interfaces.

@PrakashSingh_09 WordCamp Kolkata, 2019


RESTfull API
RESTfull API is an application programming interface that uses
resource URI to perform operation on a remote server through
verbs.

You can send a URI to the server, and get pure data back.

@PrakashSingh_09 WordCamp Kolkata, 2019


JSON
JSON is an acronym for JavaScript Object Notation .

It’s an open standard format to transmit data objects in the form of


attribute-value pairs for further processing.

JSON is typically used for asynchronous communication between


browser and servers.

All Together Now

The WP API is a RESTfull API WordPress that returns data objects in the
JSON format when provided with a route and an endpoints

@PrakashSingh_09 WordCamp Kolkata, 2019


Accessing the data from WordPress
(Older Way)
The older way to access the data from WordPress.

@PrakashSingh_09 WordCamp Kolkata, 2019


Accessing the data through WP
JSON API
WordPress JSON API open the new world to access the data from the WordPress.

@PrakashSingh_09 WordCamp Kolkata, 2019


WP API Support :CRUD

CREATE READ UPDATE DELETE

@PrakashSingh_09 WordCamp Kolkata, 2019


Key Concepts WP API
Routes/Endpoints Requests Responses

Schema Controller Classes

@PrakashSingh_09 WordCamp Kolkata, 2019


Routes/Endpoints
Endpoints

An Endpoints is a functions available through the API in the form of


the verbs GET,POST,PUT and DELETE.

An Endpoints performs a specific function by taking one or more


parameters that return the desiderd data to the client

@PrakashSingh_09 WordCamp Kolkata, 2019


Routes/Endpoints
Route

A Route is “name” that you used to access the available endpoints.

Normally the Route is the URL and the Endpoints is action performs on
the URL.

@PrakashSingh_09 WordCamp Kolkata, 2019


Understanding the Routes/Endpoints
with example
Let’s grab the data of a specific posts by post ID through the WP REST
API. http://demo.wp-api.org/wp-json/wp/v2/posts/ID

@PrakashSingh_09 WordCamp Kolkata, 2019


Requests/Responses
Requests: The WordPress REST API processes requests with a class named
WP_REST_Request. It’s a primary class in the WordPress REST API
infrastructure. It’s used to store and retrieve information for all of the
requests you make.

Responses: Responses are processed with the WP_REST_Response class.


A response is the data you receive from a request, as stated before. The API
uses this class to return data sent from endpoints. It can also return errors.

https://developer.wordpress.org/rest-api/requests/

@PrakashSingh_09 WordCamp Kolkata, 2019


Schema / Controller Classes
Schemas: These are the templates responses follow, so you always
know exactly where to look for the right data.Each endpoint requires
and provides slightly different data structures, and those structures
are defined in the API Schema.

Controller classes: In basic terms, It enable you to build your own


routes and endpoints.With a controller class you can manage the
registration of routes & endpoints, handle requests, utilize schema,
and generate API responses.

@PrakashSingh_09 WordCamp Kolkata, 2019


WordPress Rest Api authentication
In the context of Information and Communications Technology (ICT),
authentication is the idea and process of verifying the credentials of the
person or entity that asks access to a particular system.

In the particular context of WordPress REST API, an authenticated user can


carry out CRUD tasks.

@PrakashSingh_09 WordCamp Kolkata, 2019


Authentication With the WordPress
REST API
The WordPress REST API offers several options for authentication

Basic Authentication

OAuth Authentication

Cookie Authentication

https://github.com/WP-API/Basic-Auth

https://github.com/WP-API/OAuth1

@PrakashSingh_09 WordCamp Kolkata, 2019


Basic Authentication
Basic authentication refers to the basic type of HTTP authentication in which
login credentials are sent along with the headers of the request.

In Basic Authentication, the client requests a URL that requires verification. The
server, in turn, requests the client to identify itself by sending a 401 Not
Authorized code. In reply, the client sends the same request with the
credentials(in the username:password pair) in header field in base64 strings.
Since base64 strings could be decoded without much effort, this authentication
method is not very much secure.

https://github.com/WP-API/Basic-Auth

@PrakashSingh_09 WordCamp Kolkata, 2019


WordPress Predefined API Endpoints
Here are some predefined API Endpoints

● Posts wp-json/wp/v2/posts
● Categories wp-json/wp/v2/categories
● Pages wp-json/wp/v2/pages
● Comments wp-json/wp/v2/comments
● Users wp-json/wp/v2/users

https://developer.wordpress.org/rest-api/reference/

@PrakashSingh_09 WordCamp Kolkata, 2019


POST
Endpoint
Responses

@PrakashSingh_09 WordCamp Kolkata, 2019


Creating Custom Endpoints for the
WordPress REST API

The WordPress REST API provides you with more than just a set of built-in
routes. You can also create custom routes and endpoints using the same
APIs used to create default routes (for example, the register_rest_route()
function and the WP_Rest_Controller class etc.).

@PrakashSingh_09 WordCamp Kolkata, 2019


Taking an example Creating
Custom Endpoints to create
User

@PrakashSingh_09 WordCamp Kolkata, 2019


Taking an example Creating Custom
Endpoints to create User
WordPress provides us with hooks and functions to do the needful.
Taking an example, we have written a patch of code to register a new
route that allows us to create user.

As the name suggests, register_rest_route is the function that we need to


use to register a new route with WordPress. This function needs to be
added in a function attached to the rest_api_init hook.

@PrakashSingh_09 WordCamp Kolkata, 2019


Taking an example Creating Custom
Endpoints to create User
The first argument in the register_rest_route function is the namespace.
This namespace helps us to group together all our routes.

It is extremely important to add namespaces to your routes. The “core”


endpoints use the wp/v2 namespace. The first part of the namespace is
wp, which represents the vendor name; WordPress and v2 is the version
number. We’re going to use my-route as the namespace here.

@PrakashSingh_09 WordCamp Kolkata, 2019


Taking an example Creating Custom
Endpoints to create User
The second argument is the resource path. The resource path signifies
what resource the endpoint is associated with (like posts, comments
and so on). We will name it createuser.

The third argument is an array of options. Here we specify what


methods the endpoint can use and callback functions for each
method. This argument also allows us to provide a permissions
callback, which can restrict access for the endpoint to certain users.

@PrakashSingh_09 WordCamp Kolkata, 2019


Taking an example Creating Custom
Endpoints to create User
Once this endpoint is created, you can access it at the below url
https://example.com/wp-json/my-route/createuser?email_id=<email_id>

We can register user by this API as a subscriber role. It you would mail
your logging in details in the mail as well.

@PrakashSingh_09 WordCamp Kolkata, 2019


Points to remember
● To Change main API route (/wp-json/wp/v2/)
Filter Hook rest_url_prefix
● In order to get WP API working you need to set your permalink as
Post name.
● For custom post type,if you want it to be available via the REST API
you should set 'show_in_rest' => true in the arguments passed to
register_post_type dfdf

@PrakashSingh_09 WordCamp Kolkata, 2019


Thank You!

Prakash Singh
Wordpress Developer

@PrakashSingh_09 WordCamp Kolkata, 2019

Das könnte Ihnen auch gefallen