May 6, 2020
Just wanted to note that version v3.4.1 of Create-React-App is still the latest.
If you have a theme made with an earlier version of create-react-wptheme
and want to update it to the latest code, just follow these instructions.
Michael Soriano is writing a tutorial for creating a theme using React. He uses create-react-wptheme
for the initial setup. He provides a lot more detail than this readme
and the screen shots are really helpful.
Check it out:
Let's build a WordPress theme with React: Part 1 (Setup)
For more details check out the rest of this document.
To create a WordPress theme using create-react-wptheme
, follow these steps.
cd C:\xampp\htdocs\wordpress\wp-content\themes
cd /xampp/htdocs/wordpress/wp-content/themes
npx create-react-wptheme
to make a new theme
npx create-react-wptheme my_react_theme
npx create-react-wptheme my_react_theme --typescript
cd my_react_theme/react-src
npm run start
<Your Theme's Root Folder>/index.php
Now, back in your command prompt, rerun the "start" script again...
npm run start
and rerun that same command again.When new versions of Create-React-WPTheme
are released, you can easily upgrade an existing theme using a single command.
react-src
folder.
<Your Theme's Root Folder>/react-src
npm
then run this command:
npm install @devloco/react-scripts-wptheme@latest
yarn
then run this command:
yarn add @devloco/react-scripts-wptheme@latest
If your theme uses TypeScript, you'll need to modify the theme's react-app-env.d.ts
file:
<Your Theme's Root Folder>/react-src/src
folder.react-app-env.d.ts
file in your editor of choice./// <reference types="@devloco/react-scripts-wptheme" />
If you're looking at a React tutorial on the web, you can use create-react-wptheme
in place of create-react-app
.
But you do have to remember that the React app code is one extra folder down inside your theme folder.
Notice that the final folder in this path is react-src
:
/xampp/htdocs/wordpress/wp-content/themes/<Your Theme's Root Folder>/react-src
So for example, if the tutorial instructs you to edit the src/App.js
file, then for you, that file would actually be located at:
<Your Theme's Root Folder>/react-src/src/App.js
The authors of the original create-react-app
say that using the "Public" folder (found at react-src/public
in your new theme's folder) is a last ditch "escape hatch" for adding otherwise-hard-to-deal-with files.
But for this project, I've decided to use it for all things that you'd put into a normal WordPress theme (e.g. functions.php, etc.). So any PHP, hard-coded CSS, and/or hard-coded JS (like other JS libraries that you'd like to reference globally (I don't judge)), can go into the public folder.
In addition, any changes made to CSS, JS, and PHP files in the Public folder will cause a rebuild to happen. Which is unlike create-react-app
which ignores most of the files in the Public folder.
After you've created a new theme, and all the setup is done, you'll find a file named react-src/user.dev.json
that has some configuration options that you can mess with if you need to.
If you develop using SSL (i.e. HTTPS), then you might want to run the Browser Refresh Server
under SSL as well. Especially if you use Firefox, see here: Firefox Websocket security issue.
To configure the Browser Refresh Server
to use SSL, follow these steps:
create-react-wptheme
, change directory into the react-src
folder in your theme's folder
cd C:\xampp\htdocs\wordpress\wp-content\themes\<Your Theme's Root Folder>\react-src
cd /xampp/htdocs/wordpress/wp-content/themes/<Your Theme's Root Folder>/react-src
mkdir ssl
ssl
folder
cd ssl
.pem
extension, or each file has a different extension like .crt
and .key
.user.dev.json
in the folder named react-src
in your theme.
"wpThemeServer": { "enable": true, "host": "127.0.0.1", "port": 8090, "sslCert": "ssl/localhost.crt", "sslKey": "ssl/localhost.key", "watchFile": "../index.php" },
sslCert
and sslKey
items. Make sure the values point to your SSL certificate and key files.react-src
folder (as shown above).host
to 127.0.0.1
instead of "localhost". Supposedly the numeric address gets special treatment at the OS level as being mostly safe.react-src
folder.
cd ..
npm run start
user.dev.json
above.
127.0.0.1
and port 8090
as shown above, then open your browser to:
Browser Refresh Server
.While you're actively developing your theme, the files are not optimized for production. Before you put your theme into production, you need to run the build
command.
Open a command prompt and change into the react-src
folder of you theme and run this command:
npm run build
When that command finishes, your optimized files are located in a folder that can be deployed to your production server.
Here's an example showing which folder to deploy to your server:
<- deploy this folder to your production server's themes folder
If you need to continue developing your theme, simply:
cd react-src
npm run start
And all your theme files will reappear.
The react-src/user.prod.json
configuration file is read when you run npm run build
. The only option in there is setting the "homepage"
which controls the relative linking to files in your theme. The "homepage" setting in your main package.json
file is used during development (and build by default).
During development, this is probably what you want. But if you know your production server has a different root, then you can set the homepage to be different during a production build.
For example:
"homepage": "/wordpress/wp-content/themes/<Your Theme's Root Folder>"
/wordpress
part, so set the "homepage" line in your user.prod.json
file to:
"homepage": "/wp-content/themes/<Your Theme's Root Folder>"
npm run build
create-react-app
react-scripts
as little as possible.
I'm grateful to the authors of existing related projects for their ideas and collaboration:
The original.
I used this as an example for writing my own plugin for watching changes to the create-react-app "public" folder.
Create React WP Theme is open source software licensed as MIT.