SlideShare a Scribd company logo
1 of 43
Download to read offline
KATHLEEN VIGNOS
@kathleencodes
WIREDandtheWPRESTAPI
IN THE LOOP? THIRD PARTY? CUSTOM JSON? SYNCH’ING? EXTERNAL? SPA?
IN THE LOOP? THIRD PARTY? CUSTOM JSON? SYNCH’ING? EXTERNAL? SPA?
IN THE LOOP? THIRD PARTY? CUSTOM JSON? SYNCH’ING? EXTERNAL? SPA?
THIRD PARTY? CUSTOM JSON?
SYNCH’ING? EXTERNAL? SPA?
LATEST NEWS?
? ??
? ? ?
NOPE.IN THE LOOP?
IN THE LOOP? THIRD PARTY? CUSTOM JSON? SYNCH’ING? EXTERNAL? SPA?
LATEST NEWS?
LATEST NEWS?
IN THE LOOP? THIRD PARTY? CUSTOM JSON? SYNCH’ING? EXTERNAL? SPA?
Example: Latest News
• WIRED module used
everywhere
• WP REST API req’d
additional HTTP request
• needs deduping
(sometimes)
LATEST NEWS?
LATEST NEWS? THIRD PARTY? CUSTOM JSON? SYNCH’ING? EXTERNAL? SPA?
YES.
ELEMENTARY,
MY DEAR.
THIRD PARTY?
GET REQUEST
TO /POSTS/
ENDPOINT
WELL, SORT OF
LATEST NEWS? THIRD PARTY? CUSTOM JSON? SYNCH’ING? EXTERNAL? SPA?
Example:
• include vendor js
• add outbrain data
attributes
• fallback: WP REST API
LATEST NEWS? THIRD PARTY? CUSTOM JSON? SYNCH’ING? EXTERNAL? SPA?
var apiUrl = 'http://' + location.host + '/wp-json/wp/v2/posts/';
// Fallback if OBR not available
if ( typeof OBR === 'undefined' ) {
$( getAll('poweredByOutbrain') ).remove();
return $.getJSON( apiUrl, function(res) {
if ( elementId === 'we-recommend' ) {
return smart.rec( get('we-recommend') );
}
}
}
LATEST NEWS? THIRD PARTY? CUSTOM JSON? SYNCH’ING? EXTERNAL? SPA?
smart = (function() {
var mostRecent = function( target, apiUrl ) {
function render( res ) {
var frag = document.createDocumentFragment();
var data = res.slice( 0, 5 );
data.forEach(function( value, i ) {
var currLi = document.createElement('li');
....
frag.appendChild( currLi );
});
return target.appendChild( frag );
}
return $.getJSON( apiUrl, render );
};
return {
rec: mostRecent
};
}());
YES, WITH MY
CONDOLENCESCUSTOM JSON?
GET REQUEST
TO CUSTOM
ENDPOINT
THIRD PARTY? CUSTOM JSON? SYNCH’ING? EXTERNAL? SPA?LATEST NEWS?
THIRD PARTY? CUSTOM JSON? SYNCH’ING? EXTERNAL? SPA?
Example:
• Custom endpoint
• Parse WP post content to
markdown
• Output in Apple Native
Format
LATEST NEWS?
LATEST NEWS? THIRD PARTY? CUSTOM JSON? SYNCH’ING? EXTERNAL? SPA?
register_rest_route( 'apple-news/v2', '/post/(?P<post_id>w+)', array(
'methods' => WP_REST_Server::READABLE,
'callback' => array( $this, 'rest_post_output' ),
'args' => array(
'bundle',
'images',
'template',
),
) );
LATEST NEWS? THIRD PARTY? CUSTOM JSON? SYNCH’ING? EXTERNAL? SPA?
Apple News Plugin ParserWP DB
LATEST NEWS? THIRD PARTY? CUSTOM JSON? SYNCH’ING? EXTERNAL? SPA?
Apple News Plugin Parser Apple News Custom JSON
LATEST NEWS? THIRD PARTY? CUSTOM JSON? SYNCH’ING? EXTERNAL? SPA?
Apple News Plugin Parser Apple News Custom JSON
{
"role": "gallery",
"layout": "carouselLayoutNoTop",
"items": [
{
"path": "http://www.wired.com/wp-content/uploads/2016/01/CES_07-NEW-
NEW2.jpg",
"URL": "bundle://CES_07-NEW-NEW2.jpg",
"caption": "Drones as far as the eye can see."
},
{
"path": "http://www.wired.com/wp-content/uploads/2016/01/CES_11-NEW-
NEW2.jpg",
"URL": "bundle://CES_11-NEW-NEW2.jpg",
"caption": "There are still some old-school immersion tactics. This
attendee is sitting through a pre-programmed routine to lower his heart
rate and help him feel more relaxed."
}
]
}
LATEST NEWS? THIRD PARTY? CUSTOM JSON? SYNCH’ING? EXTERNAL? SPA?
Apple News Preview
(simulators)
Apple News Custom JSON
YEP, WE CAN
AUTOMATE
THAT
SYNCH’ING?
POST REQUEST
TO CREATE
POSTS ON
PUBLISH HOOK
THIRD PARTY? CUSTOM JSON? SYNCH’ING? EXTERNAL? SPA?LATEST NEWS?
THIRD PARTY? CUSTOM JSON? SYNCH’ING? EXTERNAL? SPA?
Example: Betta Beta Data Getta
• Publish/update hook
• Make POST request via
WP REST API with post_id
LIVE: www.wired.com
BETA: beta.wired.com
1. data-push
1
LATEST NEWS?
THIRD PARTY? CUSTOM JSON? SYNCH’ING? EXTERNAL? SPA?
Example: Betta Beta Data Getta
• Validate request
• Make GET request via WP
REST API for post_id
• Create post
LIVE: www.wired.com
BETA: beta.wired.com
2. data-pull
2
LATEST NEWS?
LATEST NEWS? THIRD PARTY? CUSTOM JSON? SYNCH’ING? EXTERNAL? SPA?
public function __construct() {
add_action( 'wp_insert_post', array( $this, 'notify_beta_site_post' ),
100, 3 );
}
public function notify_beta_site_post( $post_id, $post, $update ) {
$this->update_object( $post_id, 'post' );
}
public function update_object( $post_id, $object ) {
$this->make_request( $post_id, $object );
}
LATEST NEWS? THIRD PARTY? CUSTOM JSON? SYNCH’ING? EXTERNAL? SPA?
public function make_request( $id, $object ) {
// example: 'http://beta.wired.com/?beta-push=1';
$request_url = $this->get_request_url();
$request_args = array(
'body' => array(
'id' => absint( $id ),
'object' => $object,
),
'headers' => array(
'api-key' => DATA_PUSH_API_KEY,
'api-secret' => DATA_PUSH_API_SECRET,
),
'blocking' => false,
);
$result = wp_remote_post(
$request_url,
$request_args
);
}
LATEST NEWS? THIRD PARTY? CUSTOM JSON? SYNCH’ING? EXTERNAL? SPA?
public function __construct() {
add_action( 'init', array( $this, 'route_pull_request' ), 11 );
}
public function route_pull_request() {
if ( ! $this->validate_request() ) {
return;
}
$id = $this->get_request_id();
$object = $this->get_request_object(); // ex: ‘post’
$data = $this->get_data( $id, 'post' );
$result = $this->save_response_post( $id, $data );
exit();
}
LATEST NEWS? THIRD PARTY? CUSTOM JSON? SYNCH’ING? EXTERNAL? SPA?
public function get_data( $id, $object ) {
$data = array();
$request_url = get_request_url_post( $id );
// example: https://www.wired.com/wp-json/posts/1234567?context=edit
$response = wp_remote_get(
$request_url,
array_merge(
$args
)
);
if ( 200 === (int) wp_remote_retrieve_response_code( $response ) ) {
$body = json_decode( wp_remote_retrieve_body( $response ), true );
// Check to make sure the data looks right
if ( isset( $body['ID'], $body['title'] ) ) {
$data = $body;
}
}
return $data;
}
LATEST NEWS? THIRD PARTY? CUSTOM JSON? SYNCH’ING? EXTERNAL? SPA?
public function save_response_post( $id, $data ) {
$existing_post = get_post( $id );
$prepared_data = $this->prepare_post_data( $data, $id );
if ( empty( $existing_post ) ) {
$result = wp_insert_post( $prepared_data );
} else {
$result = wp_update_post( $prepared_data );
}
foreach ( $data['post_meta'] as $meta ) {
// Note that this will break serialized meta data. Our meta data
does not include serialized meta data.
update_post_meta( $result, $meta['key'], $this->$meta['value'] );
}
return $result;
}
SURE, LET’S
GET CREATIVE!EXTERNAL?
POST REQUEST
TO CREATE
POSTS WITH
AUTH
THIRD PARTY? CUSTOM JSON? SYNCH’ING? EXTERNAL? SPA?LATEST NEWS?
IN THE LOOP? THIRD PARTY? CUSTOM JSON? SYNCH’ING? EXTERNAL? SPA?
Example:
• Node application listens
for requests from Slack
• Bot invited to channel
• Every comment or image
creates a “liveblog
update” post type via WP
REST API
• WP template w/ React
updates content
Liveblog
LATEST NEWS? THIRD PARTY? CUSTOM JSON? SYNCH’ING? EXTERNAL? SPA?
WP DBNode Serverslack
IN THE LOOP? THIRD PARTY? CUSTOM JSON? SYNCH’ING? EXTERNAL? SPA?
LATEST NEWS? THIRD PARTY? CUSTOM JSON? SYNCH’ING? EXTERNAL? SPA?
var addPost = function(preparedMessage, authorID) {
return function() {
// example wpAPIUrl: http://www.wired.com/wp-json/
request( extend(
requestBase,
{
method: 'POST',
uri: config.wpAPIUrl() + '/liveblog',
body: {
type: 'liveblog',
status: 'publish',
title: preparedMessage.ts,
content: preparedMessage.text,
author: authorID,
featured_image: (preparedMessage.featuredImageID) ?
preparedMessage.featuredImageID : 0
}
}
...
LATEST NEWS? THIRD PARTY? CUSTOM JSON? SYNCH’ING? EXTERNAL? SPA?
public function __construct() {
add_action( 'rest_api_init', array( $this, 'register_routes' ) );
}
public function register_routes() {
// example: http://www.wired.com/wp-json/wired/v2/liveblog/1949283/
posts/
// React script hits this endpoint to grab the post objects
register_rest_route( 'wired/v2', '/liveblog/(?P<post_id>w+)/posts',
array(
'methods' => WP_REST_Server::READABLE,
'callback' => array( $this, 'posts_callback' ),
'args' => array(
'post_id',
),
) );
}
LATEST NEWS? THIRD PARTY? CUSTOM JSON? SYNCH’ING? EXTERNAL? SPA?
public function posts_callback( $request ) {
$posts = '';
$params = $request->get_params();
$post_id = ( isset( $params['post_id'] ) ) ?
absint( $params['post_id'] ) : 0;
$posts = array(
'version' => time(),
'posts' => $this->get_liveblog_posts( $post_id )
);
return $posts;
}
LATEST NEWS? THIRD PARTY? CUSTOM JSON? SYNCH’ING? EXTERNAL? SPA?
public function get_liveblog_posts( $post_id ) {
$liveblog_meta = get_post_meta( $post_id, '_liveblog', true );
$query = new WP_Query( array(
'posts_per_page' => 500,
'post_type' => array( wired_get_liveblog_post()->post_type ),
'tax_query' => array( array(
'taxonomy' => wired_get_liveblog_post()->taxonomy,
'field' => 'name',
'terms' => $liveblog_meta['slack-channel-name'],
))
) );
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
$clean_posts[] = array(
'id' => get_the_ID(),
...
);
}}
wp_reset_postdata();
return $clean_posts;
}
YES, BECAUSE
#NODEJSSPA?
JSON OBJECTS
#FTW!
THIRD PARTY? CUSTOM JSON? SYNCH’ING? EXTERNAL? SPA?LATEST NEWS?
THIRD PARTY? CUSTOM JSON? SYNCH’ING? EXTERNAL? SPA?LATEST NEWS?
Example: Homepage Curator (in the future)
• Dashboard: Replace WP posts admin table
ajax with SPA using React components
• Create custom WP REST API endpoint for
homepage and all section fronts
• Front end: Build homepage and section fronts
with React components
• Ability to swipe from section to section
without reload
THIRD PARTY? CUSTOM JSON? SYNCH’ING? EXTERNAL? SPA?LATEST NEWS?
THIRD PARTY? CUSTOM JSON? SYNCH’ING? EXTERNAL? SPA?LATEST NEWS?
THIRD PARTY? CUSTOM JSON? SYNCH’ING? EXTERNAL? SPA?LATEST NEWS?
THIRD PARTY? CUSTOM JSON?
SYNCH’ING? EXTERNAL? SPA?
LATEST NEWS?
NOPE. WELL, SORT OF.
YES, WITH MY
CONDOLENCES
YEP, WE CAN
AUTOMATE
THAT
SURE, LET’S
GET CREATIVE!
YES, BECAUSE
#NODEJS
THIRD PARTY? CUSTOM JSON? SYNCH’ING? EXTERNAL? SPA?
All Code Samples in this Gist:
http://bit.ly/day-of-rest-2016-kv
Slides:
http://www.slideshare.net/kvignos/wired-and-the-wp-rest-api
LATEST NEWS?
KATHLEEN VIGNOS
@kathleencodes
THANKYOU!

More Related Content

What's hot

And now you have two problems. Ruby regular expressions for fun and profit by...
And now you have two problems. Ruby regular expressions for fun and profit by...And now you have two problems. Ruby regular expressions for fun and profit by...
And now you have two problems. Ruby regular expressions for fun and profit by...Codemotion
 
ABC of Perl programming
ABC of Perl programmingABC of Perl programming
ABC of Perl programmingBo Hua Yang
 
Moving from Django Apps to Services
Moving from Django Apps to ServicesMoving from Django Apps to Services
Moving from Django Apps to ServicesCraig Kerstiens
 
Awash in a sea of connections
Awash in a sea of connectionsAwash in a sea of connections
Awash in a sea of connectionsGalen Charlton
 
A Little Backbone For Your App
A Little Backbone For Your AppA Little Backbone For Your App
A Little Backbone For Your AppLuca Mearelli
 
Putting the Cat in the Catalogue: A Feline-Inspired OPAC Theme For Koha
Putting the Cat in the Catalogue: A Feline-Inspired OPAC Theme For KohaPutting the Cat in the Catalogue: A Feline-Inspired OPAC Theme For Koha
Putting the Cat in the Catalogue: A Feline-Inspired OPAC Theme For KohaGalen Charlton
 
Writing Apps the Google-y Way (Brisbane)
Writing Apps the Google-y Way (Brisbane)Writing Apps the Google-y Way (Brisbane)
Writing Apps the Google-y Way (Brisbane)Pamela Fox
 
PerlでWeb API入門
PerlでWeb API入門PerlでWeb API入門
PerlでWeb API入門Yusuke Wada
 
With a Mighty Hammer
With a Mighty HammerWith a Mighty Hammer
With a Mighty HammerBen Scofield
 
Django - 次の一歩 gumiStudy#3
Django - 次の一歩 gumiStudy#3Django - 次の一歩 gumiStudy#3
Django - 次の一歩 gumiStudy#3makoto tsuyuki
 
The effective use of Django ORM
The effective use of Django ORMThe effective use of Django ORM
The effective use of Django ORMYaroslav Muravskyi
 
OSCON Google App Engine Codelab - July 2010
OSCON Google App Engine Codelab - July 2010OSCON Google App Engine Codelab - July 2010
OSCON Google App Engine Codelab - July 2010ikailan
 
RESTful API 제대로 만들기
RESTful API 제대로 만들기RESTful API 제대로 만들기
RESTful API 제대로 만들기Juwon Kim
 
Keeping it Small: Getting to know the Slim Micro Framework
Keeping it Small: Getting to know the Slim Micro FrameworkKeeping it Small: Getting to know the Slim Micro Framework
Keeping it Small: Getting to know the Slim Micro FrameworkJeremy Kendall
 
Hack in the Box Keynote 2006
Hack in the Box Keynote 2006Hack in the Box Keynote 2006
Hack in the Box Keynote 2006Mark Curphey
 
Keeping it small - Getting to know the Slim PHP micro framework
Keeping it small - Getting to know the Slim PHP micro frameworkKeeping it small - Getting to know the Slim PHP micro framework
Keeping it small - Getting to know the Slim PHP micro frameworkJeremy Kendall
 

What's hot (20)

And now you have two problems. Ruby regular expressions for fun and profit by...
And now you have two problems. Ruby regular expressions for fun and profit by...And now you have two problems. Ruby regular expressions for fun and profit by...
And now you have two problems. Ruby regular expressions for fun and profit by...
 
PHP code examples
PHP code examplesPHP code examples
PHP code examples
 
ABC of Perl programming
ABC of Perl programmingABC of Perl programming
ABC of Perl programming
 
Moving from Django Apps to Services
Moving from Django Apps to ServicesMoving from Django Apps to Services
Moving from Django Apps to Services
 
Awash in a sea of connections
Awash in a sea of connectionsAwash in a sea of connections
Awash in a sea of connections
 
A Little Backbone For Your App
A Little Backbone For Your AppA Little Backbone For Your App
A Little Backbone For Your App
 
Putting the Cat in the Catalogue: A Feline-Inspired OPAC Theme For Koha
Putting the Cat in the Catalogue: A Feline-Inspired OPAC Theme For KohaPutting the Cat in the Catalogue: A Feline-Inspired OPAC Theme For Koha
Putting the Cat in the Catalogue: A Feline-Inspired OPAC Theme For Koha
 
API Design - 3rd Edition
API Design - 3rd EditionAPI Design - 3rd Edition
API Design - 3rd Edition
 
Php My Sql
Php My SqlPhp My Sql
Php My Sql
 
Writing Apps the Google-y Way (Brisbane)
Writing Apps the Google-y Way (Brisbane)Writing Apps the Google-y Way (Brisbane)
Writing Apps the Google-y Way (Brisbane)
 
PerlでWeb API入門
PerlでWeb API入門PerlでWeb API入門
PerlでWeb API入門
 
With a Mighty Hammer
With a Mighty HammerWith a Mighty Hammer
With a Mighty Hammer
 
Django - 次の一歩 gumiStudy#3
Django - 次の一歩 gumiStudy#3Django - 次の一歩 gumiStudy#3
Django - 次の一歩 gumiStudy#3
 
The effective use of Django ORM
The effective use of Django ORMThe effective use of Django ORM
The effective use of Django ORM
 
OSCON Google App Engine Codelab - July 2010
OSCON Google App Engine Codelab - July 2010OSCON Google App Engine Codelab - July 2010
OSCON Google App Engine Codelab - July 2010
 
RESTful API 제대로 만들기
RESTful API 제대로 만들기RESTful API 제대로 만들기
RESTful API 제대로 만들기
 
Developing apps using Perl
Developing apps using PerlDeveloping apps using Perl
Developing apps using Perl
 
Keeping it Small: Getting to know the Slim Micro Framework
Keeping it Small: Getting to know the Slim Micro FrameworkKeeping it Small: Getting to know the Slim Micro Framework
Keeping it Small: Getting to know the Slim Micro Framework
 
Hack in the Box Keynote 2006
Hack in the Box Keynote 2006Hack in the Box Keynote 2006
Hack in the Box Keynote 2006
 
Keeping it small - Getting to know the Slim PHP micro framework
Keeping it small - Getting to know the Slim PHP micro frameworkKeeping it small - Getting to know the Slim PHP micro framework
Keeping it small - Getting to know the Slim PHP micro framework
 

Viewers also liked

WordCamp Kansai 2014_All we really need to know we have learned from wordpress.
WordCamp Kansai 2014_All we really need to know we have learned from wordpress.WordCamp Kansai 2014_All we really need to know we have learned from wordpress.
WordCamp Kansai 2014_All we really need to know we have learned from wordpress.Hiromichi Koga
 
SW6 Associates Work For Us 2016
SW6 Associates Work For Us 2016SW6 Associates Work For Us 2016
SW6 Associates Work For Us 2016James Wilson
 
Short and Long of Data Driven Innovation
Short and Long of Data Driven InnovationShort and Long of Data Driven Innovation
Short and Long of Data Driven InnovationDavid De Roure
 
Angular Remote Conf - Building with Angular & WordPress
Angular Remote Conf - Building with Angular & WordPressAngular Remote Conf - Building with Angular & WordPress
Angular Remote Conf - Building with Angular & WordPressRoy Sivan
 
Advanced Development Workflows
Advanced Development WorkflowsAdvanced Development Workflows
Advanced Development WorkflowsMicah Wood
 
今、WordPress を使う理由
今、WordPress を使う理由今、WordPress を使う理由
今、WordPress を使う理由Naoko Takano
 
Awareness of OER and OEP in Scottish Higher Education Institutions Survey Res...
Awareness of OER and OEP in Scottish Higher Education Institutions Survey Res...Awareness of OER and OEP in Scottish Higher Education Institutions Survey Res...
Awareness of OER and OEP in Scottish Higher Education Institutions Survey Res...OEPScotland
 
Content by the Slice, Information Architecture Workshop - Mobile UXCamp DC 2015
Content by the Slice, Information Architecture Workshop - Mobile UXCamp DC 2015Content by the Slice, Information Architecture Workshop - Mobile UXCamp DC 2015
Content by the Slice, Information Architecture Workshop - Mobile UXCamp DC 2015Anthony D. Paul
 
Czy blogerzy i youtuberzy zmieniają świat?
Czy blogerzy i youtuberzy zmieniają świat?Czy blogerzy i youtuberzy zmieniają świat?
Czy blogerzy i youtuberzy zmieniają świat?Natalia Hatalska
 

Viewers also liked (10)

WordCamp Kansai 2014_All we really need to know we have learned from wordpress.
WordCamp Kansai 2014_All we really need to know we have learned from wordpress.WordCamp Kansai 2014_All we really need to know we have learned from wordpress.
WordCamp Kansai 2014_All we really need to know we have learned from wordpress.
 
SW6 Associates Work For Us 2016
SW6 Associates Work For Us 2016SW6 Associates Work For Us 2016
SW6 Associates Work For Us 2016
 
Short and Long of Data Driven Innovation
Short and Long of Data Driven InnovationShort and Long of Data Driven Innovation
Short and Long of Data Driven Innovation
 
Empowering Generation Z #DigCitSummitUK
Empowering Generation Z #DigCitSummitUKEmpowering Generation Z #DigCitSummitUK
Empowering Generation Z #DigCitSummitUK
 
Angular Remote Conf - Building with Angular & WordPress
Angular Remote Conf - Building with Angular & WordPressAngular Remote Conf - Building with Angular & WordPress
Angular Remote Conf - Building with Angular & WordPress
 
Advanced Development Workflows
Advanced Development WorkflowsAdvanced Development Workflows
Advanced Development Workflows
 
今、WordPress を使う理由
今、WordPress を使う理由今、WordPress を使う理由
今、WordPress を使う理由
 
Awareness of OER and OEP in Scottish Higher Education Institutions Survey Res...
Awareness of OER and OEP in Scottish Higher Education Institutions Survey Res...Awareness of OER and OEP in Scottish Higher Education Institutions Survey Res...
Awareness of OER and OEP in Scottish Higher Education Institutions Survey Res...
 
Content by the Slice, Information Architecture Workshop - Mobile UXCamp DC 2015
Content by the Slice, Information Architecture Workshop - Mobile UXCamp DC 2015Content by the Slice, Information Architecture Workshop - Mobile UXCamp DC 2015
Content by the Slice, Information Architecture Workshop - Mobile UXCamp DC 2015
 
Czy blogerzy i youtuberzy zmieniają świat?
Czy blogerzy i youtuberzy zmieniają świat?Czy blogerzy i youtuberzy zmieniają świat?
Czy blogerzy i youtuberzy zmieniają świat?
 

Similar to WIRED and the WP REST API

WordPress Realtime - WordCamp São Paulo 2015
WordPress Realtime - WordCamp São Paulo 2015WordPress Realtime - WordCamp São Paulo 2015
WordPress Realtime - WordCamp São Paulo 2015Fernando Daciuk
 
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
 
Great Developers Steal
Great Developers StealGreat Developers Steal
Great Developers StealBen Scofield
 
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
 
WordPress REST API hacking
WordPress REST API hackingWordPress REST API hacking
WordPress REST API hackingJeroen van Dijk
 
GraphQL & Relay - 串起前後端世界的橋樑
GraphQL & Relay - 串起前後端世界的橋樑GraphQL & Relay - 串起前後端世界的橋樑
GraphQL & Relay - 串起前後端世界的橋樑Pokai Chang
 
Bootstrat REST APIs with Laravel 5
Bootstrat REST APIs with Laravel 5Bootstrat REST APIs with Laravel 5
Bootstrat REST APIs with Laravel 5Elena Kolevska
 
PHP and Rich Internet Applications
PHP and Rich Internet ApplicationsPHP and Rich Internet Applications
PHP and Rich Internet Applicationselliando dias
 
Kicking off with Zend Expressive and Doctrine ORM (PHP UK 2017)
Kicking off with Zend Expressive and Doctrine ORM (PHP UK 2017)Kicking off with Zend Expressive and Doctrine ORM (PHP UK 2017)
Kicking off with Zend Expressive and Doctrine ORM (PHP UK 2017)James Titcumb
 
Kicking off with Zend Expressive and Doctrine ORM (Sunshine PHP 2017)
Kicking off with Zend Expressive and Doctrine ORM (Sunshine PHP 2017)Kicking off with Zend Expressive and Doctrine ORM (Sunshine PHP 2017)
Kicking off with Zend Expressive and Doctrine ORM (Sunshine PHP 2017)James Titcumb
 
Make WordPress realtime.
Make WordPress realtime.Make WordPress realtime.
Make WordPress realtime.Josh Hillier
 
After max+phonegap
After max+phonegapAfter max+phonegap
After max+phonegapyangdj
 
混搭移动开发:PhoneGap+JQurey+Dreamweaver
混搭移动开发:PhoneGap+JQurey+Dreamweaver混搭移动开发:PhoneGap+JQurey+Dreamweaver
混搭移动开发:PhoneGap+JQurey+Dreamweaveryangdj
 
Doctrine For Beginners
Doctrine For BeginnersDoctrine For Beginners
Doctrine For BeginnersJonathan Wage
 
WordPressでIoTをはじめよう
WordPressでIoTをはじめようWordPressでIoTをはじめよう
WordPressでIoTをはじめようYuriko IKEDA
 
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
 

Similar to WIRED and the WP REST API (20)

WordPress Realtime - WordCamp São Paulo 2015
WordPress Realtime - WordCamp São Paulo 2015WordPress Realtime - WordCamp São Paulo 2015
WordPress Realtime - WordCamp São Paulo 2015
 
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
 
Great Developers Steal
Great Developers StealGreat Developers Steal
Great Developers Steal
 
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
 
WordPress REST API hacking
WordPress REST API hackingWordPress REST API hacking
WordPress REST API hacking
 
GraphQL & Relay - 串起前後端世界的橋樑
GraphQL & Relay - 串起前後端世界的橋樑GraphQL & Relay - 串起前後端世界的橋樑
GraphQL & Relay - 串起前後端世界的橋樑
 
Bootstrat REST APIs with Laravel 5
Bootstrat REST APIs with Laravel 5Bootstrat REST APIs with Laravel 5
Bootstrat REST APIs with Laravel 5
 
PHP and Rich Internet Applications
PHP and Rich Internet ApplicationsPHP and Rich Internet Applications
PHP and Rich Internet Applications
 
Kicking off with Zend Expressive and Doctrine ORM (PHP UK 2017)
Kicking off with Zend Expressive and Doctrine ORM (PHP UK 2017)Kicking off with Zend Expressive and Doctrine ORM (PHP UK 2017)
Kicking off with Zend Expressive and Doctrine ORM (PHP UK 2017)
 
Kicking off with Zend Expressive and Doctrine ORM (Sunshine PHP 2017)
Kicking off with Zend Expressive and Doctrine ORM (Sunshine PHP 2017)Kicking off with Zend Expressive and Doctrine ORM (Sunshine PHP 2017)
Kicking off with Zend Expressive and Doctrine ORM (Sunshine PHP 2017)
 
Symfony tips and tricks
Symfony tips and tricksSymfony tips and tricks
Symfony tips and tricks
 
Make WordPress realtime.
Make WordPress realtime.Make WordPress realtime.
Make WordPress realtime.
 
Django
DjangoDjango
Django
 
After max+phonegap
After max+phonegapAfter max+phonegap
After max+phonegap
 
混搭移动开发:PhoneGap+JQurey+Dreamweaver
混搭移动开发:PhoneGap+JQurey+Dreamweaver混搭移动开发:PhoneGap+JQurey+Dreamweaver
混搭移动开发:PhoneGap+JQurey+Dreamweaver
 
JSON and the APInauts
JSON and the APInautsJSON and the APInauts
JSON and the APInauts
 
Doctrine For Beginners
Doctrine For BeginnersDoctrine For Beginners
Doctrine For Beginners
 
WordPressでIoTをはじめよう
WordPressでIoTをはじめようWordPressでIoTをはじめよう
WordPressでIoTをはじめよう
 
Coding Ajax
Coding AjaxCoding Ajax
Coding Ajax
 
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
 

More from kvignos

LeadingEng Growing the Next Generation of Leaders, Kathleen Vignos.pdf
LeadingEng Growing the Next Generation of Leaders, Kathleen Vignos.pdfLeadingEng Growing the Next Generation of Leaders, Kathleen Vignos.pdf
LeadingEng Growing the Next Generation of Leaders, Kathleen Vignos.pdfkvignos
 
Keeping up your technical skills as a manager
Keeping up your technical skills as a managerKeeping up your technical skills as a manager
Keeping up your technical skills as a managerkvignos
 
How to keep up your technical skills without annoying your team(s)
How to keep up your technical skills without annoying your team(s)How to keep up your technical skills without annoying your team(s)
How to keep up your technical skills without annoying your team(s)kvignos
 
5 leadership skills every engineer needs - North Bay Python
5 leadership skills every engineer needs - North Bay Python5 leadership skills every engineer needs - North Bay Python
5 leadership skills every engineer needs - North Bay Pythonkvignos
 
Managing engineering teams through constant change final
Managing engineering teams through constant change finalManaging engineering teams through constant change final
Managing engineering teams through constant change finalkvignos
 
WordCamp SF 2014 - WIRED Migration Project
WordCamp SF 2014 - WIRED Migration ProjectWordCamp SF 2014 - WIRED Migration Project
WordCamp SF 2014 - WIRED Migration Projectkvignos
 

More from kvignos (6)

LeadingEng Growing the Next Generation of Leaders, Kathleen Vignos.pdf
LeadingEng Growing the Next Generation of Leaders, Kathleen Vignos.pdfLeadingEng Growing the Next Generation of Leaders, Kathleen Vignos.pdf
LeadingEng Growing the Next Generation of Leaders, Kathleen Vignos.pdf
 
Keeping up your technical skills as a manager
Keeping up your technical skills as a managerKeeping up your technical skills as a manager
Keeping up your technical skills as a manager
 
How to keep up your technical skills without annoying your team(s)
How to keep up your technical skills without annoying your team(s)How to keep up your technical skills without annoying your team(s)
How to keep up your technical skills without annoying your team(s)
 
5 leadership skills every engineer needs - North Bay Python
5 leadership skills every engineer needs - North Bay Python5 leadership skills every engineer needs - North Bay Python
5 leadership skills every engineer needs - North Bay Python
 
Managing engineering teams through constant change final
Managing engineering teams through constant change finalManaging engineering teams through constant change final
Managing engineering teams through constant change final
 
WordCamp SF 2014 - WIRED Migration Project
WordCamp SF 2014 - WIRED Migration ProjectWordCamp SF 2014 - WIRED Migration Project
WordCamp SF 2014 - WIRED Migration Project
 

Recently uploaded

Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLScyllaDB
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024Lorenzo Miniero
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsSergiu Bodiu
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024Lonnie McRorey
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxLoriGlavin3
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 3652toLead Limited
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity PlanDatabarracks
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxNavinnSomaal
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsRizwan Syed
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brandgvaughan
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubKalema Edgar
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek SchlawackFwdays
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxLoriGlavin3
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Commit University
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionDilum Bandara
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxLoriGlavin3
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfAlex Barbosa Coqueiro
 

Recently uploaded (20)

Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQL
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platforms
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024
 
DMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special EditionDMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special Edition
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity Plan
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptx
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL Certs
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brand
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding Club
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An Introduction
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdf
 

WIRED and the WP REST API

  • 2.
  • 3. IN THE LOOP? THIRD PARTY? CUSTOM JSON? SYNCH’ING? EXTERNAL? SPA?
  • 4. IN THE LOOP? THIRD PARTY? CUSTOM JSON? SYNCH’ING? EXTERNAL? SPA?
  • 5. IN THE LOOP? THIRD PARTY? CUSTOM JSON? SYNCH’ING? EXTERNAL? SPA?
  • 6. THIRD PARTY? CUSTOM JSON? SYNCH’ING? EXTERNAL? SPA? LATEST NEWS? ? ?? ? ? ?
  • 7. NOPE.IN THE LOOP? IN THE LOOP? THIRD PARTY? CUSTOM JSON? SYNCH’ING? EXTERNAL? SPA? LATEST NEWS? LATEST NEWS?
  • 8. IN THE LOOP? THIRD PARTY? CUSTOM JSON? SYNCH’ING? EXTERNAL? SPA? Example: Latest News • WIRED module used everywhere • WP REST API req’d additional HTTP request • needs deduping (sometimes) LATEST NEWS?
  • 9. LATEST NEWS? THIRD PARTY? CUSTOM JSON? SYNCH’ING? EXTERNAL? SPA? YES. ELEMENTARY, MY DEAR. THIRD PARTY? GET REQUEST TO /POSTS/ ENDPOINT WELL, SORT OF
  • 10. LATEST NEWS? THIRD PARTY? CUSTOM JSON? SYNCH’ING? EXTERNAL? SPA? Example: • include vendor js • add outbrain data attributes • fallback: WP REST API
  • 11. LATEST NEWS? THIRD PARTY? CUSTOM JSON? SYNCH’ING? EXTERNAL? SPA? var apiUrl = 'http://' + location.host + '/wp-json/wp/v2/posts/'; // Fallback if OBR not available if ( typeof OBR === 'undefined' ) { $( getAll('poweredByOutbrain') ).remove(); return $.getJSON( apiUrl, function(res) { if ( elementId === 'we-recommend' ) { return smart.rec( get('we-recommend') ); } } }
  • 12. LATEST NEWS? THIRD PARTY? CUSTOM JSON? SYNCH’ING? EXTERNAL? SPA? smart = (function() { var mostRecent = function( target, apiUrl ) { function render( res ) { var frag = document.createDocumentFragment(); var data = res.slice( 0, 5 ); data.forEach(function( value, i ) { var currLi = document.createElement('li'); .... frag.appendChild( currLi ); }); return target.appendChild( frag ); } return $.getJSON( apiUrl, render ); }; return { rec: mostRecent }; }());
  • 13. YES, WITH MY CONDOLENCESCUSTOM JSON? GET REQUEST TO CUSTOM ENDPOINT THIRD PARTY? CUSTOM JSON? SYNCH’ING? EXTERNAL? SPA?LATEST NEWS?
  • 14. THIRD PARTY? CUSTOM JSON? SYNCH’ING? EXTERNAL? SPA? Example: • Custom endpoint • Parse WP post content to markdown • Output in Apple Native Format LATEST NEWS?
  • 15. LATEST NEWS? THIRD PARTY? CUSTOM JSON? SYNCH’ING? EXTERNAL? SPA? register_rest_route( 'apple-news/v2', '/post/(?P<post_id>w+)', array( 'methods' => WP_REST_Server::READABLE, 'callback' => array( $this, 'rest_post_output' ), 'args' => array( 'bundle', 'images', 'template', ), ) );
  • 16. LATEST NEWS? THIRD PARTY? CUSTOM JSON? SYNCH’ING? EXTERNAL? SPA? Apple News Plugin ParserWP DB
  • 17. LATEST NEWS? THIRD PARTY? CUSTOM JSON? SYNCH’ING? EXTERNAL? SPA? Apple News Plugin Parser Apple News Custom JSON
  • 18. LATEST NEWS? THIRD PARTY? CUSTOM JSON? SYNCH’ING? EXTERNAL? SPA? Apple News Plugin Parser Apple News Custom JSON { "role": "gallery", "layout": "carouselLayoutNoTop", "items": [ { "path": "http://www.wired.com/wp-content/uploads/2016/01/CES_07-NEW- NEW2.jpg", "URL": "bundle://CES_07-NEW-NEW2.jpg", "caption": "Drones as far as the eye can see." }, { "path": "http://www.wired.com/wp-content/uploads/2016/01/CES_11-NEW- NEW2.jpg", "URL": "bundle://CES_11-NEW-NEW2.jpg", "caption": "There are still some old-school immersion tactics. This attendee is sitting through a pre-programmed routine to lower his heart rate and help him feel more relaxed." } ] }
  • 19. LATEST NEWS? THIRD PARTY? CUSTOM JSON? SYNCH’ING? EXTERNAL? SPA? Apple News Preview (simulators) Apple News Custom JSON
  • 20. YEP, WE CAN AUTOMATE THAT SYNCH’ING? POST REQUEST TO CREATE POSTS ON PUBLISH HOOK THIRD PARTY? CUSTOM JSON? SYNCH’ING? EXTERNAL? SPA?LATEST NEWS?
  • 21. THIRD PARTY? CUSTOM JSON? SYNCH’ING? EXTERNAL? SPA? Example: Betta Beta Data Getta • Publish/update hook • Make POST request via WP REST API with post_id LIVE: www.wired.com BETA: beta.wired.com 1. data-push 1 LATEST NEWS?
  • 22. THIRD PARTY? CUSTOM JSON? SYNCH’ING? EXTERNAL? SPA? Example: Betta Beta Data Getta • Validate request • Make GET request via WP REST API for post_id • Create post LIVE: www.wired.com BETA: beta.wired.com 2. data-pull 2 LATEST NEWS?
  • 23. LATEST NEWS? THIRD PARTY? CUSTOM JSON? SYNCH’ING? EXTERNAL? SPA? public function __construct() { add_action( 'wp_insert_post', array( $this, 'notify_beta_site_post' ), 100, 3 ); } public function notify_beta_site_post( $post_id, $post, $update ) { $this->update_object( $post_id, 'post' ); } public function update_object( $post_id, $object ) { $this->make_request( $post_id, $object ); }
  • 24. LATEST NEWS? THIRD PARTY? CUSTOM JSON? SYNCH’ING? EXTERNAL? SPA? public function make_request( $id, $object ) { // example: 'http://beta.wired.com/?beta-push=1'; $request_url = $this->get_request_url(); $request_args = array( 'body' => array( 'id' => absint( $id ), 'object' => $object, ), 'headers' => array( 'api-key' => DATA_PUSH_API_KEY, 'api-secret' => DATA_PUSH_API_SECRET, ), 'blocking' => false, ); $result = wp_remote_post( $request_url, $request_args ); }
  • 25. LATEST NEWS? THIRD PARTY? CUSTOM JSON? SYNCH’ING? EXTERNAL? SPA? public function __construct() { add_action( 'init', array( $this, 'route_pull_request' ), 11 ); } public function route_pull_request() { if ( ! $this->validate_request() ) { return; } $id = $this->get_request_id(); $object = $this->get_request_object(); // ex: ‘post’ $data = $this->get_data( $id, 'post' ); $result = $this->save_response_post( $id, $data ); exit(); }
  • 26. LATEST NEWS? THIRD PARTY? CUSTOM JSON? SYNCH’ING? EXTERNAL? SPA? public function get_data( $id, $object ) { $data = array(); $request_url = get_request_url_post( $id ); // example: https://www.wired.com/wp-json/posts/1234567?context=edit $response = wp_remote_get( $request_url, array_merge( $args ) ); if ( 200 === (int) wp_remote_retrieve_response_code( $response ) ) { $body = json_decode( wp_remote_retrieve_body( $response ), true ); // Check to make sure the data looks right if ( isset( $body['ID'], $body['title'] ) ) { $data = $body; } } return $data; }
  • 27. LATEST NEWS? THIRD PARTY? CUSTOM JSON? SYNCH’ING? EXTERNAL? SPA? public function save_response_post( $id, $data ) { $existing_post = get_post( $id ); $prepared_data = $this->prepare_post_data( $data, $id ); if ( empty( $existing_post ) ) { $result = wp_insert_post( $prepared_data ); } else { $result = wp_update_post( $prepared_data ); } foreach ( $data['post_meta'] as $meta ) { // Note that this will break serialized meta data. Our meta data does not include serialized meta data. update_post_meta( $result, $meta['key'], $this->$meta['value'] ); } return $result; }
  • 28. SURE, LET’S GET CREATIVE!EXTERNAL? POST REQUEST TO CREATE POSTS WITH AUTH THIRD PARTY? CUSTOM JSON? SYNCH’ING? EXTERNAL? SPA?LATEST NEWS?
  • 29. IN THE LOOP? THIRD PARTY? CUSTOM JSON? SYNCH’ING? EXTERNAL? SPA? Example: • Node application listens for requests from Slack • Bot invited to channel • Every comment or image creates a “liveblog update” post type via WP REST API • WP template w/ React updates content Liveblog
  • 30. LATEST NEWS? THIRD PARTY? CUSTOM JSON? SYNCH’ING? EXTERNAL? SPA? WP DBNode Serverslack
  • 31. IN THE LOOP? THIRD PARTY? CUSTOM JSON? SYNCH’ING? EXTERNAL? SPA?
  • 32. LATEST NEWS? THIRD PARTY? CUSTOM JSON? SYNCH’ING? EXTERNAL? SPA? var addPost = function(preparedMessage, authorID) { return function() { // example wpAPIUrl: http://www.wired.com/wp-json/ request( extend( requestBase, { method: 'POST', uri: config.wpAPIUrl() + '/liveblog', body: { type: 'liveblog', status: 'publish', title: preparedMessage.ts, content: preparedMessage.text, author: authorID, featured_image: (preparedMessage.featuredImageID) ? preparedMessage.featuredImageID : 0 } } ...
  • 33. LATEST NEWS? THIRD PARTY? CUSTOM JSON? SYNCH’ING? EXTERNAL? SPA? public function __construct() { add_action( 'rest_api_init', array( $this, 'register_routes' ) ); } public function register_routes() { // example: http://www.wired.com/wp-json/wired/v2/liveblog/1949283/ posts/ // React script hits this endpoint to grab the post objects register_rest_route( 'wired/v2', '/liveblog/(?P<post_id>w+)/posts', array( 'methods' => WP_REST_Server::READABLE, 'callback' => array( $this, 'posts_callback' ), 'args' => array( 'post_id', ), ) ); }
  • 34. LATEST NEWS? THIRD PARTY? CUSTOM JSON? SYNCH’ING? EXTERNAL? SPA? public function posts_callback( $request ) { $posts = ''; $params = $request->get_params(); $post_id = ( isset( $params['post_id'] ) ) ? absint( $params['post_id'] ) : 0; $posts = array( 'version' => time(), 'posts' => $this->get_liveblog_posts( $post_id ) ); return $posts; }
  • 35. LATEST NEWS? THIRD PARTY? CUSTOM JSON? SYNCH’ING? EXTERNAL? SPA? public function get_liveblog_posts( $post_id ) { $liveblog_meta = get_post_meta( $post_id, '_liveblog', true ); $query = new WP_Query( array( 'posts_per_page' => 500, 'post_type' => array( wired_get_liveblog_post()->post_type ), 'tax_query' => array( array( 'taxonomy' => wired_get_liveblog_post()->taxonomy, 'field' => 'name', 'terms' => $liveblog_meta['slack-channel-name'], )) ) ); if ( $query->have_posts() ) { while ( $query->have_posts() ) { $query->the_post(); $clean_posts[] = array( 'id' => get_the_ID(), ... ); }} wp_reset_postdata(); return $clean_posts; }
  • 36. YES, BECAUSE #NODEJSSPA? JSON OBJECTS #FTW! THIRD PARTY? CUSTOM JSON? SYNCH’ING? EXTERNAL? SPA?LATEST NEWS?
  • 37. THIRD PARTY? CUSTOM JSON? SYNCH’ING? EXTERNAL? SPA?LATEST NEWS? Example: Homepage Curator (in the future) • Dashboard: Replace WP posts admin table ajax with SPA using React components • Create custom WP REST API endpoint for homepage and all section fronts • Front end: Build homepage and section fronts with React components • Ability to swipe from section to section without reload
  • 38. THIRD PARTY? CUSTOM JSON? SYNCH’ING? EXTERNAL? SPA?LATEST NEWS?
  • 39. THIRD PARTY? CUSTOM JSON? SYNCH’ING? EXTERNAL? SPA?LATEST NEWS?
  • 40. THIRD PARTY? CUSTOM JSON? SYNCH’ING? EXTERNAL? SPA?LATEST NEWS?
  • 41. THIRD PARTY? CUSTOM JSON? SYNCH’ING? EXTERNAL? SPA? LATEST NEWS? NOPE. WELL, SORT OF. YES, WITH MY CONDOLENCES YEP, WE CAN AUTOMATE THAT SURE, LET’S GET CREATIVE! YES, BECAUSE #NODEJS
  • 42. THIRD PARTY? CUSTOM JSON? SYNCH’ING? EXTERNAL? SPA? All Code Samples in this Gist: http://bit.ly/day-of-rest-2016-kv Slides: http://www.slideshare.net/kvignos/wired-and-the-wp-rest-api LATEST NEWS?