UNPKG

13.2 kBMarkdownView Raw
1# Create React WP Theme <!-- omit in toc -->
2
3Dec. 15, 2019
4<br />
5[v3.3.0](https://github.com/facebook/create-react-app/releases/tag/v3.3.0) of [Create React App](https://create-react-app.dev/) was released. It's a major change and it's going take me another week to make the changes to `create-react-wptheme`.
6
7But don't let that stop you from creating a new theme now, as updating your theme is quite painless (see [Updating Existing Themes](#updating-existing-themes)) below.
8
9The current release of `create-react-wptheme` is based on [v3.2.0](https://github.com/facebook/create-react-app/releases/tag/v3.2.0) of [Create React App](https://create-react-app.dev/).
10
11---
12
13The intention of this project is to maintain a set of custom `react-scripts` that will allow you to
14create React WordPress themes as easily as `create-react-app` allows other devs to create their apps.
15
16The biggest difference with this project and the original is that **it uses your WordPress server as the
17development server instead of the Webpack Dev Server.**
18
19## Getting Started <!-- omit in toc -->
20
21[Michael Soriano](https://github.com/michaelsoriano) 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.
22
23Check it out:
24<br>
25[Let's build a WordPress theme with React: Part 1 (Setup)](http://michaelsoriano.com/wordpress-theme-react-part-1-setup/)
26
27## Readme Contents <!-- omit in toc -->
28
29For more details check out the rest of this document.
30
31- [Creating a New Theme](#creating-a-new-theme)
32- [Updating Existing Themes](#updating-existing-themes)
33- [Developing Your Theme](#developing-your-theme)
34 - [React Tutorials](#react-tutorials)
35 - [The Public Folder](#the-public-folder)
36 - [Dev Configuration](#dev-configuration)
37 - [HTTPS/SSL Support](#httpsssl-support)
38- [Deploying Your Theme](#deploying-your-theme)
39 - [Dealing With Differing Paths Between DEV and PROD](#dealing-with-differing-paths-between-dev-and-prod)
40- [Goals](#goals)
41- [Acknowledgements](#acknowledgements)
42- [License](#license)
43
44## Creating a New Theme
45
46To create a WordPress theme using the `create-react-wptheme`, follow these steps.
47
48- Make sure your WordPress server is up and running.
49- Change dir into your WordPress themes folder (**this is just an example, use your real themes folder**).
50 - Windows: `cd C:\xampp\htdocs\wordpress\wp-content\themes`
51 - Mac or \*nix: `cd /xampp/htdocs/wordpress/wp-content/themes`
52- Use `npx create-react-wptheme` to make a new theme
53 - For example: (**replace "my_react_theme" with whatever you want your theme to be named**):
54 - `npx create-react-wptheme my_react_theme`
55 - If you want to use TypeScript, then the command would be:
56 - `npx create-react-wptheme my_react_theme --typescript`
57- When it finishes it'll tell you to change into your new theme's folder and run the Nodejs watcher (replace "my_react_theme" with the same name you used in the previous step):
58 - `cd my_react_theme/react-src`
59 - `npm run start`
60- That sets up the theme so that it can be seen in the WordPress admin area.
61 - **Go there now and set your WordPress site to use this theme.**
62- View the site in your browser with the new theme.
63 - **You must do this as it does some extra setup via PHP.**
64 - **If you get PHP errors, most likely your web server doesn't have write access to your theme.**
65 - Write access for your web server is only needed during this setup step.
66 - **You can revoke write access after the setup has completed.**
67 - Interested (paranoid?) about what it's doing? Check out the file: `<your theme folder name>/index.php`
68 - When that's done you'll see: `Now, back in your command prompt, rerun the "start" script again...`
69 - To do that, go back to your command prompt where you first ran `npm run start` and rerun that same command again.
70- In a few seconds you should see your browser load with the standard create-react-app page, but it's running as a WordPress theme!
71
72## Updating Existing Themes
73
74When new versions of `Create React WP Theme` are released, you can upgrade an existing theme using a single command.
75
76From within your theme's `react-src` folder, run this command at a command prompt:
77
78`npm install @devloco/react-scripts-wptheme@latest`
79
80If your theme uses TypeScript, you'll need to run a second command (`create-react-wptheme` uses the Types from Facebook's React-Scripts):
81
82`npm install react-scripts@latest`
83
84**Important note for Yarn users: be sure to use `yarn add` instead of `npm install` in the above commands.**
85
86## Developing Your Theme
87
88### React Tutorials
89
90If you're looking at a React tutorial on the web, you can use `create-react-wptheme` in place of `create-react-app`.
91<br>But you do have to remember that the React app code is one extra folder down inside your theme folder.
92<br>Notice that the final folder in this path is `react-src`:
93
94`/xampp/htdocs/wordpress/wp-content/themes/<Your-Create-React-WP-Theme-Folder>/react-src`
95
96So for example, if the tutorial instructs you to edit the `src/App.js` file, then for you, that file would actually be located at:
97
98`<Your-Create-React-WP-Theme-Folder>/react-src/src/App.js`
99
100### The Public Folder
101
102The 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.
103
104But 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.
105
106**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.
107
108### Dev Configuration
109
110After 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.
111
112### HTTPS/SSL Support
113
114If 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](https://stackoverflow.com/questions/11768221/firefox-websocket-security-issue).
115
116To configure the `Browser Refresh Server` to use SSL, follow these steps:
117
118- These instructions use the command prompt.
119- Assuming you've already created a theme using `create-react-wptheme`, change directory into the `react-src` folder in your theme's folder
120 - Be sure to follow **all the instructions** under the **Usage** section at the top of this document. You need to complete the PHP portion of the setup before you can configure SSL.
121 - Windows example: `cd C:\xampp\htdocs\wordpress\wp-content\themes\<your theme's folder name>\react-src`
122 - Mac or \*nix example: `cd /xampp/htdocs/wordpress/wp-content/themes/<your theme's folder name>/react-src`
123- Create a new folder to hold you're development SSL certificate and key.
124 - All OSes: `mkdir ssl`
125- Change directory into the `ssl` folder
126 - All OSes `cd ssl`
127- Then create the SSL certificate and key.
128 - Windows: Install the [Linux Subsystem for Windows 10](https://docs.microsoft.com/en-us/windows/wsl/install-win10), then use the \*nix instructions below.
129 - Mac, see here: https://ksearch.wordpress.com/2017/08/22/generate-and-import-a-self-signed-ssl-certificate-on-mac-osx-sierra/
130 - \*nix, see here (**also works with Linux Subsystem for Windows 10**): https://stackoverflow.com/questions/10175812/how-to-create-a-self-signed-certificate-with-openssl
131- Depending on which process you use to create the certificate and key, the files you created might have different extensions than those mentioned below.
132 - That's OK.
133 - Sometimes both files have a `.pem` extension, or each file has a different extension like `.crt` and `.key`.
134 - **Just be sure you know which file is the certificate and which is the key.**
135- There is a file named `user.dev.json` in the folder named `react-src` in your theme.
136 - Open that file in your favorite text editor.
137 - Change the "wpThemeServer" section to look like this (make sure it is proper JSON):
138 - `"wpThemeServer": { "enable": true, "host": "127.0.0.1", "port": 8090, "sslCert": "ssl/localhost.crt", "sslKey": "ssl/localhost.key", "watchFile": "../index.php" },`
139 - **NOTE** the `sslCert` and `sslKey` items. **Make sure the values point to your SSL certificate and key files.**
140 - The paths to those files can be **full paths** or **relative paths** from the `react-src` folder (as shown above).
141 - I like to set the `host` to `127.0.0.1` instead of "localhost". The numeric address gets special treatment at the OS level as being mostly safe.
142- Back in your command prompt, change dir back to the `react-src` folder.
143 - All OSes: `cd ..`
144- Start Node/Webpack JS watcher as you normally would:
145 - All OSes: `npm run start`
146 - Your theme should open in a new browser tab
147- If you need to add an SSL exception to your browser for your new certificate, there is a page running over HTTPS at the "host" and "port" you set in `user.dev.json` above.
148 - For example, if you're using host `127.0.0.1` and port `8090` as shown above, then open your browser to:
149 - https<nolink>://127.0.0.1:8090/
150 - From there you'll get the standard browser warning about self-signed certificates and get the option to add an exception.
151 - Once you've finished adding an exception to your browser, you'll need to refresh the tab with your development theme to force a reconnect to the `Browser Refresh Server`.
152
153## Deploying Your Theme
154
155While 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.
156
157Open a command prompt and change into the `react-src` folder of you theme and run this command:
158`npm run build`
159
160When that command finishes, your optimized files are located in a folder that can be deployed to your production server.
161
162Here's an example showing which folder to deploy to your server:
163
164- ...wp-content
165 - themes
166 - my-theme
167 - my-theme `<- deploy this folder to your production server's themes folder`
168 - react-src
169 - !READY_TO_DEPLOY!.txt
170
171If you need to continue developing your theme, simply:
172
173- `cd react-src`
174- `npm run start`
175
176And all your theme files will reappear.
177
178### Dealing With Differing Paths Between DEV and PROD
179
180The `react-src/user.prod.json` configuration file is read when you run `npm run build`. The only option in there is setting the "homepage"
181which 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).
182
183During 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.
184
185For example:
186
187- Your WordPress dev server is running at http<nolink>://localhost/wordpress
188 - the homepage setting in your main package.json file will probably work just fine.
189 - The homepage line in your main package.json will be something like: `"homepage": "/wordpress/wp-content/themes/<your theme's folder name>"`
190- But you know that your production server runs WordPress from the root: http<nolink>://mycoolblog.com/
191 - In this case you want to remove the `/wordpress` part, so set the "homepage" line in your `user.prod.json` file to:
192 `"homepage": "/wp-content/themes/<your theme's folder name>"`
193 - Then run `npm run build`
194 - **Note** at this point, **your theme will appear broken on your dev server**. But it should look as expected on your production server.
195
196## Goals
197
198- Remove WebPackDevServer and use your WordPress dev server instead.
199 - Also, do not proxy the WordPress server.
200 - Thus removing CORS as a concern.
201- Maintain feature parity(ish) with `create-react-app`
202- Touch the original `react-scripts` as little as possible.
203 - Add new files where possible.
204 - This will make merges easier.
205
206## Acknowledgements
207
208I'm grateful to the authors of existing related projects for their ideas and collaboration:
209
210- [create-react-app](https://github.com/facebook/create-react-app)
211
212 The original.
213
214- [filewatcher-webpack-plugin](https://www.npmjs.com/package/filewatcher-webpack-plugin)
215
216 I used this as an example for writing my own plugin for watching changes to the create-react-app "public" folder.
217
218## License
219
220Create React WP Theme is open source software [licensed as MIT](https://github.com/devloco/create-react-wptheme/blob/master/LICENSE).