Slide 1

Slide 1 text

WP REST API by Rachel Baker Build a Theme with the https://speakerdeck.com/rachelbaker/build-a-theme-with-the-wp-rest-api

Slide 2

Slide 2 text

Why Would You Build a Theme This Way? 1. Because you finally can 2. Your content needs to update or change dynamically 3. You want to separate your business logic from your views 4. You don’t like PHP 5. You just spent the past 2 years of your life working on the WP REST API project

Slide 3

Slide 3 text

Requirements 1. WordPress version 4.4 2. WP REST API plugin version 2.0

Slide 4

Slide 4 text

Scripts Used 1. jQuery 2. Underscore.js
 3. Director

Slide 5

Slide 5 text

What We Are Building

Slide 6

Slide 6 text

Theme Setup

Slide 7

Slide 7 text

Theme Structure

Slide 8

Slide 8 text

Main Template Scaffold index.php

Slide 9

Slide 9 text

Load Scripts functions.php

Slide 10

Slide 10 text

Define the Routes main.js

Slide 11

Slide 11 text

Define Variables & Click Handler for Logo main.js

Slide 12

Slide 12 text

Recap 1. Created our theme directory and files 2. Filled-in the header and footer logic in our single template file (index.php) 3. Enqueued our JavaScript and CSS files (functions.php) 4. Defined our routes and initialized our client-side router (main.js) 5. Added an event handler to route visitors that click on our logo image in the header to our homepage (main.js)

Slide 13

Slide 13 text

GET Posts Collection

Slide 14

Slide 14 text

Add Underscore Template index.php

Slide 15

Slide 15 text

Fetch Posts Response & Pass Data to Underscore Template main.js

Slide 16

Slide 16 text

Display Posts from Response index.php

Slide 17

Slide 17 text

Recap 1. Created a Underscore JS template with an id of #posts-tmpl to handle the display of our Posts list (index.php) 2. Filled-in the logic in our listPostswithPagination callback to send a GET request to the WP REST API Posts endpoint and pass the response data to our Underscore template (main.js) 3. Added the presentation logic to our Underscore JS template that loops through the data passed, displaying the title and excerpt for each Post (index.php)

Slide 18

Slide 18 text

Homepage (‘/‘)

Slide 19

Slide 19 text

Paginate Posts Collection

Slide 20

Slide 20 text

Determine Paged Location from Posts Response & Pass Pagination to Underscore Template main.js

Slide 21

Slide 21 text

Display Pagination Navigation index.php

Slide 22

Slide 22 text

Recap 1. Added logic in our listPostswithPagination to determine if pagination values are relevant and, if so, include them in the data passed to the #posts-tmpl Underscore template (main.js) 2. Added the presentation logic to the #posts-tmpl Underscore JS template that displays the pagination links at the bottom of the Posts list (index.php)

Slide 23

Slide 23 text

Paged View (‘/2‘)

Slide 24

Slide 24 text

GET Single Post

Slide 25

Slide 25 text

Add Underscore Template index.php

Slide 26

Slide 26 text

Fetch a Single Post Response with Embedded Data & Pass All Data to the Underscore Template main.js

Slide 27

Slide 27 text

Display Single Post with Related Data from Response index.php

Slide 28

Slide 28 text

Recap 1. Created our Underscore JS template with an id of #post-tmpl to handle the display of a single Post (index.php) 2. Filled-in the logic in our viewPost callback to send a GET request to the WP REST API Posts endpoint with query params for the Post slug and embedding related data. We pass the response data to our #post-tmpl Underscore template (main.js) 3. Added the presentation logic to our #post-tmpl Underscore JS template that displays the featured image, title, author avatar, author name, and content for the Post (index.php)

Slide 29

Slide 29 text

Single Post View (‘/news/marge-vs-the-monorail’)

Slide 30

Slide 30 text

Resources 1. WP REST API (endpoints) 
 https://github.com/WP-API/WP-API 2. Director (used for routing) 
 https://github.com/flatiron/director 3. Demo Theme Files 
 https://github.com/rachelbaker/wcus-demo 4. Slides
 https://speakerdeck.com/rachelbaker/build-a-theme-with-the-wp-rest-api

Slide 31

Slide 31 text

Thank You Rachel Baker @rachelbaker Lead Engineer thewirecutter.com thesweethome.com