SlideShare a Scribd company logo
1 of 37
Download to read offline
JSON REST API for WordPress 
@tlovett12 
+ JSON 
REST API 
=
Who Am I? 
• My name is Taylor Lovett! 
• Director of Web Engineering at 10up 
• Open source community member 
• WordPress core contributor 
• WP API team member 
@tlovett12
We are hiring! 
@tlovett12
So what’s this new WP API thing all 
about? Don’t we already have one?
Right now, we have XML-RPC. It works but 
is extremely hard to use and outdated.
Comparison to other WordPress API’s 
! 
https://github.com/WP-API/WP-API/blob/ 
master/docs/comparison.md
Why JSON REST API? 
• In a nutshell, JSON REST API’s have swept the 
web becoming an almost standard. They are 
extremely intuitive and provide an easy way to 
distribute, collect, and modify data. 
Let’s break it down a bit.
JSON 
• JSON is an abbreviation for “JavaScript Object Notation” 
• It’s simply a way to describe data that is lightweight and 
extremely easy to use. Arguably much easier to use than 
XML.
REST 
• REST (Representational State Transfer) is an architectural style 
that dictates how HTTP and URI’s should be used and organized. 
• Verbs and resources: GET /post/1 
• Hypermedia as the Engine of Application State (HATEOAS) - 
Server provides everything you need to know how to use it in a 
response. 
• Actions are autonomous and do not depend on each other. 
• Bottom line: RESTful API’s have become extremely popular 
across the web. They are much easier to use than things like RPC 
or SOAP.
And of course, API 
• An API (Application Programming Interface) is a 
set of entry points that allow you to interact with 
a platform (WordPress in this case).
Ryan McCue and Contributors
How can I start using it now?
First, install the plugin 
http://wordpress.org/plugins/json-rest-api/ 
! 
Core integration coming soon.
What does the API allow me to do? 
/wp-json/ 
Shows all the routes and endpoints available 
/wp-json/posts 
Create, read, update, and delete posts 
/wp-json/users 
Create, read, update, and delete users 
/wp-json/media 
Create, read, update, and delete media items 
/wp-json/taxonomies 
Read taxonomies and terms 
/wp-json/pages/ 
Create, read, update, and delete pages
The API is rich with functionality. 
Explore the documentation! 
http://wp-api.org/docs-development/ 
Let’s look at a few key endpoints.
List Posts 
[{! 
"ID": 11297,! 
"title": "Post 19",! 
"status": "publish",! 
"type": "post",! 
"author": 1,! 
"content": "",! 
"parent": null,! 
"link": "http://example.com/2014/08/post-19/",! 
"format": "standard",! 
"slug": "post-19",! 
"guid": "http://example.com/2014/08/post-19/",! 
"excerpt": null,! 
"menu_order": 0,! 
"comment_status": "closed",! 
"ping_status": "open",! 
"sticky": false,! 
"meta": {},! 
"featured_image": null,! 
"terms": {}! 
}] 
GET /wp-json/posts
List Posts 
Endpoint: /wp-json/posts 
Takes a number of useful parameters: 
• Filter[]: Accepts WP_Query arguments 
• Page: Allows for pagination 
• Context: Determines usage context i.e. “view or edit” 
• … 
https://github.com/WP-API/WP-API/blob/master/docs/routes/routes.md
Retrieve A Post 
{! 
"ID": 11297,! 
"title": "Post 19",! 
"status": "publish",! 
"type": "post",! 
"author": 1,! 
"content": "",! 
"parent": null,! 
"link": "http://example.com/2014/08/post-19/",! 
"format": "standard",! 
"slug": "post-19",! 
"guid": "http://example.com/2014/08/post-19/",! 
"excerpt": null,! 
"menu_order": 0,! 
"comment_status": "closed",! 
"ping_status": "open",! 
"sticky": false,! 
"meta": {},! 
"featured_image": null,! 
"terms": {}! 
} 
GET /wp-json/posts/<id>
Edit A Post 
PUT /wp-json/posts/<id> 
curl -X PUT -H “Content-Type: application/json” -d ‘! 
{! 
"title": “Updated Title",! 
“content_raw": “Updated post content"! 
}! 
‘ -u admin:password http://example.com/wp-json/posts/<id> 
We need to send a PUT request to this endpoint with 
our post data. Of course we must authenticate before 
doing this.
Three ways to authenticate 
• Cookie Authentication (client side) 
• HTTP Basic Authentication 
• OAuth 1
HTTP Basic Authentication 
First install the WP Basic Auth Plugin: 
https://github.com/WP-API/Basic-Auth 
Remember this piece of our cURL request? 
-u admin:password 
That’s HTTP Basic Authentication! Essentially we are authenticating 
by passing an HTTP header like this: 
Authorization: Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ== 
Where that crazy looking string is username:password base64 
encoded.
HTTP Basic Authentication 
should only be used for testing!
OAuth 1.0a 
First install the WP OAuth Plugin: 
https://github.com/WP-API/OAuth1 
OAuth is outside of the scope of this talk. However, it 
should be used instead of HTTP Basic Auth when 
building external applications that interact with the 
API. Rather than giving someone an account on your 
site, you can give them temporary access with 
OAuth.
Create A Post 
POST /wp-json/posts/ 
curl -X POST -H “Content-Type: application/json” -d ‘! 
{! 
"title": “Title",! 
“content_raw": “Post content"! 
}! 
‘ -u admin:password http://example.com/wp-json/posts/ 
Notice we are using a POST request this time.
Taxonomies 
GET /wp-json/taxonomies 
[! 
{! 
"name": "Categories",! 
"slug": "category",! 
"labels": {},! 
"types": { /* Registered post types */ },! 
"show_cloud": true,! 
"hierarchical": true,! 
"meta": {}! 
},! 
{! 
"name": "Tags",! 
"slug": "post_tag",! 
"labels": {},! 
"types": { /* Registered post types */ },! 
"show_cloud": true,! 
"hierarchical": false,! 
"meta": {}! 
}! 
}! 
]
Taxonomy 
GET /wp-json/taxonomies/<taxonomy> 
{! 
"name": "Categories",! 
"slug": "category",! 
"labels": {},! 
"types": { /* Registered post types */ },! 
"show_cloud": true,! 
"hierarchical": true,! 
"meta": {}! 
}
Taxonomy Terms 
GET /wp-json/taxonomies/<taxonomy>/terms 
[! 
{! 
"ID": 1,! 
"name": "Books",! 
"slug": "books",! 
"description": "",! 
"parent": null,! 
"count": 1,! 
"link": "http://example.com/category/books/",! 
"meta": {}! 
},! 
{! 
"ID": 2,! 
"name": "Products",! 
"slug": "products",! 
"description": "",! 
"parent": null,! 
"count": 1,! 
"link": "http://example.com/category/products/",! 
"meta": {}! 
}! 
]
Build Your Own Routes and Endpoints 
WP API is very extensible (custom post types!) 
http://wp-api.org/guides/extending.html
What can I do with the JSON 
REST API for WordPress?
JavaScript 
Interact with your (or someone else’s) WordPress install with 
JavaScript. 
Backbone.js Client: 
https://github.com/WP-API/client-js 
! 
! 
Node.js Client: 
https://github.com/kadamwhite/wordpress-rest-api 
!
Backbone.js 
• Backbone.js is a JavaScript framework that lets 
you structure code in terms of models, views, 
and collections. It works great with RESTful 
JSON API’s.
_s_backbone 
• _s or underscores is a popular starter theme by 
Automattic: 
https://github.com/automattic/_s 
• _s_backbone is an _s fork that powers post 
loops using the WP API Backbone client
https://github.com/tlovett1/_s_backbone
What does this mean? 
• It means _s_backbone is a starter theme with 
infinite scroll built-in using the WP API Backbone 
client. 
• Infinite scroll is the concept of loading multiple 
rounds of entities without reloading the page. 
Let’s look at some code!
This is some JavaScript you could add to a theme or 
plugin to display your site’s posts. You will first need 
to have JSON REST API for WordPress installed and 
the “wp-api” JavaScript dependency enqueued. 
functions.php: 
js/scripts.js:
If you learned nothing so far, 
know this: 
You can do amazing things with the JSON REST 
API for WordPress. 
With core integration and ~23% of the web using 
this API in the near future, you will have much 
easier access to data across the web.
Questions? 
@tlovett12! 
taylor.lovett@10up.com! 
taylorlovett.com 
We need to send a PUT request to this endpoint with 
our post data. Of course we must authenticate before 
doing this.

More Related Content

What's hot

Advanced WordPress Development Environments
Advanced WordPress Development EnvironmentsAdvanced WordPress Development Environments
Advanced WordPress Development Environments
Beau Lebens
 

What's hot (20)

Best Practices for WordPress
Best Practices for WordPressBest Practices for WordPress
Best Practices for WordPress
 
Develop webservice in PHP
Develop webservice in PHPDevelop webservice in PHP
Develop webservice in PHP
 
Scalable web application architecture
Scalable web application architectureScalable web application architecture
Scalable web application architecture
 
Advanced WordPress Development Environments
Advanced WordPress Development EnvironmentsAdvanced WordPress Development Environments
Advanced WordPress Development Environments
 
Building Your First App with MongoDB
Building Your First App with MongoDBBuilding Your First App with MongoDB
Building Your First App with MongoDB
 
Put a little Backbone in your WordPress
Put a little Backbone in your WordPressPut a little Backbone in your WordPress
Put a little Backbone in your WordPress
 
CouchDB for Web Applications - Erlang Factory London 2009
CouchDB for Web Applications - Erlang Factory London 2009CouchDB for Web Applications - Erlang Factory London 2009
CouchDB for Web Applications - Erlang Factory London 2009
 
The never-ending REST API design debate
The never-ending REST API design debateThe never-ending REST API design debate
The never-ending REST API design debate
 
Introduction to Plugin Programming, WordCamp Miami 2011
Introduction to Plugin Programming, WordCamp Miami 2011Introduction to Plugin Programming, WordCamp Miami 2011
Introduction to Plugin Programming, WordCamp Miami 2011
 
Differential Sync and JSON Patch @ SpringOne2GX 2014
Differential Sync and JSON Patch @ SpringOne2GX 2014Differential Sync and JSON Patch @ SpringOne2GX 2014
Differential Sync and JSON Patch @ SpringOne2GX 2014
 
Hypermedia: The Missing Element to Building Adaptable Web APIs in Rails
Hypermedia: The Missing Element to Building Adaptable Web APIs in RailsHypermedia: The Missing Element to Building Adaptable Web APIs in Rails
Hypermedia: The Missing Element to Building Adaptable Web APIs in Rails
 
Introducing RaveJS: Spring Boot concepts for JavaScript applications
Introducing RaveJS: Spring Boot concepts for JavaScript applicationsIntroducing RaveJS: Spring Boot concepts for JavaScript applications
Introducing RaveJS: Spring Boot concepts for JavaScript applications
 
REST API Best Practices & Implementing in Codeigniter
REST API Best Practices & Implementing in CodeigniterREST API Best Practices & Implementing in Codeigniter
REST API Best Practices & Implementing in Codeigniter
 
Transforming WordPress Search and Query Performance with Elasticsearch
Transforming WordPress Search and Query Performance with Elasticsearch Transforming WordPress Search and Query Performance with Elasticsearch
Transforming WordPress Search and Query Performance with Elasticsearch
 
REST Web API with MongoDB
REST Web API with MongoDBREST Web API with MongoDB
REST Web API with MongoDB
 
Introduction to CouchDB
Introduction to CouchDBIntroduction to CouchDB
Introduction to CouchDB
 
Understanding and testing restful web services
Understanding and testing restful web servicesUnderstanding and testing restful web services
Understanding and testing restful web services
 
Djangocon 2014 angular + django
Djangocon 2014 angular + djangoDjangocon 2014 angular + django
Djangocon 2014 angular + django
 
Software Development with Open Source
Software Development with Open SourceSoftware Development with Open Source
Software Development with Open Source
 
REST Easy with AngularJS - ng-grid CRUD EXAMPLE
REST Easy with AngularJS - ng-grid CRUD EXAMPLEREST Easy with AngularJS - ng-grid CRUD EXAMPLE
REST Easy with AngularJS - ng-grid CRUD EXAMPLE
 

Viewers also liked

CodaでClipを使ってWordPress開発を早くするススメ。
 CodaでClipを使ってWordPress開発を早くするススメ。 CodaでClipを使ってWordPress開発を早くするススメ。
CodaでClipを使ってWordPress開発を早くするススメ。
Shinichi Nishikawa
 

Viewers also liked (20)

WordPress & A Mobile App
WordPress & A Mobile AppWordPress & A Mobile App
WordPress & A Mobile App
 
マルチパブリッシング プラットフォームとしてのWordPress
マルチパブリッシング プラットフォームとしてのWordPressマルチパブリッシング プラットフォームとしてのWordPress
マルチパブリッシング プラットフォームとしてのWordPress
 
Demystifying the REST API
Demystifying the REST APIDemystifying the REST API
Demystifying the REST API
 
WordPress and the Enterprise
WordPress and the EnterpriseWordPress and the Enterprise
WordPress and the Enterprise
 
Building WordPress sites with AngularJS and the RESTful plugin JSON API @ Dev...
Building WordPress sites with AngularJS and the RESTful plugin JSON API @ Dev...Building WordPress sites with AngularJS and the RESTful plugin JSON API @ Dev...
Building WordPress sites with AngularJS and the RESTful plugin JSON API @ Dev...
 
WordCamp Seoul: WordPress Based web services in Japan / WordCamp 서울 : 일본에서 워드...
WordCamp Seoul: WordPress Based web services in Japan / WordCamp 서울 : 일본에서 워드...WordCamp Seoul: WordPress Based web services in Japan / WordCamp 서울 : 일본에서 워드...
WordCamp Seoul: WordPress Based web services in Japan / WordCamp 서울 : 일본에서 워드...
 
PHP classの教室
PHP classの教室PHP classの教室
PHP classの教室
 
第三章ナビゲーション &lt; 第4回デザイニング・インターフェース勉強会
第三章ナビゲーション &lt; 第4回デザイニング・インターフェース勉強会第三章ナビゲーション &lt; 第4回デザイニング・インターフェース勉強会
第三章ナビゲーション &lt; 第4回デザイニング・インターフェース勉強会
 
CodaでClipを使ってWordPress開発を早くするススメ。
 CodaでClipを使ってWordPress開発を早くするススメ。 CodaでClipを使ってWordPress開発を早くするススメ。
CodaでClipを使ってWordPress開発を早くするススメ。
 
WP REST API
WP REST APIWP REST API
WP REST API
 
WordCamp Tokyo 2012 Concept
WordCamp Tokyo 2012 ConceptWordCamp Tokyo 2012 Concept
WordCamp Tokyo 2012 Concept
 
WordPress中級者への道!テンプレートタグはどう動くのか!?
WordPress中級者への道!テンプレートタグはどう動くのか!?WordPress中級者への道!テンプレートタグはどう動くのか!?
WordPress中級者への道!テンプレートタグはどう動くのか!?
 
8時間耐久CakePHP2 勉強会
8時間耐久CakePHP2 勉強会8時間耐久CakePHP2 勉強会
8時間耐久CakePHP2 勉強会
 
WordPress REST API hacking
WordPress REST API hackingWordPress REST API hacking
WordPress REST API hacking
 
How to get your theme on Top 15 Popular Themes at WordPress.org
How to get your theme on Top 15 Popular Themes at WordPress.orgHow to get your theme on Top 15 Popular Themes at WordPress.org
How to get your theme on Top 15 Popular Themes at WordPress.org
 
Child Theme
Child ThemeChild Theme
Child Theme
 
An easy guide to Plugin Development
An easy guide to Plugin DevelopmentAn easy guide to Plugin Development
An easy guide to Plugin Development
 
Javantura v4 - True RESTful Java Web Services with JSON API and Katharsis - M...
Javantura v4 - True RESTful Java Web Services with JSON API and Katharsis - M...Javantura v4 - True RESTful Java Web Services with JSON API and Katharsis - M...
Javantura v4 - True RESTful Java Web Services with JSON API and Katharsis - M...
 
Getting Started With WP REST API
Getting Started With WP REST APIGetting Started With WP REST API
Getting Started With WP REST API
 
Design Beautiful REST + JSON APIs
Design Beautiful REST + JSON APIsDesign Beautiful REST + JSON APIs
Design Beautiful REST + JSON APIs
 

Similar to JSON REST API for WordPress

The liferay case: lessons learned evolving from RPC to Hypermedia REST APIs
The liferay case: lessons learned evolving from RPC to Hypermedia REST APIsThe liferay case: lessons learned evolving from RPC to Hypermedia REST APIs
The liferay case: lessons learned evolving from RPC to Hypermedia REST APIs
Jorge Ferrer
 

Similar to JSON REST API for WordPress (20)

JSON REST API for WordPress
JSON REST API for WordPressJSON REST API for WordPress
JSON REST API for WordPress
 
AtlasCamp2014: Introducing the Confluence REST API
AtlasCamp2014: Introducing the Confluence REST APIAtlasCamp2014: Introducing the Confluence REST API
AtlasCamp2014: Introducing the Confluence REST API
 
Designing a beautiful REST json api
Designing a beautiful REST json apiDesigning a beautiful REST json api
Designing a beautiful REST json api
 
WordPress APIs
WordPress APIsWordPress APIs
WordPress APIs
 
Finding Restfulness - Madrid.rb April 2014
Finding Restfulness - Madrid.rb April 2014Finding Restfulness - Madrid.rb April 2014
Finding Restfulness - Madrid.rb April 2014
 
Building Better Web APIs with Rails
Building Better Web APIs with RailsBuilding Better Web APIs with Rails
Building Better Web APIs with Rails
 
The liferay case: lessons learned evolving from RPC to Hypermedia REST APIs
The liferay case: lessons learned evolving from RPC to Hypermedia REST APIsThe liferay case: lessons learned evolving from RPC to Hypermedia REST APIs
The liferay case: lessons learned evolving from RPC to Hypermedia REST APIs
 
2.28.17 Introducing DSpace 7 Webinar Slides
2.28.17 Introducing DSpace 7 Webinar Slides2.28.17 Introducing DSpace 7 Webinar Slides
2.28.17 Introducing DSpace 7 Webinar Slides
 
Web Services
Web ServicesWeb Services
Web Services
 
Building APIs in an easy way using API Platform
Building APIs in an easy way using API PlatformBuilding APIs in an easy way using API Platform
Building APIs in an easy way using API Platform
 
Have You Seen Spring Lately?
Have You Seen Spring Lately?Have You Seen Spring Lately?
Have You Seen Spring Lately?
 
REST easy with API Platform
REST easy with API PlatformREST easy with API Platform
REST easy with API Platform
 
FOXX - a Javascript application framework on top of ArangoDB
FOXX - a Javascript application framework on top of ArangoDBFOXX - a Javascript application framework on top of ArangoDB
FOXX - a Javascript application framework on top of ArangoDB
 
Survival Strategies for API Documentation: Presentation to Southwestern Ontar...
Survival Strategies for API Documentation: Presentation to Southwestern Ontar...Survival Strategies for API Documentation: Presentation to Southwestern Ontar...
Survival Strategies for API Documentation: Presentation to Southwestern Ontar...
 
ITB2016 - Building ColdFusion RESTFul Services
ITB2016 - Building ColdFusion RESTFul ServicesITB2016 - Building ColdFusion RESTFul Services
ITB2016 - Building ColdFusion RESTFul Services
 
Using the new WordPress REST API
Using the new WordPress REST APIUsing the new WordPress REST API
Using the new WordPress REST API
 
Rest in practice
Rest in practiceRest in practice
Rest in practice
 
Be a microservices hero
Be a microservices heroBe a microservices hero
Be a microservices hero
 
WordCamp Wilmington 2017 WP-API Why?
WordCamp Wilmington 2017   WP-API Why?WordCamp Wilmington 2017   WP-API Why?
WordCamp Wilmington 2017 WP-API Why?
 
Big Data Web applications for Interactive Hadoop by ENRICO BERTI at Big Data...
 Big Data Web applications for Interactive Hadoop by ENRICO BERTI at Big Data... Big Data Web applications for Interactive Hadoop by ENRICO BERTI at Big Data...
Big Data Web applications for Interactive Hadoop by ENRICO BERTI at Big Data...
 

More from Taylor Lovett (6)

WordPress Acceptance Testing, Solved!
WordPress Acceptance Testing, Solved!WordPress Acceptance Testing, Solved!
WordPress Acceptance Testing, Solved!
 
Best practices-wordpress-enterprise
Best practices-wordpress-enterpriseBest practices-wordpress-enterprise
Best practices-wordpress-enterprise
 
Wordpress search-elasticsearch
Wordpress search-elasticsearchWordpress search-elasticsearch
Wordpress search-elasticsearch
 
Modernizing WordPress Search with Elasticsearch
Modernizing WordPress Search with ElasticsearchModernizing WordPress Search with Elasticsearch
Modernizing WordPress Search with Elasticsearch
 
What You Missed in Computer Science
What You Missed in Computer ScienceWhat You Missed in Computer Science
What You Missed in Computer Science
 
Saving Time with WP-CLI
Saving Time with WP-CLISaving Time with WP-CLI
Saving Time with WP-CLI
 

Recently uploaded

Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
Joaquim Jorge
 

Recently uploaded (20)

Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation Strategies
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 

JSON REST API for WordPress

  • 1. JSON REST API for WordPress @tlovett12 + JSON REST API =
  • 2. Who Am I? • My name is Taylor Lovett! • Director of Web Engineering at 10up • Open source community member • WordPress core contributor • WP API team member @tlovett12
  • 3. We are hiring! @tlovett12
  • 4. So what’s this new WP API thing all about? Don’t we already have one?
  • 5. Right now, we have XML-RPC. It works but is extremely hard to use and outdated.
  • 6. Comparison to other WordPress API’s ! https://github.com/WP-API/WP-API/blob/ master/docs/comparison.md
  • 7. Why JSON REST API? • In a nutshell, JSON REST API’s have swept the web becoming an almost standard. They are extremely intuitive and provide an easy way to distribute, collect, and modify data. Let’s break it down a bit.
  • 8. JSON • JSON is an abbreviation for “JavaScript Object Notation” • It’s simply a way to describe data that is lightweight and extremely easy to use. Arguably much easier to use than XML.
  • 9. REST • REST (Representational State Transfer) is an architectural style that dictates how HTTP and URI’s should be used and organized. • Verbs and resources: GET /post/1 • Hypermedia as the Engine of Application State (HATEOAS) - Server provides everything you need to know how to use it in a response. • Actions are autonomous and do not depend on each other. • Bottom line: RESTful API’s have become extremely popular across the web. They are much easier to use than things like RPC or SOAP.
  • 10. And of course, API • An API (Application Programming Interface) is a set of entry points that allow you to interact with a platform (WordPress in this case).
  • 11. Ryan McCue and Contributors
  • 12. How can I start using it now?
  • 13. First, install the plugin http://wordpress.org/plugins/json-rest-api/ ! Core integration coming soon.
  • 14. What does the API allow me to do? /wp-json/ Shows all the routes and endpoints available /wp-json/posts Create, read, update, and delete posts /wp-json/users Create, read, update, and delete users /wp-json/media Create, read, update, and delete media items /wp-json/taxonomies Read taxonomies and terms /wp-json/pages/ Create, read, update, and delete pages
  • 15. The API is rich with functionality. Explore the documentation! http://wp-api.org/docs-development/ Let’s look at a few key endpoints.
  • 16. List Posts [{! "ID": 11297,! "title": "Post 19",! "status": "publish",! "type": "post",! "author": 1,! "content": "",! "parent": null,! "link": "http://example.com/2014/08/post-19/",! "format": "standard",! "slug": "post-19",! "guid": "http://example.com/2014/08/post-19/",! "excerpt": null,! "menu_order": 0,! "comment_status": "closed",! "ping_status": "open",! "sticky": false,! "meta": {},! "featured_image": null,! "terms": {}! }] GET /wp-json/posts
  • 17. List Posts Endpoint: /wp-json/posts Takes a number of useful parameters: • Filter[]: Accepts WP_Query arguments • Page: Allows for pagination • Context: Determines usage context i.e. “view or edit” • … https://github.com/WP-API/WP-API/blob/master/docs/routes/routes.md
  • 18. Retrieve A Post {! "ID": 11297,! "title": "Post 19",! "status": "publish",! "type": "post",! "author": 1,! "content": "",! "parent": null,! "link": "http://example.com/2014/08/post-19/",! "format": "standard",! "slug": "post-19",! "guid": "http://example.com/2014/08/post-19/",! "excerpt": null,! "menu_order": 0,! "comment_status": "closed",! "ping_status": "open",! "sticky": false,! "meta": {},! "featured_image": null,! "terms": {}! } GET /wp-json/posts/<id>
  • 19. Edit A Post PUT /wp-json/posts/<id> curl -X PUT -H “Content-Type: application/json” -d ‘! {! "title": “Updated Title",! “content_raw": “Updated post content"! }! ‘ -u admin:password http://example.com/wp-json/posts/<id> We need to send a PUT request to this endpoint with our post data. Of course we must authenticate before doing this.
  • 20. Three ways to authenticate • Cookie Authentication (client side) • HTTP Basic Authentication • OAuth 1
  • 21. HTTP Basic Authentication First install the WP Basic Auth Plugin: https://github.com/WP-API/Basic-Auth Remember this piece of our cURL request? -u admin:password That’s HTTP Basic Authentication! Essentially we are authenticating by passing an HTTP header like this: Authorization: Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ== Where that crazy looking string is username:password base64 encoded.
  • 22. HTTP Basic Authentication should only be used for testing!
  • 23. OAuth 1.0a First install the WP OAuth Plugin: https://github.com/WP-API/OAuth1 OAuth is outside of the scope of this talk. However, it should be used instead of HTTP Basic Auth when building external applications that interact with the API. Rather than giving someone an account on your site, you can give them temporary access with OAuth.
  • 24. Create A Post POST /wp-json/posts/ curl -X POST -H “Content-Type: application/json” -d ‘! {! "title": “Title",! “content_raw": “Post content"! }! ‘ -u admin:password http://example.com/wp-json/posts/ Notice we are using a POST request this time.
  • 25. Taxonomies GET /wp-json/taxonomies [! {! "name": "Categories",! "slug": "category",! "labels": {},! "types": { /* Registered post types */ },! "show_cloud": true,! "hierarchical": true,! "meta": {}! },! {! "name": "Tags",! "slug": "post_tag",! "labels": {},! "types": { /* Registered post types */ },! "show_cloud": true,! "hierarchical": false,! "meta": {}! }! }! ]
  • 26. Taxonomy GET /wp-json/taxonomies/<taxonomy> {! "name": "Categories",! "slug": "category",! "labels": {},! "types": { /* Registered post types */ },! "show_cloud": true,! "hierarchical": true,! "meta": {}! }
  • 27. Taxonomy Terms GET /wp-json/taxonomies/<taxonomy>/terms [! {! "ID": 1,! "name": "Books",! "slug": "books",! "description": "",! "parent": null,! "count": 1,! "link": "http://example.com/category/books/",! "meta": {}! },! {! "ID": 2,! "name": "Products",! "slug": "products",! "description": "",! "parent": null,! "count": 1,! "link": "http://example.com/category/products/",! "meta": {}! }! ]
  • 28. Build Your Own Routes and Endpoints WP API is very extensible (custom post types!) http://wp-api.org/guides/extending.html
  • 29. What can I do with the JSON REST API for WordPress?
  • 30. JavaScript Interact with your (or someone else’s) WordPress install with JavaScript. Backbone.js Client: https://github.com/WP-API/client-js ! ! Node.js Client: https://github.com/kadamwhite/wordpress-rest-api !
  • 31. Backbone.js • Backbone.js is a JavaScript framework that lets you structure code in terms of models, views, and collections. It works great with RESTful JSON API’s.
  • 32. _s_backbone • _s or underscores is a popular starter theme by Automattic: https://github.com/automattic/_s • _s_backbone is an _s fork that powers post loops using the WP API Backbone client
  • 34. What does this mean? • It means _s_backbone is a starter theme with infinite scroll built-in using the WP API Backbone client. • Infinite scroll is the concept of loading multiple rounds of entities without reloading the page. Let’s look at some code!
  • 35. This is some JavaScript you could add to a theme or plugin to display your site’s posts. You will first need to have JSON REST API for WordPress installed and the “wp-api” JavaScript dependency enqueued. functions.php: js/scripts.js:
  • 36. If you learned nothing so far, know this: You can do amazing things with the JSON REST API for WordPress. With core integration and ~23% of the web using this API in the near future, you will have much easier access to data across the web.
  • 37. Questions? @tlovett12! taylor.lovett@10up.com! taylorlovett.com We need to send a PUT request to this endpoint with our post data. Of course we must authenticate before doing this.