Upgrade to Pro — share decks privately, control downloads, hide ads and more …

WP API, What is it good for? Absolutely Everything! - WordCamp Raleigh 2016

Evan Mullins
September 24, 2016

WP API, What is it good for? Absolutely Everything! - WordCamp Raleigh 2016

See the Power of the WP API. Now that every WordPress website has (or will have) an API built-in, what can you do with it? It allows us to further separate the data from the code. Use WordPress to manage our data and then via the API easily access or update that data to power whatever we like. We’ll touch how to set it up and a handful of examples and then explore an iOS app pulling all it’s data and assets from a WordPress site via this API.

This will be geared for developers with some “how to” but also for everyone interested in the power of WordPress and where things are heading.

Learn how to spell WP-API
Learn about the power and flexibility it brings to WordPress
See it working in a live app

Evan Mullins

September 24, 2016
Tweet

More Decks by Evan Mullins

Other Decks in Programming

Transcript

  1. Introductions Evan Mullins Lead Web Developer Brown Bag Marketing @circlecube

    circlecube.com WordPress user since 2006 Full-time web developer since 2007
  2. API An application programming interface (API) is a set of

    subroutine definitions, protocols, and tools for building software and applications. A good API makes it easier to develop a program by providing all the building blocks, which are then put together by the programmer.
  3. REST REpresentational State Transfer It describes how one system can

    communicate state with another. One example would be the state of a product (its name, description etc) represented as JSON. The generalised idea of state is termed a resource.
  4. JSON JavaScript Object Notation is a lightweight data-interchange format. It

    is easy for humans to read and write. It is easy for machines to parse and generate. It is a text format that is completely language independent but uses conventions that are familiar to programmers. These properties make JSON an ideal data-interchange language. JSON is built on two structures: • A collection of name/value pairs. (object). • An ordered list of values. (array).
  5. A little history Ryan McCue started development on an API,

    and then proposed a WordPress JSON REST API Project for GSOC in April 2013 (when WordPress was on version 3.5). The current version 2.0 of the plugin is in beta state and is already partially included (the infrastructure) in the WordPress core in version 4.4 (December 2015).
  6. A little history The rest of the REST API is

    slated for inclusion in version 4.7 after considerable discussion and planning (it was originally intended for 4.5). It was delayed in order to build it out with more endpoints. Until merging this API into core (hopefully in 4.7 - December 2016-ish), we’ll need to install the REST API feature Plugin.
  7. Nutshell So in a nutshell, rather than getting your content

    or data via a webpage as part of a website with php, html, css and javascript, you can use whatever you want and retrieve your data via the API. You’ll get json data that is compact and fast to transfer and then you can do endless things with it. Create an app, load it into another website, analyze it as data… it’s a great step for WordPress and a great step to continuing democratizing the web.
  8. Tools Postman - HTTP or API client Also: REST Easy

    for Firefox or httpie for the command line
  9. Back to WordPress The plugin/API exposes your data in JSON

    format in the following content types: Posts Pages Media Custom Post Types Post Meta Revisions Comments Terms Users
  10. Routes / Endpoints Endpoints are functions available through the API

    and are simply urls. This can be things like retrieving the API index, updating a post, or deleting a comment. Endpoints perform a specific function, taking some number of parameters and return data to the client. A route is the “name” you use to access endpoints, used in the URL. A route can have multiple endpoints associated with it, and which is used depends on the HTTP verb.
  11. Routes / Endpoints Example With the URL http://example.com/wp-json/wp/v2/posts/123 The “route”

    is wp/v2/posts/123 - The route doesn’t include wp-json because wp-json is the base path for the API itself. This route has 3 endpoints: • GET triggers a get_item method, returning the post data to the client. • PUT triggers an update_item method, taking the data to update, and returning the updated post data. • DELETE triggers a delete_item method, returning the now-deleted post data to the client.
  12. GET /wp/v2/posts get list of latest posts GET /wp/v2/posts/<id> retrieve

    a single post V1: https://2016.raleigh.wordcamp.org/wp-json/posts V2: http://demo.wp-api.org/wp-json/wp/v2/posts V2: http://demo.wp-api.org/wp-json/wp/v2/posts/470 GET - to read data
  13. Standard arguments as you’d expect and more One Example: filter

    Use WP Query arguments to modify the response; private query vars require appropriate authorization. GET
  14. PUT - to create or update data POST /wp/v2/posts create

    post POST /wp/v2/posts/<id> update existing post
  15. Authentication If you are building a theme or a plugin

    and want to access the API of the site you’re on you’ll want to authenticate with a cookie. WordPress already does this, so you’ll just want to check if the user has permissions to do what you’re attempting to do. Use a nonce (WordPress security token) to connect to a local API if your theme or plugin wants to connect to the API of the site it’s on via ajax.
  16. Authentication OAuth authentication is the main authentication handler used for

    external clients. It requires installing the OAuth plugin on the site which then handles authorizations and tokens. For examples on how to use OAuth Authentication checkout the Demo PHP API Client, the CLI client or the API console.
  17. Extend - Do More Modifying Responses Adding Endpoints Custom Content

    Types $args = array( 'show_in_rest' => true );
  18. US Presidential Mobile App An app will test your knowledge

    and teach you the Presidents of the USA – powered with WordPress via the REST API.
  19. USMNT Soccer App A similar app powered by the WordPress

    REST API, but with data centering around the US Soccer team.
  20. Sitemapper An in-house plugin at Brown Bag Marketing to quickly

    get up and running with a new WordPress site in our prototyping and wireframing multisite. Construct your sitemap and the plugin will create a new site (in the multisite network) and via the API create new pages according to the supplied sitemap.
  21. Digital Dashboard Angular web app built at Brown Bag Marketing

    to display live stats about a bunch of sites. Connects to sites like pingdom, analytics etc, . The sites are stored in WordPress as a CPT and loaded dynamically and it has featured content that’s pulled from the WP API.