SlideShare a Scribd company logo
Introduction to the Pods 
JSON API 
Josh Pollock, @josh412 
Building Applications: Introduction to the Pods JSON API // Josh Pollock // PodsCamp 2014
What Is The WordPress REST API? 
A really easy way to move data between sites or inside of a 
site using the standardized JSON format. 
Currently a plugin, Hopefully WordPress 4.1 
http://wp-api.org 
https://speakerdeck.com/rmccue/wcmke2014 
Building Applications: Introduction to the Pods JSON API // Josh Pollock // PodsCamp 2014
Helpful Functions 
json_url() 
● REST API Root URL 
● REST API 
add_query_arg() 
● Add arguments to URLs 
● WordPress 
json_encode() 
● Convert PHP to JSON 
● PHP 
json_decode() 
● Convert JSON to PHP 
● PHP 
Building Applications: Introduction to the Pods JSON API // Josh Pollock // PodsCamp 2014
The Pods JSON API 
Extends the WordPress REST API 
Routes for: 
● Pods 
● Pods API 
● Pods Components 
https://github.com/pods-framework/pods-json-api 
Building Applications: Introduction to the Pods JSON API // Josh Pollock // PodsCamp 2014
Authentication Options 
● Basic Authentication 
● Nonce/Cookie 
● Key pairs 
● oAuth1 
Building Applications: Introduction to the Pods JSON API // Josh Pollock // PodsCamp 2014
Access Filters In Pods JSON API 
Endpoints in Pods 
apply_filters( 'pods_json_api_access_pods_' . $method, $access, $method, $pod, $item ); 
apply_filters( 'pods_json_api_access_pods', $access, $method, $pod, $item ); 
Endpoints in Pods API 
apply_filters( 'pods_json_api_access_api_' . $method, $access, $method ); 
apply_filters( 'pods_json_api_access_api', $access, $method ); 
Endpoints in Components 
apply_filters( 'pods_json_api_access_components_' . $method, $access, $method ); 
apply_filters( 'pods_json_api_access_components', $access, $method ); 
Building Applications: Introduction to the Pods JSON API // Josh Pollock // PodsCamp 2014
GET vs POST 
RESTful APIs use the basic HTTP methods: 
GET POST PUT DELETE 
We will be using GET to get items and POST to create items. 
Building Applications: Introduction to the Pods JSON API // Josh Pollock // PodsCamp 2014
Capabilities of The Pods JSON API 
○ Show Pods and content 
○ Save Pods 
○ Create Pods and Fields 
○ Import a Pods Package 
○ Activate/ deactivate components 
Building Applications: Introduction to the Pods JSON API // Josh Pollock // PodsCamp 2014
The WordPress HTTP API 
A simple PHP API in WordPress for making HTTP requests. 
Helpful functions such as: 
wp_remote_get() 
http://codex.wordpress.org/Function_Reference/wp_remote_get 
wp_remote_retrieve_body() 
http://codex.wordpress.org/Function_API/wp_remote_retrieve_body 
Building Applications: Introduction to the Pods JSON API // Josh Pollock // PodsCamp 2014
Getting Pods Items 
Make a GET request to 
<json-url>/pods/<pod> 
or 
<json-url>/pods/<pod>/<id> 
$headers = array ( 
'Authorization' => 'Basic ' . base64_encode( 'username' 
. ':' . 'password' ), 
); 
$url = json_url( 'pods/jedi' ); 
$response = wp_remote_post( $url, array ( 
'method' => 'GET', 
'headers' => $headers, 
) 
); 
//make sure response isn't an error 
if ( ! is_wp_error( $response ) ) { 
//show the updated post item 
var_dump( wp_remote_retrieve_body( $response ) ); 
} 
Building Applications: Introduction to the Pods JSON API // Josh Pollock // PodsCamp 2014
Using Pods Find 
Add the parameters to the URL, 
using add_query_arg() 
$headers = array ( 
'Authorization' => 'Basic ' . base64_encode( 'username' . ':' 
. 'password' ), 
); 
$url = json_url( 'pods/jedi' ); 
$params = array( 
'home_world.post_title' => 'Tatooine' 
); 
$url = add_query_arg( $params, $url ); 
$response = wp_remote_post( $url, array ( 
'method' => 'GET', 
'headers' => $headers, 
) 
); 
//make sure response isn't an error 
if ( ! is_wp_error( $response ) ) { 
//show the updated post item 
var_dump( wp_remote_retrieve_body( $response ) ); 
} 
Building Applications: Introduction to the Pods JSON API // Josh Pollock // PodsCamp 2014
Saving Pods Items 
Make POST request to 
New item: 
<json-url>/<pod> 
Update item: 
<json-url>/<pod>/<id> 
$data = array( 'home_planet' => 'Alderann' ); 
$url = json_url( 'pods/jedi/9' ); 
$headers = array ( 
'Authorization' => 'Basic ' . base64_encode( 'username' . ':' . 
'password' ), 
); 
$response = wp_remote_post( $url, array ( 
'method' => 'POST', 
'headers' => $headers, 
'body' => json_encode( $data ) 
) 
); 
//make sure response isn't an error 
if ( ! is_wp_error( $response ) ) { 
//show the updated post item 
var_dump( wp_remote_retrieve_body( $response ) ); 
} 
Building Applications: Introduction to the Pods JSON API // Josh Pollock // PodsCamp 2014
Creating Pods 
POST to <json-url>/<pods-api> 
Body of request passed to 
PodsAPI->save_pod() 
$data = array( 
'name' => 'jedi', 
'type' => 'post_type', 
); 
$url = json_url( 'pods-api' ); 
$headers = array ( 
'Authorization' => 'Basic ' . base64_encode( 'username' . ':' . 
'password' ), 
); 
$response = wp_remote_post( $url, array ( 
'method' => 'POST', 
'headers' => $headers, 
'body' => json_encode( $data ) 
) 
); 
//make sure response isn't an error 
if ( ! is_wp_error( $response ) ) { 
//show the updated post item 
var_dump( wp_remote_retrieve_body( $response ) ); 
} 
Building Applications: Introduction to the Pods JSON API // Josh Pollock // PodsCamp 2014
Update Pods 
Same as before but use: 
<json-url>/<pods-api>/<pod-name> 
or 
<json-url>/<pods-api>/<pod-id> 
Building Applications: Introduction to the Pods JSON API // Josh Pollock // PodsCamp 2014
AJAX Time! 
GET or POST data asynchronously, and render it in the 
browser. 
Make your site more dynamic and “app” like. 
Building Applications: Introduction to the Pods JSON API // Josh Pollock // PodsCamp 2014
Using The REST API Client-JS 
Provides Backbone collections and models for all REST API 
endpoints. 
No Pods integration, but… 
Gives us an easy way to handle authentication 
https://github.com/WP-API/client-js 
Building Applications: Introduction to the Pods JSON API // Josh Pollock // PodsCamp 2014
Preparing To Use Client JS 
Enqueue The Script 
Localize a nonce and the root 
JSON url. 
add_action( 'wp_enqueue_scripts', 'json_api_client_js' 
); 
add_action( 'wp_enqueue_scripts', 
'json_api_talk_scripts' ); 
function json_api_talk_scripts() { 
if ( ! function_exists( 'json_get_url_prefix' ) ) { 
return; 
} 
wp_enqueue_script( 'json-api-talk', plugins_url( 
'/json-api-talk.js', __FILE__ ), array( 'jquery' ), 
'1.0', true ); 
} 
Building Applications: Introduction to the Pods JSON API // Josh Pollock // PodsCamp 2014
Setup Variables From Localize Data 
(function($){ 
//root JSON URL 
var root_URL = WP_API_Settings.root; 
//API nonce 
var api_NONCE = WP_API_Settings.nonce; 
//Pods endpoint URL 
var pods_URL = WP_API_Settings + 'pods'; 
})(jQuery); 
Prepare URLs and nonce 
from localized data for 
use in functions. 
Building Applications: Introduction to the Pods JSON API // Josh Pollock // PodsCamp 2014
Get Items Via AJAX 
function getItem( id, pod ) { 
var URL = pods_URL + '/' + pod + '/' + 'id'; 
$.ajax({ 
type:"GET", 
url: url, 
dataType : 'json', 
beforeSend : function( xhr ) { 
xhr.setRequestHeader( 'X-WP-Nonce', api_Nonce ); 
}, 
success: function(response) { 
//do something 
} 
}); 
} 
Building Applications: Introduction to the Pods JSON API // Josh Pollock // PodsCamp 2014
Save Items Via AJAX 
function saveItem( id, pod ) { 
var save_url = podsURL + '/' + pod + '/' + 'id'; 
var title = ''; 
var home_planet = ''; 
var lightsaber_color = ''; 
var JSONObj = { 
"title" : title, 
"home_planet" : home_planet, 
'lightsaber_color' : lightsaber_color, 
"status" : 'publish' 
}; 
//encode data as JSON 
var data = JSON.stringify( JSONObj ); 
$.ajax({ 
type:"POST", 
url: save_url, 
dataType : 'json', 
data: data, 
beforeSend : function( xhr ) { 
xhr.setRequestHeader( 'X-WP-Nonce', apiNonce ); 
}, 
success: function(response) { 
alert( 'WOO!'); 
} 
}); 
} 
Building Applications: Introduction to the Pods JSON API // Josh Pollock // PodsCamp 2014
Render With A Handlebars Template 
function getItem( id, pod, templateID, containerID ) { 
var get_url = podsURL + '/' + pod + '/' + 'id'; 
$.ajax({ 
type:"GET", 
url: get_url, 
dataType : 'json', 
beforeSend : function( xhr ) { 
xhr.setRequestHeader( 'X-WP-Nonce', apiNonce ); 
}, 
success: function(response) { 
var source = $( templateID ).html(); 
var template = Handlebars.compile( source ); 
var html = template( data ); 
$( container ).append( html ); 
} 
}); 
} 
Building Applications: Introduction to the Pods JSON API // Josh Pollock // PodsCamp 2014
Non WordPress Front-ends 
Angular Client For Pods API 
https://github.com/bjoernklose/angular-wordpress-pods 
Using the WordPress REST API in a mobile app 
http://apppresser.com/using-wordpress-rest-api-mobile-app/ 
Building Applications: Introduction to the Pods JSON API // Josh Pollock // PodsCamp 2014
Questions? 
Building Applications: Introduction to the Pods JSON API // Josh Pollock // PodsCamp 2014

More Related Content

What's hot (20)

PPTX
Document your rest api using swagger - Devoxx 2015
johannes_fiala
 
PDF
Workshop 4: NodeJS. Express Framework & MongoDB.
Visual Engineering
 
PDF
Full Text Search In PostgreSQL
Karwin Software Solutions LLC
 
PPTX
Introduction to WordPress Development - Hooks
Edmund Chan
 
PDF
Frans Rosén Keynote at BSides Ahmedabad
Security BSides Ahmedabad
 
PDF
Securite des Web Services (SOAP vs REST) / OWASP Geneva dec. 2012
Sylvain Maret
 
PPTX
Express js
Manav Prasad
 
PDF
An Overview of Deserialization Vulnerabilities in the Java Virtual Machine (J...
joaomatosf_
 
ODP
ES6 PPT FOR 2016
Manoj Kumar
 
PDF
How to steal and modify data using Business Logic flaws - Insecure Direct Obj...
Frans Rosén
 
PDF
OWASP-VulnerableFlaskApp
anilyelken
 
PDF
Session 2 - NGSI-LD primer & Smart Data Models | Train the Trainers Program
FIWARE
 
PDF
Exception handling
Anna Pietras
 
PDF
Introducing Async/Await
Valeri Karpov
 
PPTX
Angular 2.0 Pipes
Eyal Vardi
 
PPTX
Promises, Promises
Domenic Denicola
 
PDF
Routing in NEXTJS.pdf
AnishaDahal5
 
PDF
SQL Injection INSERT ON DUPLICATE KEY trick
Mathias Karlsson
 
PPTX
Local SQLite Database with Node for beginners
Laurence Svekis ✔
 
PPTX
Scrapy.for.dummies
Chandler Huang
 
Document your rest api using swagger - Devoxx 2015
johannes_fiala
 
Workshop 4: NodeJS. Express Framework & MongoDB.
Visual Engineering
 
Full Text Search In PostgreSQL
Karwin Software Solutions LLC
 
Introduction to WordPress Development - Hooks
Edmund Chan
 
Frans Rosén Keynote at BSides Ahmedabad
Security BSides Ahmedabad
 
Securite des Web Services (SOAP vs REST) / OWASP Geneva dec. 2012
Sylvain Maret
 
Express js
Manav Prasad
 
An Overview of Deserialization Vulnerabilities in the Java Virtual Machine (J...
joaomatosf_
 
ES6 PPT FOR 2016
Manoj Kumar
 
How to steal and modify data using Business Logic flaws - Insecure Direct Obj...
Frans Rosén
 
OWASP-VulnerableFlaskApp
anilyelken
 
Session 2 - NGSI-LD primer & Smart Data Models | Train the Trainers Program
FIWARE
 
Exception handling
Anna Pietras
 
Introducing Async/Await
Valeri Karpov
 
Angular 2.0 Pipes
Eyal Vardi
 
Promises, Promises
Domenic Denicola
 
Routing in NEXTJS.pdf
AnishaDahal5
 
SQL Injection INSERT ON DUPLICATE KEY trick
Mathias Karlsson
 
Local SQLite Database with Node for beginners
Laurence Svekis ✔
 
Scrapy.for.dummies
Chandler Huang
 

Viewers also liked (7)

PPTX
No More “Cowboy Coding”: A Best Practices Guide to Local Development & Migration
podsframework
 
PDF
Building a Rails API with the JSON API Spec
Sonja Peterson
 
PDF
Five events in the life of every WordPress request you should know
Caldera Labs
 
PDF
Ember Data and JSON API
yoranbe
 
PDF
Caldera Learn - LoopConf WP API + Angular FTW Workshop
CalderaLearn
 
PDF
Connecting Content Silos: One CMS, Many Sites With The WordPress REST API
Caldera Labs
 
PPTX
Build A Killer Client For Your REST+JSON API
Stormpath
 
No More “Cowboy Coding”: A Best Practices Guide to Local Development & Migration
podsframework
 
Building a Rails API with the JSON API Spec
Sonja Peterson
 
Five events in the life of every WordPress request you should know
Caldera Labs
 
Ember Data and JSON API
yoranbe
 
Caldera Learn - LoopConf WP API + Angular FTW Workshop
CalderaLearn
 
Connecting Content Silos: One CMS, Many Sites With The WordPress REST API
Caldera Labs
 
Build A Killer Client For Your REST+JSON API
Stormpath
 
Ad

Similar to Introduction to the Pods JSON API (20)

PDF
JSON REST API for WordPress
Taylor Lovett
 
PPTX
WordPress Rest API
Brian Layman
 
PDF
JSON REST API for WordPress
Taylor Lovett
 
PDF
Using the new WordPress REST API
Caldera Labs
 
PDF
WordCamp Raleigh 2016 - WP API, What is it good for? Absolutely Everything!
Evan Mullins
 
PPTX
The WordPress REST API as a Springboard for Website Greatness
WP Engine UK
 
PPTX
The JSON REST API for WordPress
Taylor Lovett
 
PDF
WordCamp Birmingham 2016 - WP API, What is it good for? Absolutely Everything!
Evan Mullins
 
PDF
WordCamp Wilmington 2017 WP-API Why?
Evan Mullins
 
PPTX
Using WordPress as your application stack
Paul Bearne
 
PDF
WordPress Tallahassee Meetup: Turning WordPress Sites Into Web & Mobile Apps
Caldera Labs
 
PDF
Old WP REST API, New Tricks
WordPress Community Montreal
 
PPTX
WP REST API - Building a simple Web Application
Edmund Chan
 
PPTX
Building native mobile apps with word press
Nikhil Vishnu P.V
 
PDF
SunShine PHP
David Bisset
 
PPTX
An Introduction to the WP REST API
Edmund Chan
 
PDF
Maine WordPress Meetup JSON REST API, 3/16/2016
Andre Gagnon
 
PPTX
Build Modern Web Applications with React and WordPress
Imran Sayed
 
PDF
WordPress REST API
Anthony Montalbano
 
PDF
WooCommerce & Apple TV
Marko Heijnen
 
JSON REST API for WordPress
Taylor Lovett
 
WordPress Rest API
Brian Layman
 
JSON REST API for WordPress
Taylor Lovett
 
Using the new WordPress REST API
Caldera Labs
 
WordCamp Raleigh 2016 - WP API, What is it good for? Absolutely Everything!
Evan Mullins
 
The WordPress REST API as a Springboard for Website Greatness
WP Engine UK
 
The JSON REST API for WordPress
Taylor Lovett
 
WordCamp Birmingham 2016 - WP API, What is it good for? Absolutely Everything!
Evan Mullins
 
WordCamp Wilmington 2017 WP-API Why?
Evan Mullins
 
Using WordPress as your application stack
Paul Bearne
 
WordPress Tallahassee Meetup: Turning WordPress Sites Into Web & Mobile Apps
Caldera Labs
 
Old WP REST API, New Tricks
WordPress Community Montreal
 
WP REST API - Building a simple Web Application
Edmund Chan
 
Building native mobile apps with word press
Nikhil Vishnu P.V
 
SunShine PHP
David Bisset
 
An Introduction to the WP REST API
Edmund Chan
 
Maine WordPress Meetup JSON REST API, 3/16/2016
Andre Gagnon
 
Build Modern Web Applications with React and WordPress
Imran Sayed
 
WordPress REST API
Anthony Montalbano
 
WooCommerce & Apple TV
Marko Heijnen
 
Ad

Recently uploaded (20)

PDF
Online Queue Management System for Public Service Offices in Nepal [Focused i...
Rishab Acharya
 
PDF
Alarm in Android-Scheduling Timed Tasks Using AlarmManager in Android.pdf
Nabin Dhakal
 
PPTX
Home Care Tools: Benefits, features and more
Third Rock Techkno
 
PDF
Linux Certificate of Completion - LabEx Certificate
VICTOR MAESTRE RAMIREZ
 
PDF
Generic or Specific? Making sensible software design decisions
Bert Jan Schrijver
 
PDF
Revenue streams of the Wazirx clone script.pdf
aaronjeffray
 
PDF
Why Businesses Are Switching to Open Source Alternatives to Crystal Reports.pdf
Varsha Nayak
 
PDF
iTop VPN With Crack Lifetime Activation Key-CODE
utfefguu
 
PDF
4K Video Downloader Plus Pro Crack for MacOS New Download 2025
bashirkhan333g
 
PDF
Alexander Marshalov - How to use AI Assistants with your Monitoring system Q2...
VictoriaMetrics
 
PDF
The 5 Reasons for IT Maintenance - Arna Softech
Arna Softech
 
PDF
Driver Easy Pro 6.1.1 Crack Licensce key 2025 FREE
utfefguu
 
PDF
Automate Cybersecurity Tasks with Python
VICTOR MAESTRE RAMIREZ
 
PDF
Top Agile Project Management Tools for Teams in 2025
Orangescrum
 
PDF
Wondershare PDFelement Pro Crack for MacOS New Version Latest 2025
bashirkhan333g
 
PDF
SAP Firmaya İade ABAB Kodları - ABAB ile yazılmıl hazır kod örneği
Salih Küçük
 
PDF
Digger Solo: Semantic search and maps for your local files
seanpedersen96
 
PPTX
Agentic Automation Journey Series Day 2 – Prompt Engineering for UiPath Agents
klpathrudu
 
PPTX
Homogeneity of Variance Test Options IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
PDF
유니티에서 Burst Compiler+ThreadedJobs+SIMD 적용사례
Seongdae Kim
 
Online Queue Management System for Public Service Offices in Nepal [Focused i...
Rishab Acharya
 
Alarm in Android-Scheduling Timed Tasks Using AlarmManager in Android.pdf
Nabin Dhakal
 
Home Care Tools: Benefits, features and more
Third Rock Techkno
 
Linux Certificate of Completion - LabEx Certificate
VICTOR MAESTRE RAMIREZ
 
Generic or Specific? Making sensible software design decisions
Bert Jan Schrijver
 
Revenue streams of the Wazirx clone script.pdf
aaronjeffray
 
Why Businesses Are Switching to Open Source Alternatives to Crystal Reports.pdf
Varsha Nayak
 
iTop VPN With Crack Lifetime Activation Key-CODE
utfefguu
 
4K Video Downloader Plus Pro Crack for MacOS New Download 2025
bashirkhan333g
 
Alexander Marshalov - How to use AI Assistants with your Monitoring system Q2...
VictoriaMetrics
 
The 5 Reasons for IT Maintenance - Arna Softech
Arna Softech
 
Driver Easy Pro 6.1.1 Crack Licensce key 2025 FREE
utfefguu
 
Automate Cybersecurity Tasks with Python
VICTOR MAESTRE RAMIREZ
 
Top Agile Project Management Tools for Teams in 2025
Orangescrum
 
Wondershare PDFelement Pro Crack for MacOS New Version Latest 2025
bashirkhan333g
 
SAP Firmaya İade ABAB Kodları - ABAB ile yazılmıl hazır kod örneği
Salih Küçük
 
Digger Solo: Semantic search and maps for your local files
seanpedersen96
 
Agentic Automation Journey Series Day 2 – Prompt Engineering for UiPath Agents
klpathrudu
 
Homogeneity of Variance Test Options IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
유니티에서 Burst Compiler+ThreadedJobs+SIMD 적용사례
Seongdae Kim
 

Introduction to the Pods JSON API

  • 1. Introduction to the Pods JSON API Josh Pollock, @josh412 Building Applications: Introduction to the Pods JSON API // Josh Pollock // PodsCamp 2014
  • 2. What Is The WordPress REST API? A really easy way to move data between sites or inside of a site using the standardized JSON format. Currently a plugin, Hopefully WordPress 4.1 http://wp-api.org https://speakerdeck.com/rmccue/wcmke2014 Building Applications: Introduction to the Pods JSON API // Josh Pollock // PodsCamp 2014
  • 3. Helpful Functions json_url() ● REST API Root URL ● REST API add_query_arg() ● Add arguments to URLs ● WordPress json_encode() ● Convert PHP to JSON ● PHP json_decode() ● Convert JSON to PHP ● PHP Building Applications: Introduction to the Pods JSON API // Josh Pollock // PodsCamp 2014
  • 4. The Pods JSON API Extends the WordPress REST API Routes for: ● Pods ● Pods API ● Pods Components https://github.com/pods-framework/pods-json-api Building Applications: Introduction to the Pods JSON API // Josh Pollock // PodsCamp 2014
  • 5. Authentication Options ● Basic Authentication ● Nonce/Cookie ● Key pairs ● oAuth1 Building Applications: Introduction to the Pods JSON API // Josh Pollock // PodsCamp 2014
  • 6. Access Filters In Pods JSON API Endpoints in Pods apply_filters( 'pods_json_api_access_pods_' . $method, $access, $method, $pod, $item ); apply_filters( 'pods_json_api_access_pods', $access, $method, $pod, $item ); Endpoints in Pods API apply_filters( 'pods_json_api_access_api_' . $method, $access, $method ); apply_filters( 'pods_json_api_access_api', $access, $method ); Endpoints in Components apply_filters( 'pods_json_api_access_components_' . $method, $access, $method ); apply_filters( 'pods_json_api_access_components', $access, $method ); Building Applications: Introduction to the Pods JSON API // Josh Pollock // PodsCamp 2014
  • 7. GET vs POST RESTful APIs use the basic HTTP methods: GET POST PUT DELETE We will be using GET to get items and POST to create items. Building Applications: Introduction to the Pods JSON API // Josh Pollock // PodsCamp 2014
  • 8. Capabilities of The Pods JSON API ○ Show Pods and content ○ Save Pods ○ Create Pods and Fields ○ Import a Pods Package ○ Activate/ deactivate components Building Applications: Introduction to the Pods JSON API // Josh Pollock // PodsCamp 2014
  • 9. The WordPress HTTP API A simple PHP API in WordPress for making HTTP requests. Helpful functions such as: wp_remote_get() http://codex.wordpress.org/Function_Reference/wp_remote_get wp_remote_retrieve_body() http://codex.wordpress.org/Function_API/wp_remote_retrieve_body Building Applications: Introduction to the Pods JSON API // Josh Pollock // PodsCamp 2014
  • 10. Getting Pods Items Make a GET request to <json-url>/pods/<pod> or <json-url>/pods/<pod>/<id> $headers = array ( 'Authorization' => 'Basic ' . base64_encode( 'username' . ':' . 'password' ), ); $url = json_url( 'pods/jedi' ); $response = wp_remote_post( $url, array ( 'method' => 'GET', 'headers' => $headers, ) ); //make sure response isn't an error if ( ! is_wp_error( $response ) ) { //show the updated post item var_dump( wp_remote_retrieve_body( $response ) ); } Building Applications: Introduction to the Pods JSON API // Josh Pollock // PodsCamp 2014
  • 11. Using Pods Find Add the parameters to the URL, using add_query_arg() $headers = array ( 'Authorization' => 'Basic ' . base64_encode( 'username' . ':' . 'password' ), ); $url = json_url( 'pods/jedi' ); $params = array( 'home_world.post_title' => 'Tatooine' ); $url = add_query_arg( $params, $url ); $response = wp_remote_post( $url, array ( 'method' => 'GET', 'headers' => $headers, ) ); //make sure response isn't an error if ( ! is_wp_error( $response ) ) { //show the updated post item var_dump( wp_remote_retrieve_body( $response ) ); } Building Applications: Introduction to the Pods JSON API // Josh Pollock // PodsCamp 2014
  • 12. Saving Pods Items Make POST request to New item: <json-url>/<pod> Update item: <json-url>/<pod>/<id> $data = array( 'home_planet' => 'Alderann' ); $url = json_url( 'pods/jedi/9' ); $headers = array ( 'Authorization' => 'Basic ' . base64_encode( 'username' . ':' . 'password' ), ); $response = wp_remote_post( $url, array ( 'method' => 'POST', 'headers' => $headers, 'body' => json_encode( $data ) ) ); //make sure response isn't an error if ( ! is_wp_error( $response ) ) { //show the updated post item var_dump( wp_remote_retrieve_body( $response ) ); } Building Applications: Introduction to the Pods JSON API // Josh Pollock // PodsCamp 2014
  • 13. Creating Pods POST to <json-url>/<pods-api> Body of request passed to PodsAPI->save_pod() $data = array( 'name' => 'jedi', 'type' => 'post_type', ); $url = json_url( 'pods-api' ); $headers = array ( 'Authorization' => 'Basic ' . base64_encode( 'username' . ':' . 'password' ), ); $response = wp_remote_post( $url, array ( 'method' => 'POST', 'headers' => $headers, 'body' => json_encode( $data ) ) ); //make sure response isn't an error if ( ! is_wp_error( $response ) ) { //show the updated post item var_dump( wp_remote_retrieve_body( $response ) ); } Building Applications: Introduction to the Pods JSON API // Josh Pollock // PodsCamp 2014
  • 14. Update Pods Same as before but use: <json-url>/<pods-api>/<pod-name> or <json-url>/<pods-api>/<pod-id> Building Applications: Introduction to the Pods JSON API // Josh Pollock // PodsCamp 2014
  • 15. AJAX Time! GET or POST data asynchronously, and render it in the browser. Make your site more dynamic and “app” like. Building Applications: Introduction to the Pods JSON API // Josh Pollock // PodsCamp 2014
  • 16. Using The REST API Client-JS Provides Backbone collections and models for all REST API endpoints. No Pods integration, but… Gives us an easy way to handle authentication https://github.com/WP-API/client-js Building Applications: Introduction to the Pods JSON API // Josh Pollock // PodsCamp 2014
  • 17. Preparing To Use Client JS Enqueue The Script Localize a nonce and the root JSON url. add_action( 'wp_enqueue_scripts', 'json_api_client_js' ); add_action( 'wp_enqueue_scripts', 'json_api_talk_scripts' ); function json_api_talk_scripts() { if ( ! function_exists( 'json_get_url_prefix' ) ) { return; } wp_enqueue_script( 'json-api-talk', plugins_url( '/json-api-talk.js', __FILE__ ), array( 'jquery' ), '1.0', true ); } Building Applications: Introduction to the Pods JSON API // Josh Pollock // PodsCamp 2014
  • 18. Setup Variables From Localize Data (function($){ //root JSON URL var root_URL = WP_API_Settings.root; //API nonce var api_NONCE = WP_API_Settings.nonce; //Pods endpoint URL var pods_URL = WP_API_Settings + 'pods'; })(jQuery); Prepare URLs and nonce from localized data for use in functions. Building Applications: Introduction to the Pods JSON API // Josh Pollock // PodsCamp 2014
  • 19. Get Items Via AJAX function getItem( id, pod ) { var URL = pods_URL + '/' + pod + '/' + 'id'; $.ajax({ type:"GET", url: url, dataType : 'json', beforeSend : function( xhr ) { xhr.setRequestHeader( 'X-WP-Nonce', api_Nonce ); }, success: function(response) { //do something } }); } Building Applications: Introduction to the Pods JSON API // Josh Pollock // PodsCamp 2014
  • 20. Save Items Via AJAX function saveItem( id, pod ) { var save_url = podsURL + '/' + pod + '/' + 'id'; var title = ''; var home_planet = ''; var lightsaber_color = ''; var JSONObj = { "title" : title, "home_planet" : home_planet, 'lightsaber_color' : lightsaber_color, "status" : 'publish' }; //encode data as JSON var data = JSON.stringify( JSONObj ); $.ajax({ type:"POST", url: save_url, dataType : 'json', data: data, beforeSend : function( xhr ) { xhr.setRequestHeader( 'X-WP-Nonce', apiNonce ); }, success: function(response) { alert( 'WOO!'); } }); } Building Applications: Introduction to the Pods JSON API // Josh Pollock // PodsCamp 2014
  • 21. Render With A Handlebars Template function getItem( id, pod, templateID, containerID ) { var get_url = podsURL + '/' + pod + '/' + 'id'; $.ajax({ type:"GET", url: get_url, dataType : 'json', beforeSend : function( xhr ) { xhr.setRequestHeader( 'X-WP-Nonce', apiNonce ); }, success: function(response) { var source = $( templateID ).html(); var template = Handlebars.compile( source ); var html = template( data ); $( container ).append( html ); } }); } Building Applications: Introduction to the Pods JSON API // Josh Pollock // PodsCamp 2014
  • 22. Non WordPress Front-ends Angular Client For Pods API https://github.com/bjoernklose/angular-wordpress-pods Using the WordPress REST API in a mobile app http://apppresser.com/using-wordpress-rest-api-mobile-app/ Building Applications: Introduction to the Pods JSON API // Josh Pollock // PodsCamp 2014
  • 23. Questions? Building Applications: Introduction to the Pods JSON API // Josh Pollock // PodsCamp 2014