Saotn.org now provides the WordPress REST API (WP API), for you to use my posts to fill your website. How great is that?! :) But what is a REST API exactly, and what can I do with it?
The WordPress REST API is – currently – available as a plugin that allows applications to access the data produced by WordPress in JSON format. This opens up a whole new area for WordPress powered websites to share their contents, or incorperate content from others. Not to mention new possiblities in themes and plugins.
From the WP REST API website:
This plugin provides an easy to use REST API, available via HTTP. Grab your site’s data in simple JSON format, including users, posts, taxonomies and more. Retrieving or updating data is as simple as sending a HTTP request.
Want to get your site’s posts? Simply send a
/wp-json/posts. Update user with ID 4? Send a
/wp-json/users/4. Get all posts with the search term “
GET /wp-json/posts?filter[s]=awesome. It’s that easy.
WP API exposes a simple yet easy interface to WP Query, the posts API, post meta API, users API, revisions API and many more. Chances are, if you can do it with WordPress, WP API will let you do it.
For update actions you need to be authenticated of course, so you only have read/GET access on my content. A PHP example:
<?php $options = array ( 'http' => array ( 'ignore_errors' => true, ), ); $context = stream_context_create( $options ); $response = file_get_contents( 'https://www.saotn.org/wp-json/posts?type=post', false, $context ); $response = json_decode( $response ); ?>
Or to grab a specific post ID (2037):
<?php $options = array ( 'http' => array ( 'ignore_errors' => true, ), ); $context = stream_context_create( $options ); $response = file_get_contents( 'https://www.saotn.org/wp-json/posts/2037', false, $context ); $response = json_decode( $response ); ?>
Developer resources can be found here: https://developer.wordpress.com/docs/api/.
Various demo applications, plugins and articles are available for you to get more information and to build on. For example:
- Using the WordPress REST API in a mobile app (AppPresser)
- Processing forms with ajax using the JSON REST API (Torque)
- Working with taxonomies using the JSON REST API (Torque)
- Using AngularJS and JSON API in your WordPress theme (1Fix.io)
- Bringing 24% of the Internet to scale (lara littlefield)
If you decide to let your app talk to my API URL I would appreciate a heads-up.