devloco / create-react-wptheme

Create modern, React-enabled WordPress themes with a single command.
MIT License
359 stars 60 forks source link
create-react-app php react react-theme react-tutorial react-wordpress-themes reactjs wordpress wordpress-api wordpress-development wordpress-server wordpress-starter-theme wordpress-theme

Create React WP Theme

Still Up To Date!

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.


Getting Started

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)

Readme Contents

For more details check out the rest of this document.

Creating a New Theme

To create a WordPress theme using create-react-wptheme, follow these steps.

Updating Existing Themes

When new versions of Create-React-WPTheme are released, you can easily upgrade an existing theme using a single command.

Updating TypeScript Types

If your theme uses TypeScript, you'll need to modify the theme's react-app-env.d.ts file:

Developing Your Theme

React Tutorials

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 Public Folder

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.

Dev Configuration

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.

HTTPS/SSL Support

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:

Deploying Your Theme

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:

If you need to continue developing your theme, simply:

And all your theme files will reappear.

Dealing With Differing Paths Between DEV and PROD

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:

Goals

Acknowledgements

I'm grateful to the authors of existing related projects for their ideas and collaboration:

License

Create React WP Theme is open source software licensed as MIT.