jsforwp-conf-workshop-2019
jsforwp-conf-workshop-2019 copied to clipboard
JavaScript, React and WordPress Workshop for the 2019 JavaScript for WordPress Conference
JavaScript, React & WordPress Workshop for Beginners
This workshop is from the 2019 JavaScript for WordPress Conference and includes a day long worth of exercises and projects to help you get up and running with JavaScript, React and how to use them in WordPress.
Part 1 - Setup
In this section we will get our development environments up and running with the following:
- Make sure you have Node & NPM installed
- Get a code editor with a command line interface (Try VSCode for Free)
- Download the project files
- Open the project files in your code editor
- Setup a local install of WordPress (Try Desktop Server for Free)
- Add three posts with featured images
- Copy the four themes in the
wordpress
directory into your localwp-content/themes
folder
Part 2 - JavaScript
In this section we will begin learning some JavaScript in order to build a custom lightbox from scratch.
Step 00 - Practice
js/step-00-practice.html
In this exercise we practice writing some basic JavaScript and getting information from the user.
Step 01 - Button Click
js/step-01-button-click.html
In this exercise we learn how to take an action when the user clicks on a button.
Step 02 - Adding Markup with JavaScript
js/step-02-adding-markup.html
Here we create a button on the fly with JavaScript and add it to the page.
Step 03 - Updating the Page with JavaScript
js/step-03-updating-the-page.html
Now we look at how to get user input and use it to modify existing content on the page.
Step 04 - Making a Lightbox Overlay
js/step-04-lightbox-overlay.html
In this step we start from scratch building a lightbox overlay and displaying it when an image is clicked.
Step 05 - Adding an Image to the Lightbox
js/step-05-lightbox-image.html
Here we take the overlay we designed and dynamically add the image that was clicked on to it, along with the alt text.
Step 06 - Closing the Lightbox
js/step-06-lightbox-close.html
In our final steps we learn some tricks for closing a lightbox based on where the user clicks or what keys they press.
Part 3 - React
In this section we take what we learned about JavaScript and HTML and apply it to use React to build the same thing we built using JavaScript with the React library.
Step 00 - Practice
react/step-00-practice
In our first step we learn how to use npm install
and npm start
to get a React app built with Create React App up and running.
Step 01 - Button Click with React
react/step-01-button-click
In this step we learn how to do a simple onClick event in React using a button.
Step 02 - Using State
react/step-02-using-state
Here we introduce the important topic of state in React and how we can use it to keep track of data that changes in our code.
Step 03 - Lightbox Overlay
react/step-03-lightbox-overlay
Now we look at how to use state to create a lightbox overlay that only appears when the user clicks on an image.
Step 04 - Adding the Image to the Lightbox
react/step-04-lightbox-image
In this step we look at how to use state in React along with binding to get the image clicked on and display it in the lightbox.
Step 05 - Closing the Lightbox
react/step-05-lightbox-close
Now we complete our lightbox and the user to close it by clicking outside the image or pressing the Escape key.
Step 06 - Starting with Posts in State
react/step-06-lightbox-posts-in-state
In a bonus step to our lightbox, we look at how we can start with our images in state rather than hardcoded on the page.
Part 4 - WordPress
In the final section of the workshop we will learn how to add our JavaScript and React code into WordPress and convert our React lightbox to display previews of posts.
Step 01 - Loading React
wordpress/twentynineteen-child-step-01-loading-react
- run
npm init
in command line - Add
build
andstart
scripts topackage.json
- Create a
src/index.js
file for our React - Create a
Hello()
function - Call
wp.element.render()
to display<Hello />
in#main .entry-content
- Run
npm start
and seebuild/index.js
created and updated - Enqueue
build/index.js
in thefunctions.php
- Check frontend to see
<Hello />
take over page content - Stop the server in command line with
ctl + c
- Run
npm build
and test everything again on the frontend
Step 02 - Server Side Rendering Content
wordpress/twentynineteen-child-step-02-server-side-render
- Create 3 new posts with feature images
- Create a
react-template.php
file - Add
Template Name: React Template
in a PHP comment at the top - Add a
get_header()
andget_footer()
- Make a WP_Query call for 3 latest posts
- Create markup for each post with the featured image, title and excerpt
- Create a new page and assign it the React Template template
- Comment out the
wp.element.render
in thesrc/index.js
file - Run
npm build
and check the React Template page
Step 03 - React Rendering
wordpress/twentynineteen-child-step-03-react-render
- Get the post containers
- Setup an empty post object
- For each post container, add post details to post object
- Create a
Posts()
function that returns markup for posts - Call
wp.element.render
for<Posts />
to load inside of#react-template .posts
- This should replace the server side rendered posts with a React app with posts
Step 04 - Add Lightbox for Posts
wordpress/twentynineteen-child-step-04-react-lightbox
- Get the post containers
- Setup an empty post object
- For each post container, add post details to post object
- Create a
Posts()
function that returns markup for posts - Call
wp.element.render
for<Posts />
to load inside of#react-template .posts
- This should replace the server side rendered posts with a React app with posts
Further Learning
To learn more about JavaScript, React and how to use it with WordPress, I suggest the following resources: