SlideShare ist ein Scribd-Unternehmen logo
1 von 23
Downloaden Sie, um offline zu lesen
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

Weitere ähnliche Inhalte

Was ist angesagt?

Endless runner game in unreal engine 4
Endless runner game in unreal engine 4Endless runner game in unreal engine 4
Endless runner game in unreal engine 4Vasilis Kamakaris
 
Android pentesting the hackers-meetup
Android pentesting the hackers-meetupAndroid pentesting the hackers-meetup
Android pentesting the hackers-meetupkunwaratul hax0r
 
automation framework
automation frameworkautomation framework
automation frameworkANSHU GOYAL
 
Top 10 real life WebSocket use cases & experiences - Devoxx UK 2015
Top 10 real life WebSocket use cases & experiences - Devoxx UK 2015Top 10 real life WebSocket use cases & experiences - Devoxx UK 2015
Top 10 real life WebSocket use cases & experiences - Devoxx UK 2015Rich Cullen
 
모바일 게임 테스트 자동화 (Appium 확장)
모바일 게임 테스트 자동화 (Appium 확장)모바일 게임 테스트 자동화 (Appium 확장)
모바일 게임 테스트 자동화 (Appium 확장)Jongwon Kim
 
Lean Usability
Lean UsabilityLean Usability
Lean Usabilityglusman
 
[12]MVVM과 Grab Architecture : MVVM에 가기 위한 여행기
[12]MVVM과 Grab Architecture : MVVM에 가기 위한 여행기[12]MVVM과 Grab Architecture : MVVM에 가기 위한 여행기
[12]MVVM과 Grab Architecture : MVVM에 가기 위한 여행기NAVER Engineering
 
jeu de serpent (Snake)
jeu de serpent (Snake)jeu de serpent (Snake)
jeu de serpent (Snake)chamhi
 
Introduce Game Testing And QA
Introduce Game Testing And QAIntroduce Game Testing And QA
Introduce Game Testing And QAPham Anh Tuan
 
Reactive Web - Servlet & Async, Non-blocking I/O
Reactive Web - Servlet & Async, Non-blocking I/OReactive Web - Servlet & Async, Non-blocking I/O
Reactive Web - Servlet & Async, Non-blocking I/OArawn Park
 
Callbacks, Promises, and Coroutines (oh my!): Asynchronous Programming Patter...
Callbacks, Promises, and Coroutines (oh my!): Asynchronous Programming Patter...Callbacks, Promises, and Coroutines (oh my!): Asynchronous Programming Patter...
Callbacks, Promises, and Coroutines (oh my!): Asynchronous Programming Patter...Domenic Denicola
 
이재훈 개발 포트폴리오.pdf
이재훈 개발 포트폴리오.pdf이재훈 개발 포트폴리오.pdf
이재훈 개발 포트폴리오.pdfjaehoon lee
 
유니티 + Nodejs를 활용한 멀티플레이어 게임 개발하기
유니티 + Nodejs를 활용한 멀티플레이어 게임 개발하기유니티 + Nodejs를 활용한 멀티플레이어 게임 개발하기
유니티 + Nodejs를 활용한 멀티플레이어 게임 개발하기Kiyoung Moon
 
Alphorm.com Formation Laravel : Le Guide Complet du Débutant
Alphorm.com Formation Laravel : Le Guide Complet du DébutantAlphorm.com Formation Laravel : Le Guide Complet du Débutant
Alphorm.com Formation Laravel : Le Guide Complet du DébutantAlphorm
 
[KGC2014] 두 마리 토끼를 잡기 위한 C++ - C# 혼합 멀티플랫폼 게임 아키텍처 설계
[KGC2014] 두 마리 토끼를 잡기 위한 C++ - C#  혼합 멀티플랫폼 게임 아키텍처 설계[KGC2014] 두 마리 토끼를 잡기 위한 C++ - C#  혼합 멀티플랫폼 게임 아키텍처 설계
[KGC2014] 두 마리 토끼를 잡기 위한 C++ - C# 혼합 멀티플랫폼 게임 아키텍처 설계Sungkyun Kim
 
Alphorm.com Formation Laravel : Construire une Application de A à Z
Alphorm.com Formation Laravel : Construire une Application de A à ZAlphorm.com Formation Laravel : Construire une Application de A à Z
Alphorm.com Formation Laravel : Construire une Application de A à ZAlphorm
 
Umg ,이벤트 바인딩, Invaidation Box
Umg ,이벤트 바인딩, Invaidation BoxUmg ,이벤트 바인딩, Invaidation Box
Umg ,이벤트 바인딩, Invaidation Box대영 노
 

Was ist angesagt? (20)

Endless runner game in unreal engine 4
Endless runner game in unreal engine 4Endless runner game in unreal engine 4
Endless runner game in unreal engine 4
 
Android pentesting the hackers-meetup
Android pentesting the hackers-meetupAndroid pentesting the hackers-meetup
Android pentesting the hackers-meetup
 
automation framework
automation frameworkautomation framework
automation framework
 
Promises, Promises
Promises, PromisesPromises, Promises
Promises, Promises
 
Top 10 real life WebSocket use cases & experiences - Devoxx UK 2015
Top 10 real life WebSocket use cases & experiences - Devoxx UK 2015Top 10 real life WebSocket use cases & experiences - Devoxx UK 2015
Top 10 real life WebSocket use cases & experiences - Devoxx UK 2015
 
모바일 게임 테스트 자동화 (Appium 확장)
모바일 게임 테스트 자동화 (Appium 확장)모바일 게임 테스트 자동화 (Appium 확장)
모바일 게임 테스트 자동화 (Appium 확장)
 
.NET Framework
.NET Framework.NET Framework
.NET Framework
 
Lean Usability
Lean UsabilityLean Usability
Lean Usability
 
[12]MVVM과 Grab Architecture : MVVM에 가기 위한 여행기
[12]MVVM과 Grab Architecture : MVVM에 가기 위한 여행기[12]MVVM과 Grab Architecture : MVVM에 가기 위한 여행기
[12]MVVM과 Grab Architecture : MVVM에 가기 위한 여행기
 
jeu de serpent (Snake)
jeu de serpent (Snake)jeu de serpent (Snake)
jeu de serpent (Snake)
 
Introduce Game Testing And QA
Introduce Game Testing And QAIntroduce Game Testing And QA
Introduce Game Testing And QA
 
Reactive Web - Servlet & Async, Non-blocking I/O
Reactive Web - Servlet & Async, Non-blocking I/OReactive Web - Servlet & Async, Non-blocking I/O
Reactive Web - Servlet & Async, Non-blocking I/O
 
Callbacks, Promises, and Coroutines (oh my!): Asynchronous Programming Patter...
Callbacks, Promises, and Coroutines (oh my!): Asynchronous Programming Patter...Callbacks, Promises, and Coroutines (oh my!): Asynchronous Programming Patter...
Callbacks, Promises, and Coroutines (oh my!): Asynchronous Programming Patter...
 
이재훈 개발 포트폴리오.pdf
이재훈 개발 포트폴리오.pdf이재훈 개발 포트폴리오.pdf
이재훈 개발 포트폴리오.pdf
 
유니티 + Nodejs를 활용한 멀티플레이어 게임 개발하기
유니티 + Nodejs를 활용한 멀티플레이어 게임 개발하기유니티 + Nodejs를 활용한 멀티플레이어 게임 개발하기
유니티 + Nodejs를 활용한 멀티플레이어 게임 개발하기
 
Alphorm.com Formation Laravel : Le Guide Complet du Débutant
Alphorm.com Formation Laravel : Le Guide Complet du DébutantAlphorm.com Formation Laravel : Le Guide Complet du Débutant
Alphorm.com Formation Laravel : Le Guide Complet du Débutant
 
Test automation using selenium
Test automation using seleniumTest automation using selenium
Test automation using selenium
 
[KGC2014] 두 마리 토끼를 잡기 위한 C++ - C# 혼합 멀티플랫폼 게임 아키텍처 설계
[KGC2014] 두 마리 토끼를 잡기 위한 C++ - C#  혼합 멀티플랫폼 게임 아키텍처 설계[KGC2014] 두 마리 토끼를 잡기 위한 C++ - C#  혼합 멀티플랫폼 게임 아키텍처 설계
[KGC2014] 두 마리 토끼를 잡기 위한 C++ - C# 혼합 멀티플랫폼 게임 아키텍처 설계
 
Alphorm.com Formation Laravel : Construire une Application de A à Z
Alphorm.com Formation Laravel : Construire une Application de A à ZAlphorm.com Formation Laravel : Construire une Application de A à Z
Alphorm.com Formation Laravel : Construire une Application de A à Z
 
Umg ,이벤트 바인딩, Invaidation Box
Umg ,이벤트 바인딩, Invaidation BoxUmg ,이벤트 바인딩, Invaidation Box
Umg ,이벤트 바인딩, Invaidation Box
 

Andere mochten auch

No More “Cowboy Coding”: A Best Practices Guide to Local Development & Migration
No More “Cowboy Coding”: A Best Practices Guide to Local Development & MigrationNo More “Cowboy Coding”: A Best Practices Guide to Local Development & Migration
No More “Cowboy Coding”: A Best Practices Guide to Local Development & Migrationpodsframework
 
Building a Rails API with the JSON API Spec
Building a Rails API with the JSON API SpecBuilding a Rails API with the JSON API Spec
Building a Rails API with the JSON API SpecSonja Peterson
 
Five events in the life of every WordPress request you should know
Five events in the life of every WordPress request you should knowFive events in the life of every WordPress request you should know
Five events in the life of every WordPress request you should knowCaldera Labs
 
Ember Data and JSON API
Ember Data and JSON APIEmber Data and JSON API
Ember Data and JSON APIyoranbe
 
Caldera Learn - LoopConf WP API + Angular FTW Workshop
Caldera Learn - LoopConf WP API + Angular FTW WorkshopCaldera Learn - LoopConf WP API + Angular FTW Workshop
Caldera Learn - LoopConf WP API + Angular FTW WorkshopCalderaLearn
 
Connecting Content Silos: One CMS, Many Sites With The WordPress REST API
 Connecting Content Silos: One CMS, Many Sites With The WordPress REST API Connecting Content Silos: One CMS, Many Sites With The WordPress REST API
Connecting Content Silos: One CMS, Many Sites With The WordPress REST APICaldera Labs
 
Build A Killer Client For Your REST+JSON API
Build A Killer Client For Your REST+JSON APIBuild A Killer Client For Your REST+JSON API
Build A Killer Client For Your REST+JSON APIStormpath
 

Andere mochten auch (7)

No More “Cowboy Coding”: A Best Practices Guide to Local Development & Migration
No More “Cowboy Coding”: A Best Practices Guide to Local Development & MigrationNo More “Cowboy Coding”: A Best Practices Guide to Local Development & Migration
No More “Cowboy Coding”: A Best Practices Guide to Local Development & Migration
 
Building a Rails API with the JSON API Spec
Building a Rails API with the JSON API SpecBuilding a Rails API with the JSON API Spec
Building a Rails API with the JSON API Spec
 
Five events in the life of every WordPress request you should know
Five events in the life of every WordPress request you should knowFive events in the life of every WordPress request you should know
Five events in the life of every WordPress request you should know
 
Ember Data and JSON API
Ember Data and JSON APIEmber Data and JSON API
Ember Data and JSON API
 
Caldera Learn - LoopConf WP API + Angular FTW Workshop
Caldera Learn - LoopConf WP API + Angular FTW WorkshopCaldera Learn - LoopConf WP API + Angular FTW Workshop
Caldera Learn - LoopConf WP API + Angular FTW Workshop
 
Connecting Content Silos: One CMS, Many Sites With The WordPress REST API
 Connecting Content Silos: One CMS, Many Sites With The WordPress REST API Connecting Content Silos: One CMS, Many Sites With The WordPress REST API
Connecting Content Silos: One CMS, Many Sites With The WordPress REST API
 
Build A Killer Client For Your REST+JSON API
Build A Killer Client For Your REST+JSON APIBuild A Killer Client For Your REST+JSON API
Build A Killer Client For Your REST+JSON API
 

Ähnlich wie Introduction to the Pods JSON API

Creating REST Applications with the Slim Micro-Framework by Vikram Vaswani
Creating REST Applications with the Slim Micro-Framework by Vikram VaswaniCreating REST Applications with the Slim Micro-Framework by Vikram Vaswani
Creating REST Applications with the Slim Micro-Framework by Vikram Vaswanivvaswani
 
Teaming up WordPress API with Backbone.js in Titanium
Teaming up WordPress API with Backbone.js in TitaniumTeaming up WordPress API with Backbone.js in Titanium
Teaming up WordPress API with Backbone.js in TitaniumJeroen van Dijk
 
Using the new WordPress REST API
Using the new WordPress REST APIUsing the new WordPress REST API
Using the new WordPress REST APICaldera Labs
 
Building WordPress Client Side Applications with WP and WP-API - #wcmia
Building WordPress Client Side Applications with WP and WP-API - #wcmiaBuilding WordPress Client Side Applications with WP and WP-API - #wcmia
Building WordPress Client Side Applications with WP and WP-API - #wcmiaRoy Sivan
 
Build Modern Web Applications with React and WordPress
Build Modern Web Applications with React and WordPressBuild Modern Web Applications with React and WordPress
Build Modern Web Applications with React and WordPressImran Sayed
 
How to build Client Side Applications with WordPress and WP-API | #wcmia
How to build Client Side Applications with WordPress and WP-API | #wcmiaHow to build Client Side Applications with WordPress and WP-API | #wcmia
How to build Client Side Applications with WordPress and WP-API | #wcmiaRoy Sivan
 
React with WordPress : Headless CMS
React with WordPress : Headless CMSReact with WordPress : Headless CMS
React with WordPress : Headless CMSImran Sayed
 
WordCamp Raleigh 2016 - WP API, What is it good for? Absolutely Everything!
WordCamp Raleigh 2016 - WP API, What is it good for? Absolutely Everything!WordCamp Raleigh 2016 - WP API, What is it good for? Absolutely Everything!
WordCamp Raleigh 2016 - WP API, What is it good for? Absolutely Everything!Evan Mullins
 
Remedie: Building a desktop app with HTTP::Engine, SQLite and jQuery
Remedie: Building a desktop app with HTTP::Engine, SQLite and jQueryRemedie: Building a desktop app with HTTP::Engine, SQLite and jQuery
Remedie: Building a desktop app with HTTP::Engine, SQLite and jQueryTatsuhiko Miyagawa
 
Flask and Angular: An approach to build robust platforms
Flask and Angular:  An approach to build robust platformsFlask and Angular:  An approach to build robust platforms
Flask and Angular: An approach to build robust platformsAyush Sharma
 
Building native mobile apps with word press
Building native mobile apps with word pressBuilding native mobile apps with word press
Building native mobile apps with word pressNikhil Vishnu P.V
 
WIRED and the WP REST API
WIRED and the WP REST APIWIRED and the WP REST API
WIRED and the WP REST APIkvignos
 
Full stack development with node and NoSQL - All Things Open - October 2017
Full stack development with node and NoSQL - All Things Open - October 2017Full stack development with node and NoSQL - All Things Open - October 2017
Full stack development with node and NoSQL - All Things Open - October 2017Matthew Groves
 
Full Stack Development with Node.js and NoSQL
Full Stack Development with Node.js and NoSQLFull Stack Development with Node.js and NoSQL
Full Stack Development with Node.js and NoSQLAll Things Open
 
API Driven Application - AngulatJS, NodeJS and MongoDB | JCertif Tunisia 2015
API  Driven Application - AngulatJS, NodeJS and MongoDB | JCertif Tunisia 2015 API  Driven Application - AngulatJS, NodeJS and MongoDB | JCertif Tunisia 2015
API Driven Application - AngulatJS, NodeJS and MongoDB | JCertif Tunisia 2015 Hamdi Hmidi
 
jsSaturday - PhoneGap and jQuery Mobile for SharePoint 2013
jsSaturday - PhoneGap and jQuery Mobile for SharePoint 2013jsSaturday - PhoneGap and jQuery Mobile for SharePoint 2013
jsSaturday - PhoneGap and jQuery Mobile for SharePoint 2013Kiril Iliev
 
Introduction to VueJS & The WordPress REST API
Introduction to VueJS & The WordPress REST APIIntroduction to VueJS & The WordPress REST API
Introduction to VueJS & The WordPress REST APICaldera Labs
 
RESTful API development in Laravel 4 - Christopher Pecoraro
RESTful API development in Laravel 4 - Christopher PecoraroRESTful API development in Laravel 4 - Christopher Pecoraro
RESTful API development in Laravel 4 - Christopher PecoraroChristopher Pecoraro
 
Extending the WordPress REST API - Josh Pollock
Extending the WordPress REST API - Josh PollockExtending the WordPress REST API - Josh Pollock
Extending the WordPress REST API - Josh PollockCaldera Labs
 

Ähnlich wie Introduction to the Pods JSON API (20)

Creating REST Applications with the Slim Micro-Framework by Vikram Vaswani
Creating REST Applications with the Slim Micro-Framework by Vikram VaswaniCreating REST Applications with the Slim Micro-Framework by Vikram Vaswani
Creating REST Applications with the Slim Micro-Framework by Vikram Vaswani
 
Teaming up WordPress API with Backbone.js in Titanium
Teaming up WordPress API with Backbone.js in TitaniumTeaming up WordPress API with Backbone.js in Titanium
Teaming up WordPress API with Backbone.js in Titanium
 
Using the new WordPress REST API
Using the new WordPress REST APIUsing the new WordPress REST API
Using the new WordPress REST API
 
Building WordPress Client Side Applications with WP and WP-API - #wcmia
Building WordPress Client Side Applications with WP and WP-API - #wcmiaBuilding WordPress Client Side Applications with WP and WP-API - #wcmia
Building WordPress Client Side Applications with WP and WP-API - #wcmia
 
Build Modern Web Applications with React and WordPress
Build Modern Web Applications with React and WordPressBuild Modern Web Applications with React and WordPress
Build Modern Web Applications with React and WordPress
 
How to build Client Side Applications with WordPress and WP-API | #wcmia
How to build Client Side Applications with WordPress and WP-API | #wcmiaHow to build Client Side Applications with WordPress and WP-API | #wcmia
How to build Client Side Applications with WordPress and WP-API | #wcmia
 
React with WordPress : Headless CMS
React with WordPress : Headless CMSReact with WordPress : Headless CMS
React with WordPress : Headless CMS
 
WordCamp Raleigh 2016 - WP API, What is it good for? Absolutely Everything!
WordCamp Raleigh 2016 - WP API, What is it good for? Absolutely Everything!WordCamp Raleigh 2016 - WP API, What is it good for? Absolutely Everything!
WordCamp Raleigh 2016 - WP API, What is it good for? Absolutely Everything!
 
Remedie: Building a desktop app with HTTP::Engine, SQLite and jQuery
Remedie: Building a desktop app with HTTP::Engine, SQLite and jQueryRemedie: Building a desktop app with HTTP::Engine, SQLite and jQuery
Remedie: Building a desktop app with HTTP::Engine, SQLite and jQuery
 
Flask and Angular: An approach to build robust platforms
Flask and Angular:  An approach to build robust platformsFlask and Angular:  An approach to build robust platforms
Flask and Angular: An approach to build robust platforms
 
Building native mobile apps with word press
Building native mobile apps with word pressBuilding native mobile apps with word press
Building native mobile apps with word press
 
WIRED and the WP REST API
WIRED and the WP REST APIWIRED and the WP REST API
WIRED and the WP REST API
 
Full stack development with node and NoSQL - All Things Open - October 2017
Full stack development with node and NoSQL - All Things Open - October 2017Full stack development with node and NoSQL - All Things Open - October 2017
Full stack development with node and NoSQL - All Things Open - October 2017
 
Full Stack Development with Node.js and NoSQL
Full Stack Development with Node.js and NoSQLFull Stack Development with Node.js and NoSQL
Full Stack Development with Node.js and NoSQL
 
API Driven Application - AngulatJS, NodeJS and MongoDB | JCertif Tunisia 2015
API  Driven Application - AngulatJS, NodeJS and MongoDB | JCertif Tunisia 2015 API  Driven Application - AngulatJS, NodeJS and MongoDB | JCertif Tunisia 2015
API Driven Application - AngulatJS, NodeJS and MongoDB | JCertif Tunisia 2015
 
jsSaturday - PhoneGap and jQuery Mobile for SharePoint 2013
jsSaturday - PhoneGap and jQuery Mobile for SharePoint 2013jsSaturday - PhoneGap and jQuery Mobile for SharePoint 2013
jsSaturday - PhoneGap and jQuery Mobile for SharePoint 2013
 
Introduction to VueJS & The WordPress REST API
Introduction to VueJS & The WordPress REST APIIntroduction to VueJS & The WordPress REST API
Introduction to VueJS & The WordPress REST API
 
RESTful API development in Laravel 4 - Christopher Pecoraro
RESTful API development in Laravel 4 - Christopher PecoraroRESTful API development in Laravel 4 - Christopher Pecoraro
RESTful API development in Laravel 4 - Christopher Pecoraro
 
Extending the WordPress REST API - Josh Pollock
Extending the WordPress REST API - Josh PollockExtending the WordPress REST API - Josh Pollock
Extending the WordPress REST API - Josh Pollock
 
REST API for your WP7 App
REST API for your WP7 AppREST API for your WP7 App
REST API for your WP7 App
 

Kürzlich hochgeladen

%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisamasabamasaba
 
SHRMPro HRMS Software Solutions Presentation
SHRMPro HRMS Software Solutions PresentationSHRMPro HRMS Software Solutions Presentation
SHRMPro HRMS Software Solutions PresentationShrmpro
 
%in Lydenburg+277-882-255-28 abortion pills for sale in Lydenburg
%in Lydenburg+277-882-255-28 abortion pills for sale in Lydenburg%in Lydenburg+277-882-255-28 abortion pills for sale in Lydenburg
%in Lydenburg+277-882-255-28 abortion pills for sale in Lydenburgmasabamasaba
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsJhone kinadey
 
10 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 202410 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 2024Mind IT Systems
 
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) SolutionIntroducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) SolutionOnePlan Solutions
 
%in Durban+277-882-255-28 abortion pills for sale in Durban
%in Durban+277-882-255-28 abortion pills for sale in Durban%in Durban+277-882-255-28 abortion pills for sale in Durban
%in Durban+277-882-255-28 abortion pills for sale in Durbanmasabamasaba
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️Delhi Call girls
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️Delhi Call girls
 
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park %in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park masabamasaba
 
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdfintroduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdfVishalKumarJha10
 
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdfPayment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdfkalichargn70th171
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Steffen Staab
 
%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrandmasabamasaba
 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...masabamasaba
 
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfonteinmasabamasaba
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfkalichargn70th171
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Modelsaagamshah0812
 
Architecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastArchitecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastPapp Krisztián
 
8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech studentsHimanshiGarg82
 

Kürzlich hochgeladen (20)

%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 
SHRMPro HRMS Software Solutions Presentation
SHRMPro HRMS Software Solutions PresentationSHRMPro HRMS Software Solutions Presentation
SHRMPro HRMS Software Solutions Presentation
 
%in Lydenburg+277-882-255-28 abortion pills for sale in Lydenburg
%in Lydenburg+277-882-255-28 abortion pills for sale in Lydenburg%in Lydenburg+277-882-255-28 abortion pills for sale in Lydenburg
%in Lydenburg+277-882-255-28 abortion pills for sale in Lydenburg
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
 
10 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 202410 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 2024
 
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) SolutionIntroducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
 
%in Durban+277-882-255-28 abortion pills for sale in Durban
%in Durban+277-882-255-28 abortion pills for sale in Durban%in Durban+277-882-255-28 abortion pills for sale in Durban
%in Durban+277-882-255-28 abortion pills for sale in Durban
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park %in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
 
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdfintroduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
 
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdfPayment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
 
%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand
 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
 
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Models
 
Architecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastArchitecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the past
 
8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students
 

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