SlideShare a Scribd company logo
Plastical Sagl Corso S. Gottardo 14, cp 1512
6830 Chiasso 1
Switzerland
tel.: +41 (0) 91 9675780
info@plastical.com
plastical.com
WP 4.7 & React

A perfect marriage?
Ryan Vannin — 12.04.2017
FrontEnders Ticino @ DOS Group
Ryan Vannin
MSc. com., PhD Informatics drop-out, soon-to-become lawyer.
Owner at Plastical, organiser of Hack the City.
Tech and innovation passionate. Below-the-average coder. Runner.
Follow me: @ryanvannin
Agenda
- Ideal vs. real world

- All is not lost! WP 4.7 + React

- Basic setup 

- Data fetching with Redux + Ducks

- Routing (React Router 4.0 alpha) + hacks

- Multi-language with“intl”

- Wrap-up

- Q&A
Ideal vs. real world
In an ideal world
I’m using the latest and fanciest technology available…
… running on great infrastructure or on customised self-
owned “metal”!
In a few words…
… Y’all know what?
I’m a full-stack developer, you a**holes!
In the real world
Crazy requirements

(Does coffee heating?… And don’t forget cats’ gifs!)
Tech constraints

(LAMP stack, wtf!)
Impossible deadlines and limited budget

(Duh, was I supposed to pay you for the website?
Visibility wasn’t enough?)
Let’s be honest here…
… this stuff won’t go away anytime soon!
All is not lost!

Wordpress 4.7 + React
Let’s put them together…
+
Wordpress 4.7
WP Rest API made it to the core in the 4.7 update of WP…
Good news:

no need to install a plug-in…
Bad news:

to fully benefit of its endpoints, you have to build your
own, custom theme!
Endpoints
Some examples:
…/wp-json/wp/v2/posts
…/wp-json/wp/v2/pages
…/wp-json/wp/v2/users
…/wp-json/wp/v2/events
React
… Seriously???
… Ok, not 100% true, but I had to use some cool marketing jargon
“Build a pure, beautiful and feature-rich JS
theme and never look back again to the legacy
PHP spaghetti-code Wordpress throws in”
Example of a complex WP theme
Posts and single pages (usual stuff)
Multi-language (requires a plug-in, e.g. WPML)
Events (+ management) via custom post types
List users and custom author pages with details
Contact form(s) with validation and file upload
Beautiful, responsive and fast theme
Basic setup
Localhost
Usual stuff to run WP locally,

i.e. MAMP (PRO) or via Terminal with LAMP stack


… And, no, Webpack Dev Server won’t do the task!
Folder structure

(usually in /wp-content/themes/your-theme)
…
footer.php
functions.php
header.php
index.php
package.json
webpack.config.js
…
— bin/ (for additional PHP config files)
— less/ (or sass, it’s up to you)
— src/ (our main react folder)

Yep! React stuff mixed up with PHP!!!
However, on deploy these go away
functions.php (1)
Let’s first configure WP to talk to React
https://github.com/plastical/react-theme/blob/master/
functions.php
functions.php (2)
function plastical_scripts() {
$bundleDir = ($isWithinTemplate) ? get_template_directory_uri() : ‘/
assets';
wp_enqueue_style('plastical-style', $bundleDir . ‘/build/bundle.css');
wp_enqueue_script(PLASTICAL_VENDOR, $bundleDir . '/build/
vendor.bundle.js', array('jquery'), PLASTICAL_VERSION, true);
wp_enqueue_script(PLASTICAL_APP, $bundleDir . '/build/bundle.js',
array('jquery'), PLASTICAL_VERSION, true);
…
}
index.php
<?php get_header(); ?>
<div id="root"></div>
<?php get_footer(); ?>
No sidebar.php?

No, unless you want to turn it into a nightmare…
package.json
Usual stuff
{

….

“intl”: “ˆ1.2.5”

…

"react-intl": "^2.1.5",
"react-intl-redux": "^0.2.0",

…

“react-router”: “4.0.0-alpha.6”

…



"wordpress-query-comments": "^1.1.0",
"wordpress-query-custom-posts-events": "^0.2.0",
"wordpress-query-menu": "^1.1.0",
"wordpress-query-page": "^1.1.0",
"wordpress-query-page-children": "^0.1.4",
"wordpress-query-posts": "^1.1.1",
"wordpress-query-term": "^1.1.0",
"wordpress-query-users": "^0.2.0"

}
+
Ducks!!!

Back to this stuff later
Our app structure

(/src)
— components/
— data/
— i18n/
index.js (app entry point)
reducer.js
— routing/
structure.js
— utils/
More about this folder later…
webpack.config.js (v 2.0)
Again, usual stuff…
module.exports = {
entry: {
assets: './src/index.js',
vendor: ['react','react-dom','react-router','redux']
},
output: {
path: path.join(__dirname, '../../../assets/build'),
filename: 'bundle.js'
},
resolve: {
extensions: ['.js', '.jsx'],
alias: {
data: path.join(__dirname, 'src/data'),
components: path.join(__dirname, 'src/components'),
utils: path.resolve(__dirname, 'src/utils'),
},
modules: ['node_modules', ‘src']
},
….
Remember? all the stuff goes under /assets/…
+
Data fetching with Redux + Ducks
Redux
Redux will hold the application state in its store,
including data fetched from the WP Rest API endpoints



https://github.com/plastical/react-theme/blob/master/
src/store.js
PS.: nothing fancy here…
Ducks???
“Ducks” are redux modules containing single-purpose
constants, actions and reducers.



https://github.com/plastical/react-theme/blob/master/
src/reducer.js
Ex.: https://github.com/plastical/wordpress-query-
users
Fetching and data persistence
Routing (React Router 4.0 alpha) + hacks
React Router 4.0 alpha
Component based, but unstable (e.g., Match vs. Route)
Not working with “react-router-redux”, had to build my
own sync…
Huge mess with “intl", “react-intl” and “react-redux-intl”…



https://github.com/plastical/react-theme/blob/master/
src/routing/index.js
PS.: version > 4.0 beta? Need contributions!
Routing? Welcome some ugly hacks…
index.js
https://github.com/plastical/react-theme/blob/master/
src/index.js
structure.js
https://github.com/plastical/react-theme/blob/master/
src/structure.js
External URLs + click capture… jQuery!!!
Multi-language with“intl”
Intl
Needs i18n
https://github.com/plastical/react-theme/tree/master/
src/i18n
Import in component as decorator:
import { injectIntl } from 'react-intl';
…
{
…
<h1>
{props.intl.formatMessage({ id: 'home.latest_news' })}
</h1>
…
}
export default injectIntl(
connect((state, ownProps) => { … })(Home)
);
Translations as messages:
…

{

…

‘home.latest_news’: ‘Latest news’,

…

}
WP multi language with React?
Needs WPML (or a similar plug-in)
To fully recognise new locale a browser reload is needed
(onClick)
Translation strings are hardcoded, thus wasn’t easier an
other solution (e.g. inject directly from PHP)?
Wrap-up
Some cool stuff…
WP 4.7 + React: excellent combination to build an almost
pure JS theme
Blazing fast (once your bundles are in cache)
Using endpoints opens up endless possibilities with a few
specialised components, e.g. search and contact
Some less cool stuff… (1)
You HAVE to know both JS and PHP in order to benefit of
both words
You have to configure WP to use the WP Rest APIs,
otherwise it doesn’t work as expected (e.g. menus!!!)
Overcomplicated, over bloated
Simple out-of-the-box features do not work without
tweaks (ex. active state on navigation)
Some less cool stuff… (2)
Multi language is just a nightmare, as well as routing
(you need to manipulate the DOM — not the virtual, but
the real — to detect clicks or onEnters…)
You are stuck with a LAMP stack, unless you fetch data
from outside…
In production, simplest changes (e.g., string translations
or add/modify a className) require “npm run build”,
while in PHP just edit the .php file
Some less cool stuff… (3)
Bundle size ~1MB!!!
(ok, can cache it and gzip it, but what’s the point?)
The result
Site in production:

http://dev.agire.ch
Source code:

https://github.com/plastical/react-theme
Q&As
Plastical Sagl Corso S. Gottardo 14, cp 1512
6830 Chiasso 1
Switzerland
tel.: +41 (0) 91 9675780
info@plastical.com
plastical.com
Thanks

@ryanvannin / ryan@plastical.com

More Related Content

What's hot

Phing - A PHP Build Tool (An Introduction)
Phing - A PHP Build Tool (An Introduction)Phing - A PHP Build Tool (An Introduction)
Phing - A PHP Build Tool (An Introduction)
Michiel Rook
 
Automated Deployment With Phing
Automated Deployment With PhingAutomated Deployment With Phing
Automated Deployment With Phing
Daniel Cousineau
 
Makefile for python projects
Makefile for python projectsMakefile for python projects
Makefile for python projects
Mpho Mphego
 
Presentation php
Presentation phpPresentation php
Presentation php
Muhammad Saqib Malik
 
Build Automation of PHP Applications
Build Automation of PHP ApplicationsBuild Automation of PHP Applications
Build Automation of PHP Applications
Pavan Kumar N
 
Deploying PHP applications with Phing
Deploying PHP applications with PhingDeploying PHP applications with Phing
Deploying PHP applications with Phing
Michiel Rook
 
Web Development in Perl
Web Development in PerlWeb Development in Perl
Web Development in Perl
Naveen Gupta
 
Os Treat
Os TreatOs Treat
Os Treat
oscon2007
 
Php Ppt
Php PptPhp Ppt
Php Ppt
vsnmurthy
 
Php Power Tools
Php Power ToolsPhp Power Tools
Php Power Tools
Michelangelo van Dam
 
Ant vs Phing
Ant vs PhingAnt vs Phing
Ant vs Phing
Manuel Baldassarri
 
Introduction to Web Programming with Perl
Introduction to Web Programming with PerlIntroduction to Web Programming with Perl
Introduction to Web Programming with Perl
Dave Cross
 
Building and Deploying PHP Apps Using phing
Building and Deploying PHP Apps Using phingBuilding and Deploying PHP Apps Using phing
Building and Deploying PHP Apps Using phing
Mihail Irintchev
 
Php Dependency Management with Composer ZendCon 2016
Php Dependency Management with Composer ZendCon 2016Php Dependency Management with Composer ZendCon 2016
Php Dependency Management with Composer ZendCon 2016
Clark Everetts
 
Phpbasics
PhpbasicsPhpbasics
Phpbasics
PrinceGuru MS
 
Vagrant move over, here is Docker
Vagrant move over, here is DockerVagrant move over, here is Docker
Vagrant move over, here is Docker
Nick Belhomme
 
Php presentation
Php presentationPhp presentation
Php presentation
Helen Pitlick
 
Php
PhpPhp
Phing
PhingPhing
Phing
mdekrijger
 
New EEA Plone Add-ons
New EEA Plone Add-onsNew EEA Plone Add-ons
New EEA Plone Add-ons
Alin Voinea
 

What's hot (20)

Phing - A PHP Build Tool (An Introduction)
Phing - A PHP Build Tool (An Introduction)Phing - A PHP Build Tool (An Introduction)
Phing - A PHP Build Tool (An Introduction)
 
Automated Deployment With Phing
Automated Deployment With PhingAutomated Deployment With Phing
Automated Deployment With Phing
 
Makefile for python projects
Makefile for python projectsMakefile for python projects
Makefile for python projects
 
Presentation php
Presentation phpPresentation php
Presentation php
 
Build Automation of PHP Applications
Build Automation of PHP ApplicationsBuild Automation of PHP Applications
Build Automation of PHP Applications
 
Deploying PHP applications with Phing
Deploying PHP applications with PhingDeploying PHP applications with Phing
Deploying PHP applications with Phing
 
Web Development in Perl
Web Development in PerlWeb Development in Perl
Web Development in Perl
 
Os Treat
Os TreatOs Treat
Os Treat
 
Php Ppt
Php PptPhp Ppt
Php Ppt
 
Php Power Tools
Php Power ToolsPhp Power Tools
Php Power Tools
 
Ant vs Phing
Ant vs PhingAnt vs Phing
Ant vs Phing
 
Introduction to Web Programming with Perl
Introduction to Web Programming with PerlIntroduction to Web Programming with Perl
Introduction to Web Programming with Perl
 
Building and Deploying PHP Apps Using phing
Building and Deploying PHP Apps Using phingBuilding and Deploying PHP Apps Using phing
Building and Deploying PHP Apps Using phing
 
Php Dependency Management with Composer ZendCon 2016
Php Dependency Management with Composer ZendCon 2016Php Dependency Management with Composer ZendCon 2016
Php Dependency Management with Composer ZendCon 2016
 
Phpbasics
PhpbasicsPhpbasics
Phpbasics
 
Vagrant move over, here is Docker
Vagrant move over, here is DockerVagrant move over, here is Docker
Vagrant move over, here is Docker
 
Php presentation
Php presentationPhp presentation
Php presentation
 
Php
PhpPhp
Php
 
Phing
PhingPhing
Phing
 
New EEA Plone Add-ons
New EEA Plone Add-onsNew EEA Plone Add-ons
New EEA Plone Add-ons
 

Similar to WP 4.7 & React — A perfect marriage?

Current state-of-php
Current state-of-phpCurrent state-of-php
Current state-of-php
Richard McIntyre
 
TTW FTW: Plone as the new wordpress
TTW FTW: Plone as the new wordpressTTW FTW: Plone as the new wordpress
TTW FTW: Plone as the new wordpress
Dylan Jay
 
Everything is Awesome - Cutting the Corners off the Web
Everything is Awesome - Cutting the Corners off the WebEverything is Awesome - Cutting the Corners off the Web
Everything is Awesome - Cutting the Corners off the Web
James Rakich
 
Enjoying the full stack - Frontend 2010
Enjoying the full stack - Frontend 2010Enjoying the full stack - Frontend 2010
Enjoying the full stack - Frontend 2010
Christian Heilmann
 
A intro to (hosted) Shiny Apps
A intro to (hosted) Shiny AppsA intro to (hosted) Shiny Apps
A intro to (hosted) Shiny Apps
Daniel Koller
 
Fundamentals of web_design_v2
Fundamentals of web_design_v2Fundamentals of web_design_v2
Fundamentals of web_design_v2
hussain534
 
Bring drupal 8 to all in their native languages
Bring drupal 8 to all in their native languagesBring drupal 8 to all in their native languages
Bring drupal 8 to all in their native languages
Sébastien Corbin
 
All a flutter about Flutter.io
All a flutter about Flutter.ioAll a flutter about Flutter.io
All a flutter about Flutter.io
Steven Cooper
 
HAXTheWeb @ Apereo 19
HAXTheWeb @ Apereo 19HAXTheWeb @ Apereo 19
HAXTheWeb @ Apereo 19
btopro
 
Introduction to Flutter - truly crossplatform, amazingly fast
Introduction to Flutter - truly crossplatform, amazingly fastIntroduction to Flutter - truly crossplatform, amazingly fast
Introduction to Flutter - truly crossplatform, amazingly fast
Bartosz Kosarzycki
 
Drupal Theme Development - DrupalCon Chicago 2011
Drupal Theme Development - DrupalCon Chicago 2011Drupal Theme Development - DrupalCon Chicago 2011
Drupal Theme Development - DrupalCon Chicago 2011
Ryan Price
 
Demystifying Maven
Demystifying MavenDemystifying Maven
Demystifying Maven
Mike Desjardins
 
Stencil the time for vanilla web components has arrived
Stencil the time for vanilla web components has arrivedStencil the time for vanilla web components has arrived
Stencil the time for vanilla web components has arrived
Gil Fink
 
Devtools cheatsheet
Devtools cheatsheetDevtools cheatsheet
Devtools cheatsheet
Dr. Volkan OBAN
 
Devtools cheatsheet
Devtools cheatsheetDevtools cheatsheet
Devtools cheatsheet
Dieudonne Nahigombeye
 
DSLs in JavaScript
DSLs in JavaScriptDSLs in JavaScript
DSLs in JavaScript
elliando dias
 
Apache Bigtop and ARM64 / AArch64 - Empowering Big Data Everywhere
Apache Bigtop and ARM64 / AArch64 - Empowering Big Data EverywhereApache Bigtop and ARM64 / AArch64 - Empowering Big Data Everywhere
Apache Bigtop and ARM64 / AArch64 - Empowering Big Data Everywhere
Ganesh Raju
 
Moving from Web 1.0 to Web 2.0
Moving from Web 1.0 to Web 2.0Moving from Web 1.0 to Web 2.0
Moving from Web 1.0 to Web 2.0
Estelle Weyl
 
Pinax Long Tutorial Slides
Pinax Long Tutorial SlidesPinax Long Tutorial Slides
Pinax Long Tutorial Slides
Daniel Greenfeld
 
Drupal in 30 Minutes
Drupal in 30 MinutesDrupal in 30 Minutes
Drupal in 30 Minutes
Robert Carr
 

Similar to WP 4.7 & React — A perfect marriage? (20)

Current state-of-php
Current state-of-phpCurrent state-of-php
Current state-of-php
 
TTW FTW: Plone as the new wordpress
TTW FTW: Plone as the new wordpressTTW FTW: Plone as the new wordpress
TTW FTW: Plone as the new wordpress
 
Everything is Awesome - Cutting the Corners off the Web
Everything is Awesome - Cutting the Corners off the WebEverything is Awesome - Cutting the Corners off the Web
Everything is Awesome - Cutting the Corners off the Web
 
Enjoying the full stack - Frontend 2010
Enjoying the full stack - Frontend 2010Enjoying the full stack - Frontend 2010
Enjoying the full stack - Frontend 2010
 
A intro to (hosted) Shiny Apps
A intro to (hosted) Shiny AppsA intro to (hosted) Shiny Apps
A intro to (hosted) Shiny Apps
 
Fundamentals of web_design_v2
Fundamentals of web_design_v2Fundamentals of web_design_v2
Fundamentals of web_design_v2
 
Bring drupal 8 to all in their native languages
Bring drupal 8 to all in their native languagesBring drupal 8 to all in their native languages
Bring drupal 8 to all in their native languages
 
All a flutter about Flutter.io
All a flutter about Flutter.ioAll a flutter about Flutter.io
All a flutter about Flutter.io
 
HAXTheWeb @ Apereo 19
HAXTheWeb @ Apereo 19HAXTheWeb @ Apereo 19
HAXTheWeb @ Apereo 19
 
Introduction to Flutter - truly crossplatform, amazingly fast
Introduction to Flutter - truly crossplatform, amazingly fastIntroduction to Flutter - truly crossplatform, amazingly fast
Introduction to Flutter - truly crossplatform, amazingly fast
 
Drupal Theme Development - DrupalCon Chicago 2011
Drupal Theme Development - DrupalCon Chicago 2011Drupal Theme Development - DrupalCon Chicago 2011
Drupal Theme Development - DrupalCon Chicago 2011
 
Demystifying Maven
Demystifying MavenDemystifying Maven
Demystifying Maven
 
Stencil the time for vanilla web components has arrived
Stencil the time for vanilla web components has arrivedStencil the time for vanilla web components has arrived
Stencil the time for vanilla web components has arrived
 
Devtools cheatsheet
Devtools cheatsheetDevtools cheatsheet
Devtools cheatsheet
 
Devtools cheatsheet
Devtools cheatsheetDevtools cheatsheet
Devtools cheatsheet
 
DSLs in JavaScript
DSLs in JavaScriptDSLs in JavaScript
DSLs in JavaScript
 
Apache Bigtop and ARM64 / AArch64 - Empowering Big Data Everywhere
Apache Bigtop and ARM64 / AArch64 - Empowering Big Data EverywhereApache Bigtop and ARM64 / AArch64 - Empowering Big Data Everywhere
Apache Bigtop and ARM64 / AArch64 - Empowering Big Data Everywhere
 
Moving from Web 1.0 to Web 2.0
Moving from Web 1.0 to Web 2.0Moving from Web 1.0 to Web 2.0
Moving from Web 1.0 to Web 2.0
 
Pinax Long Tutorial Slides
Pinax Long Tutorial SlidesPinax Long Tutorial Slides
Pinax Long Tutorial Slides
 
Drupal in 30 Minutes
Drupal in 30 MinutesDrupal in 30 Minutes
Drupal in 30 Minutes
 

Recently uploaded

Azure API Management to expose backend services securely
Azure API Management to expose backend services securelyAzure API Management to expose backend services securely
Azure API Management to expose backend services securely
Dinusha Kumarasiri
 
Dandelion Hashtable: beyond billion requests per second on a commodity server
Dandelion Hashtable: beyond billion requests per second on a commodity serverDandelion Hashtable: beyond billion requests per second on a commodity server
Dandelion Hashtable: beyond billion requests per second on a commodity server
Antonios Katsarakis
 
System Design Case Study: Building a Scalable E-Commerce Platform - Hiike
System Design Case Study: Building a Scalable E-Commerce Platform - HiikeSystem Design Case Study: Building a Scalable E-Commerce Platform - Hiike
System Design Case Study: Building a Scalable E-Commerce Platform - Hiike
Hiike
 
Generating privacy-protected synthetic data using Secludy and Milvus
Generating privacy-protected synthetic data using Secludy and MilvusGenerating privacy-protected synthetic data using Secludy and Milvus
Generating privacy-protected synthetic data using Secludy and Milvus
Zilliz
 
JavaLand 2024: Application Development Green Masterplan
JavaLand 2024: Application Development Green MasterplanJavaLand 2024: Application Development Green Masterplan
JavaLand 2024: Application Development Green Masterplan
Miro Wengner
 
Introduction of Cybersecurity with OSS at Code Europe 2024
Introduction of Cybersecurity with OSS  at Code Europe 2024Introduction of Cybersecurity with OSS  at Code Europe 2024
Introduction of Cybersecurity with OSS at Code Europe 2024
Hiroshi SHIBATA
 
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAUHCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
panagenda
 
Digital Marketing Trends in 2024 | Guide for Staying Ahead
Digital Marketing Trends in 2024 | Guide for Staying AheadDigital Marketing Trends in 2024 | Guide for Staying Ahead
Digital Marketing Trends in 2024 | Guide for Staying Ahead
Wask
 
Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...
Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...
Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...
saastr
 
Digital Banking in the Cloud: How Citizens Bank Unlocked Their Mainframe
Digital Banking in the Cloud: How Citizens Bank Unlocked Their MainframeDigital Banking in the Cloud: How Citizens Bank Unlocked Their Mainframe
Digital Banking in the Cloud: How Citizens Bank Unlocked Their Mainframe
Precisely
 
Salesforce Integration for Bonterra Impact Management (fka Social Solutions A...
Salesforce Integration for Bonterra Impact Management (fka Social Solutions A...Salesforce Integration for Bonterra Impact Management (fka Social Solutions A...
Salesforce Integration for Bonterra Impact Management (fka Social Solutions A...
Jeffrey Haguewood
 
June Patch Tuesday
June Patch TuesdayJune Patch Tuesday
June Patch Tuesday
Ivanti
 
Choosing The Best AWS Service For Your Website + API.pptx
Choosing The Best AWS Service For Your Website + API.pptxChoosing The Best AWS Service For Your Website + API.pptx
Choosing The Best AWS Service For Your Website + API.pptx
Brandon Minnick, MBA
 
SAP S/4 HANA sourcing and procurement to Public cloud
SAP S/4 HANA sourcing and procurement to Public cloudSAP S/4 HANA sourcing and procurement to Public cloud
SAP S/4 HANA sourcing and procurement to Public cloud
maazsz111
 
WeTestAthens: Postman's AI & Automation Techniques
WeTestAthens: Postman's AI & Automation TechniquesWeTestAthens: Postman's AI & Automation Techniques
WeTestAthens: Postman's AI & Automation Techniques
Postman
 
Skybuffer SAM4U tool for SAP license adoption
Skybuffer SAM4U tool for SAP license adoptionSkybuffer SAM4U tool for SAP license adoption
Skybuffer SAM4U tool for SAP license adoption
Tatiana Kojar
 
“Temporal Event Neural Networks: A More Efficient Alternative to the Transfor...
“Temporal Event Neural Networks: A More Efficient Alternative to the Transfor...“Temporal Event Neural Networks: A More Efficient Alternative to the Transfor...
“Temporal Event Neural Networks: A More Efficient Alternative to the Transfor...
Edge AI and Vision Alliance
 
dbms calicut university B. sc Cs 4th sem.pdf
dbms  calicut university B. sc Cs 4th sem.pdfdbms  calicut university B. sc Cs 4th sem.pdf
dbms calicut university B. sc Cs 4th sem.pdf
Shinana2
 
Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...
Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...
Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...
saastr
 
leewayhertz.com-AI in predictive maintenance Use cases technologies benefits ...
leewayhertz.com-AI in predictive maintenance Use cases technologies benefits ...leewayhertz.com-AI in predictive maintenance Use cases technologies benefits ...
leewayhertz.com-AI in predictive maintenance Use cases technologies benefits ...
alexjohnson7307
 

Recently uploaded (20)

Azure API Management to expose backend services securely
Azure API Management to expose backend services securelyAzure API Management to expose backend services securely
Azure API Management to expose backend services securely
 
Dandelion Hashtable: beyond billion requests per second on a commodity server
Dandelion Hashtable: beyond billion requests per second on a commodity serverDandelion Hashtable: beyond billion requests per second on a commodity server
Dandelion Hashtable: beyond billion requests per second on a commodity server
 
System Design Case Study: Building a Scalable E-Commerce Platform - Hiike
System Design Case Study: Building a Scalable E-Commerce Platform - HiikeSystem Design Case Study: Building a Scalable E-Commerce Platform - Hiike
System Design Case Study: Building a Scalable E-Commerce Platform - Hiike
 
Generating privacy-protected synthetic data using Secludy and Milvus
Generating privacy-protected synthetic data using Secludy and MilvusGenerating privacy-protected synthetic data using Secludy and Milvus
Generating privacy-protected synthetic data using Secludy and Milvus
 
JavaLand 2024: Application Development Green Masterplan
JavaLand 2024: Application Development Green MasterplanJavaLand 2024: Application Development Green Masterplan
JavaLand 2024: Application Development Green Masterplan
 
Introduction of Cybersecurity with OSS at Code Europe 2024
Introduction of Cybersecurity with OSS  at Code Europe 2024Introduction of Cybersecurity with OSS  at Code Europe 2024
Introduction of Cybersecurity with OSS at Code Europe 2024
 
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAUHCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
 
Digital Marketing Trends in 2024 | Guide for Staying Ahead
Digital Marketing Trends in 2024 | Guide for Staying AheadDigital Marketing Trends in 2024 | Guide for Staying Ahead
Digital Marketing Trends in 2024 | Guide for Staying Ahead
 
Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...
Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...
Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...
 
Digital Banking in the Cloud: How Citizens Bank Unlocked Their Mainframe
Digital Banking in the Cloud: How Citizens Bank Unlocked Their MainframeDigital Banking in the Cloud: How Citizens Bank Unlocked Their Mainframe
Digital Banking in the Cloud: How Citizens Bank Unlocked Their Mainframe
 
Salesforce Integration for Bonterra Impact Management (fka Social Solutions A...
Salesforce Integration for Bonterra Impact Management (fka Social Solutions A...Salesforce Integration for Bonterra Impact Management (fka Social Solutions A...
Salesforce Integration for Bonterra Impact Management (fka Social Solutions A...
 
June Patch Tuesday
June Patch TuesdayJune Patch Tuesday
June Patch Tuesday
 
Choosing The Best AWS Service For Your Website + API.pptx
Choosing The Best AWS Service For Your Website + API.pptxChoosing The Best AWS Service For Your Website + API.pptx
Choosing The Best AWS Service For Your Website + API.pptx
 
SAP S/4 HANA sourcing and procurement to Public cloud
SAP S/4 HANA sourcing and procurement to Public cloudSAP S/4 HANA sourcing and procurement to Public cloud
SAP S/4 HANA sourcing and procurement to Public cloud
 
WeTestAthens: Postman's AI & Automation Techniques
WeTestAthens: Postman's AI & Automation TechniquesWeTestAthens: Postman's AI & Automation Techniques
WeTestAthens: Postman's AI & Automation Techniques
 
Skybuffer SAM4U tool for SAP license adoption
Skybuffer SAM4U tool for SAP license adoptionSkybuffer SAM4U tool for SAP license adoption
Skybuffer SAM4U tool for SAP license adoption
 
“Temporal Event Neural Networks: A More Efficient Alternative to the Transfor...
“Temporal Event Neural Networks: A More Efficient Alternative to the Transfor...“Temporal Event Neural Networks: A More Efficient Alternative to the Transfor...
“Temporal Event Neural Networks: A More Efficient Alternative to the Transfor...
 
dbms calicut university B. sc Cs 4th sem.pdf
dbms  calicut university B. sc Cs 4th sem.pdfdbms  calicut university B. sc Cs 4th sem.pdf
dbms calicut university B. sc Cs 4th sem.pdf
 
Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...
Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...
Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...
 
leewayhertz.com-AI in predictive maintenance Use cases technologies benefits ...
leewayhertz.com-AI in predictive maintenance Use cases technologies benefits ...leewayhertz.com-AI in predictive maintenance Use cases technologies benefits ...
leewayhertz.com-AI in predictive maintenance Use cases technologies benefits ...
 

WP 4.7 & React — A perfect marriage?

  • 1. Plastical Sagl Corso S. Gottardo 14, cp 1512 6830 Chiasso 1 Switzerland tel.: +41 (0) 91 9675780 info@plastical.com plastical.com WP 4.7 & React
 A perfect marriage? Ryan Vannin — 12.04.2017 FrontEnders Ticino @ DOS Group
  • 2. Ryan Vannin MSc. com., PhD Informatics drop-out, soon-to-become lawyer. Owner at Plastical, organiser of Hack the City. Tech and innovation passionate. Below-the-average coder. Runner. Follow me: @ryanvannin
  • 3. Agenda - Ideal vs. real world
 - All is not lost! WP 4.7 + React
 - Basic setup 
 - Data fetching with Redux + Ducks
 - Routing (React Router 4.0 alpha) + hacks
 - Multi-language with“intl”
 - Wrap-up
 - Q&A
  • 5. In an ideal world I’m using the latest and fanciest technology available…
  • 6. … running on great infrastructure or on customised self- owned “metal”!
  • 7. In a few words… … Y’all know what? I’m a full-stack developer, you a**holes!
  • 8. In the real world Crazy requirements
 (Does coffee heating?… And don’t forget cats’ gifs!) Tech constraints
 (LAMP stack, wtf!) Impossible deadlines and limited budget
 (Duh, was I supposed to pay you for the website? Visibility wasn’t enough?)
  • 9. Let’s be honest here… … this stuff won’t go away anytime soon!
  • 10. All is not lost!
 Wordpress 4.7 + React
  • 11. Let’s put them together… +
  • 12. Wordpress 4.7 WP Rest API made it to the core in the 4.7 update of WP… Good news:
 no need to install a plug-in… Bad news:
 to fully benefit of its endpoints, you have to build your own, custom theme!
  • 15. … Ok, not 100% true, but I had to use some cool marketing jargon “Build a pure, beautiful and feature-rich JS theme and never look back again to the legacy PHP spaghetti-code Wordpress throws in”
  • 16. Example of a complex WP theme Posts and single pages (usual stuff) Multi-language (requires a plug-in, e.g. WPML) Events (+ management) via custom post types List users and custom author pages with details Contact form(s) with validation and file upload Beautiful, responsive and fast theme
  • 18. Localhost Usual stuff to run WP locally,
 i.e. MAMP (PRO) or via Terminal with LAMP stack 
 … And, no, Webpack Dev Server won’t do the task!
  • 19. Folder structure
 (usually in /wp-content/themes/your-theme) … footer.php functions.php header.php index.php package.json webpack.config.js … — bin/ (for additional PHP config files) — less/ (or sass, it’s up to you) — src/ (our main react folder)
 Yep! React stuff mixed up with PHP!!! However, on deploy these go away
  • 20. functions.php (1) Let’s first configure WP to talk to React https://github.com/plastical/react-theme/blob/master/ functions.php
  • 21. functions.php (2) function plastical_scripts() { $bundleDir = ($isWithinTemplate) ? get_template_directory_uri() : ‘/ assets'; wp_enqueue_style('plastical-style', $bundleDir . ‘/build/bundle.css'); wp_enqueue_script(PLASTICAL_VENDOR, $bundleDir . '/build/ vendor.bundle.js', array('jquery'), PLASTICAL_VERSION, true); wp_enqueue_script(PLASTICAL_APP, $bundleDir . '/build/bundle.js', array('jquery'), PLASTICAL_VERSION, true); … }
  • 22. index.php <?php get_header(); ?> <div id="root"></div> <?php get_footer(); ?> No sidebar.php?
 No, unless you want to turn it into a nightmare…
  • 23. package.json Usual stuff {
 ….
 “intl”: “ˆ1.2.5”
 …
 "react-intl": "^2.1.5", "react-intl-redux": "^0.2.0",
 …
 “react-router”: “4.0.0-alpha.6”
 …
 
 "wordpress-query-comments": "^1.1.0", "wordpress-query-custom-posts-events": "^0.2.0", "wordpress-query-menu": "^1.1.0", "wordpress-query-page": "^1.1.0", "wordpress-query-page-children": "^0.1.4", "wordpress-query-posts": "^1.1.1", "wordpress-query-term": "^1.1.0", "wordpress-query-users": "^0.2.0"
 } + Ducks!!!
 Back to this stuff later
  • 24. Our app structure
 (/src) — components/ — data/ — i18n/ index.js (app entry point) reducer.js — routing/ structure.js — utils/ More about this folder later…
  • 25. webpack.config.js (v 2.0) Again, usual stuff… module.exports = { entry: { assets: './src/index.js', vendor: ['react','react-dom','react-router','redux'] }, output: { path: path.join(__dirname, '../../../assets/build'), filename: 'bundle.js' }, resolve: { extensions: ['.js', '.jsx'], alias: { data: path.join(__dirname, 'src/data'), components: path.join(__dirname, 'src/components'), utils: path.resolve(__dirname, 'src/utils'), }, modules: ['node_modules', ‘src'] }, …. Remember? all the stuff goes under /assets/… +
  • 26. Data fetching with Redux + Ducks
  • 27. Redux Redux will hold the application state in its store, including data fetched from the WP Rest API endpoints
 
 https://github.com/plastical/react-theme/blob/master/ src/store.js PS.: nothing fancy here…
  • 28. Ducks??? “Ducks” are redux modules containing single-purpose constants, actions and reducers.
 
 https://github.com/plastical/react-theme/blob/master/ src/reducer.js Ex.: https://github.com/plastical/wordpress-query- users
  • 29. Fetching and data persistence
  • 30. Routing (React Router 4.0 alpha) + hacks
  • 31. React Router 4.0 alpha Component based, but unstable (e.g., Match vs. Route) Not working with “react-router-redux”, had to build my own sync… Huge mess with “intl", “react-intl” and “react-redux-intl”…
 
 https://github.com/plastical/react-theme/blob/master/ src/routing/index.js PS.: version > 4.0 beta? Need contributions!
  • 32. Routing? Welcome some ugly hacks… index.js https://github.com/plastical/react-theme/blob/master/ src/index.js structure.js https://github.com/plastical/react-theme/blob/master/ src/structure.js External URLs + click capture… jQuery!!!
  • 34. Intl Needs i18n https://github.com/plastical/react-theme/tree/master/ src/i18n Import in component as decorator: import { injectIntl } from 'react-intl'; … { … <h1> {props.intl.formatMessage({ id: 'home.latest_news' })} </h1> … } export default injectIntl( connect((state, ownProps) => { … })(Home) ); Translations as messages: …
 {
 …
 ‘home.latest_news’: ‘Latest news’,
 …
 }
  • 35. WP multi language with React? Needs WPML (or a similar plug-in) To fully recognise new locale a browser reload is needed (onClick) Translation strings are hardcoded, thus wasn’t easier an other solution (e.g. inject directly from PHP)?
  • 37. Some cool stuff… WP 4.7 + React: excellent combination to build an almost pure JS theme Blazing fast (once your bundles are in cache) Using endpoints opens up endless possibilities with a few specialised components, e.g. search and contact
  • 38. Some less cool stuff… (1) You HAVE to know both JS and PHP in order to benefit of both words You have to configure WP to use the WP Rest APIs, otherwise it doesn’t work as expected (e.g. menus!!!) Overcomplicated, over bloated Simple out-of-the-box features do not work without tweaks (ex. active state on navigation)
  • 39. Some less cool stuff… (2) Multi language is just a nightmare, as well as routing (you need to manipulate the DOM — not the virtual, but the real — to detect clicks or onEnters…) You are stuck with a LAMP stack, unless you fetch data from outside… In production, simplest changes (e.g., string translations or add/modify a className) require “npm run build”, while in PHP just edit the .php file
  • 40. Some less cool stuff… (3) Bundle size ~1MB!!! (ok, can cache it and gzip it, but what’s the point?)
  • 41. The result Site in production:
 http://dev.agire.ch Source code:
 https://github.com/plastical/react-theme
  • 42. Q&As
  • 43. Plastical Sagl Corso S. Gottardo 14, cp 1512 6830 Chiasso 1 Switzerland tel.: +41 (0) 91 9675780 info@plastical.com plastical.com Thanks
 @ryanvannin / ryan@plastical.com