SlideShare a Scribd company logo
1 of 44
Download to read offline
SanDeigoWordCamp2018
WordPress+React
=
Adam Rasheed
Using WordPress as a
Backend for Your React
Project
SanDiegoWordCamp2018
A few words about Me
• Front-end Developer based in San Diego
• Started my career doing custom WordPress sites
(and before that, custom MySpace pages).
• Now I do a lot more work in vanilla
HTML,CSS,JS (ES6 is siiick), and some PHP and
React work.
• On a quest to use React to make my site an
single-page web app.
SanDiegoWordCamp2018
adamrasheed.com
• Currently based in Jekyll (static-site generator)
• I want to get into posting blogs more regularly
• I want an easier time publishing blog articles and case studies. (I’ve found
I’m most consistent using the WordPress admin dashboard)
• Jekyll requires blogging in markdown, and then publishing
• Want an SPA (mostly for rad page transitions)
• Looking for the perfect solution
SanDiegoWordCamp2018
Solution: Headless CMS
• React for Front-end
• WordPress for Backend
SanDiegoWordCamp2018
Let’s Learn How!
SanDiegoWordCamp2018
Preface: You Don’t Have to Use React
• You can use these teaching for:
• vue
• angular
• or just to decouple your backend data from how you present it.
SanDiegoWordCamp2018
Getting Started – Overview
• What’s The Point of Headless?
• Setting Up Dev Environment (WordPress and React)
• WordPress & REST API
• React
• Creating a Component
• Pulling in Data
• Publishing Live
• Netlify (front-end)
• DigitalOcean (WordPress)
SanDiegoWordCamp2018
What’s The Point of Headless?
• Completely Custom Front-end Development
• Free from the constraints and structures of WordPress theming. Eliminates “div-itis”.
• Speeds Up Your Site
• Moves display logic to client side, and streamlines the backend. The Backend can solely
focus on logic, and the front-end can solely focus on displaying that logic.

• Enhanced interactive capabilities on the website.
• The back-end becomes the “state machine”, but back-and-forth interaction happens real-
time in the browser. (e.g. ‘clap’ feature on medium.com)
• You can host your backend and front-end in two different places!
SanDiegoWordCamp2018
Mindset Shift: Separate Yo Concerns!
• Data/Logic != Presentation/View
SanDiegoWordCamp2018
Traditional CMS
Server/Database Wordpress CMS
(PHP Renders HTML)
Website
SanDiegoWordCamp2018
Headless CMS
Server/Database Wordpress CMS
Website
Mobile App
JSON
Data
SanDiegoWordCamp2018
Setting Up WordPress Environment
• Local development environment of your choice.
• (No Cowboy Coding!)
• Local by FlyWheel (easy to spin up)
• Scotchbox (also great)
• Much more control (exactly match your hosting environment)
SanDiegoWordCamp2018
Local by Flywheel
SanDiegoWordCamp2018
Wordpress - Folder Structure
SanDiegoWordCamp2018
Setting Up React Environment
• Create React App
• npx create-react-app my-app
• cd my-app
• npm start
SanDiegoWordCamp2018
Folder Structure
SanDiegoWordCamp2018
Developing Wordpress
• We don’t need to display anything on the actual site.
• Using either the REST API natively or GraphQL through a plugin
• Just make sure custom post types and advanced custom fields show up in
the API (done through a plugin)
SanDiegoWordCamp2018
WordPress REST API
• Let’s you access your WordPress site’s data in JSON
• Endpoints (Posts/ Pages / Site Info)
• Available natively in 4.7+
• REST API Handbook – https://developer.wordpress.org/rest-api/
• ACF integration
• Plugin (ACF to REST API: https://github.com/airesvsg/acf-to-rest-api/)
SanDiegoWordCamp2018
How to Access Endpoints
http: //domain.com/wp-json/wp/v2/
SanDiegoWordCamp2018
How to Access EndPoints
Access Posts
• https: //domain.com/wp-json/wp/v2/posts
Access Pages
• https: //domain.com/wp-json/wp/v2/pages
SanDiegoWordCamp2018
Let’s Check Out Posts on My Site
• http://adam-rasheed-headless.local/wp-json/wp/v2/posts
SanDiegoWordCamp2018
Let’s Check Out Posts on My Site
• http://adam-rasheed-headless.local/wp-json/wp/v2/posts
W
TF?
SanDiegoWordCamp2018
Reading Data for Humans
• Curl
• PostMan —>
SanDiegoWordCamp2018
Reading Data for Humans
SanDiegoWordCamp2018
Wordpress – My Blank Theme
http: //bit.ly/headlesswptheme
ACF to REST API Plugin
SanDiegoWordCamp2018
React/JS v. PHP/WordPress
• PHP is server-side (renders HTML that shows up in the browser through
a server)
• React/JS are client-side (show up in the browser natively)
SanDiegoWordCamp2018
React
• React is a Javascript framework
• Vanilla React vs Frameworks like Gatsby
• GatsbyJs (https://www.gatsbyjs.org/)
• Like Jekyll for React
• Built in Router, Helmet, and GraphQli (IDE for GraphQL)
SanDiegoWordCamp2018
Create React App
• Up and running with react.
• No configuration needed
• Can easily build for production
SanDiegoWordCamp2018
Simple Exercise
• Creating a Component that pulls in a list of our WordPress Posts
SanDiegoWordCamp2018
Folder Structure
<— Only need to look at this
SanDiegoWordCamp2018
App.js
SanDiegoWordCamp2018
posts.js
SanDiegoWordCamp2018
posts.js
SanDiegoWordCamp2018
Singlepost
SanDiegoWordCamp2018
posts.js
SanDiegoWordCamp2018
posts.js – fetchData()
SanDiegoWordCamp2018
consoled data
SanDiegoWordCamp2018
End result
SanDiegoWordCamp2018
Recap: Fetching WordPress Data
• Set endpoint
• Mount Component
• Fetch Data
• Set state
• Render component
SanDiegoWordCamp2018
Hosting
• You can host on two different platforms
• DigitalOcean/SSD-based server for WordPress
• Netlify for front-end.
• Host WordPress first, update your endpoints in your React project, then
host that.
SanDiegoWordCamp2018
If React isn’t your Speed
• Learn Javascript. Seriously.
• You don’t have create a completely Headless CMS.
Nearly Headless
CMS
You can create use the WordPress
API to add a layer of functionality
to your site.
SanDiegoWordCamp2018
Ideas for using the Word API on your current
Site
• “Load More’ button that loads the next few blog posts without refreshing
the page
• “Clap” button like on medium.com
• Twitter like button on posts that updates “like” count on your website
without refreshing the page.
Go out there and Build
something Rad. !
!44

More Related Content

What's hot

WHAT IS HTML5? (at CSS Nite Osaka)
WHAT IS HTML5? (at CSS Nite Osaka)WHAT IS HTML5? (at CSS Nite Osaka)
WHAT IS HTML5? (at CSS Nite Osaka)
Shumpei Shiraishi
 

What's hot (20)

Frontend as a first class citizen
Frontend as a first class citizenFrontend as a first class citizen
Frontend as a first class citizen
 
WHAT IS HTML5? (at CSS Nite Osaka)
WHAT IS HTML5? (at CSS Nite Osaka)WHAT IS HTML5? (at CSS Nite Osaka)
WHAT IS HTML5? (at CSS Nite Osaka)
 
Freelancing and side-projects on Rails
Freelancing and side-projects on RailsFreelancing and side-projects on Rails
Freelancing and side-projects on Rails
 
LESS is More (ChiHTML5 Meetup June 2016)
LESS is More (ChiHTML5 Meetup June 2016)LESS is More (ChiHTML5 Meetup June 2016)
LESS is More (ChiHTML5 Meetup June 2016)
 
How to build a static website in two and a half days with Nuxt and Tailwind CSS
How to build a static website in two and a half days with Nuxt and Tailwind CSSHow to build a static website in two and a half days with Nuxt and Tailwind CSS
How to build a static website in two and a half days with Nuxt and Tailwind CSS
 
NextJS, A JavaScript Framework for building next generation SPA
NextJS, A JavaScript Framework for building next generation SPA  NextJS, A JavaScript Framework for building next generation SPA
NextJS, A JavaScript Framework for building next generation SPA
 
Windycityrails page performance
Windycityrails page performanceWindycityrails page performance
Windycityrails page performance
 
EXPERTALKS: Jul 2012 - Build using Gradle
EXPERTALKS: Jul 2012 - Build using GradleEXPERTALKS: Jul 2012 - Build using Gradle
EXPERTALKS: Jul 2012 - Build using Gradle
 
Client Side rendering Not so Easy
Client Side rendering Not so EasyClient Side rendering Not so Easy
Client Side rendering Not so Easy
 
The WordPress REST API as a Springboard for Website Greatness
The WordPress REST API as a Springboard for Website GreatnessThe WordPress REST API as a Springboard for Website Greatness
The WordPress REST API as a Springboard for Website Greatness
 
Client Side Performance for Back End Developers - Camb Expert Talks, Nov 2016
Client Side Performance for Back End Developers - Camb Expert Talks, Nov 2016Client Side Performance for Back End Developers - Camb Expert Talks, Nov 2016
Client Side Performance for Back End Developers - Camb Expert Talks, Nov 2016
 
Next.js in production by Jasdeep Lalli
Next.js in production by Jasdeep Lalli Next.js in production by Jasdeep Lalli
Next.js in production by Jasdeep Lalli
 
Webpack and Web Performance Optimization
Webpack and Web Performance OptimizationWebpack and Web Performance Optimization
Webpack and Web Performance Optimization
 
Fastest to Mobile with Scalatra + Swagger
Fastest to Mobile with Scalatra + SwaggerFastest to Mobile with Scalatra + Swagger
Fastest to Mobile with Scalatra + Swagger
 
Paperclip
PaperclipPaperclip
Paperclip
 
ClojureScript: I can't believe this is JavaScript
ClojureScript: I can't believe this is JavaScriptClojureScript: I can't believe this is JavaScript
ClojureScript: I can't believe this is JavaScript
 
Sean schofield & Richard Lister, Spree Commerce_ Fearless deployment @ Open C...
Sean schofield & Richard Lister, Spree Commerce_ Fearless deployment @ Open C...Sean schofield & Richard Lister, Spree Commerce_ Fearless deployment @ Open C...
Sean schofield & Richard Lister, Spree Commerce_ Fearless deployment @ Open C...
 
Windy cityrails performance_tuning
Windy cityrails performance_tuningWindy cityrails performance_tuning
Windy cityrails performance_tuning
 
Polymer, HTML includes y core-ajax
Polymer, HTML includes y core-ajaxPolymer, HTML includes y core-ajax
Polymer, HTML includes y core-ajax
 
How we built a job board in one week with JHipster
How we built a job board in one week with JHipsterHow we built a job board in one week with JHipster
How we built a job board in one week with JHipster
 

Similar to Using WordPress as a Backend for Your React Project

Midgard Create and editing content via RDFa
Midgard Create and editing content via RDFaMidgard Create and editing content via RDFa
Midgard Create and editing content via RDFa
Henri Bergius
 

Similar to Using WordPress as a Backend for Your React Project (20)

You Got React.js in My PHP
You Got React.js in My PHPYou Got React.js in My PHP
You Got React.js in My PHP
 
Midgard Create and editing content via RDFa
Midgard Create and editing content via RDFaMidgard Create and editing content via RDFa
Midgard Create and editing content via RDFa
 
Scaling Marketplace to 10,000 Add-Ons - Arun Bhalla
Scaling Marketplace to 10,000 Add-Ons  - Arun BhallaScaling Marketplace to 10,000 Add-Ons  - Arun Bhalla
Scaling Marketplace to 10,000 Add-Ons - Arun Bhalla
 
Building a Headless Shop
Building a Headless ShopBuilding a Headless Shop
Building a Headless Shop
 
Server rendering-talk
Server rendering-talkServer rendering-talk
Server rendering-talk
 
Build Fast WordPress Site With Gatsby
Build Fast WordPress Site With GatsbyBuild Fast WordPress Site With Gatsby
Build Fast WordPress Site With Gatsby
 
A Day Building Fast, Responsive, Extensible Single Page Applications
A Day Building Fast, Responsive, Extensible Single Page ApplicationsA Day Building Fast, Responsive, Extensible Single Page Applications
A Day Building Fast, Responsive, Extensible Single Page Applications
 
MidwestJS 2014 Reconciling ReactJS as a View Layer Replacement
MidwestJS 2014 Reconciling ReactJS as a View Layer ReplacementMidwestJS 2014 Reconciling ReactJS as a View Layer Replacement
MidwestJS 2014 Reconciling ReactJS as a View Layer Replacement
 
Reconciling ReactJS as a View Layer Replacement (MidwestJS 2014)
Reconciling ReactJS as a View Layer Replacement (MidwestJS 2014)Reconciling ReactJS as a View Layer Replacement (MidwestJS 2014)
Reconciling ReactJS as a View Layer Replacement (MidwestJS 2014)
 
Building the next generation of themes with WP Rig 2.0
Building the next generation of themes with WP Rig 2.0Building the next generation of themes with WP Rig 2.0
Building the next generation of themes with WP Rig 2.0
 
VIE - Using RDFa to make content editable
VIE - Using RDFa to make content editableVIE - Using RDFa to make content editable
VIE - Using RDFa to make content editable
 
Page experience road - WordCamp Athens 2022
Page experience road  - WordCamp Athens 2022Page experience road  - WordCamp Athens 2022
Page experience road - WordCamp Athens 2022
 
Developing JavaScript Widgets
Developing JavaScript WidgetsDeveloping JavaScript Widgets
Developing JavaScript Widgets
 
GraphQL Bangkok Meetup 6.0
GraphQL Bangkok Meetup 6.0GraphQL Bangkok Meetup 6.0
GraphQL Bangkok Meetup 6.0
 
JAMstack
JAMstackJAMstack
JAMstack
 
Web summit.pptx
Web summit.pptxWeb summit.pptx
Web summit.pptx
 
Angular SEO
Angular SEO Angular SEO
Angular SEO
 
Pump up the JAM with Gatsby
Pump up the JAM with GatsbyPump up the JAM with Gatsby
Pump up the JAM with Gatsby
 
wt mod3.pdf
wt mod3.pdfwt mod3.pdf
wt mod3.pdf
 
Rapid and Responsive - UX to Prototype with Bootstrap
Rapid and Responsive - UX to Prototype with BootstrapRapid and Responsive - UX to Prototype with Bootstrap
Rapid and Responsive - UX to Prototype with Bootstrap
 

Recently uploaded

+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
WSO2
 

Recently uploaded (20)

presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
A Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source MilvusA Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source Milvus
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
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...
 
Ransomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfRansomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdf
 
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
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
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
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot ModelNavi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 

Using WordPress as a Backend for Your React Project