Why React and WordPress
With WordPress you get:
- easy content management for help pages, your landing page, your blog, and your other marketing activities
- secure and proven user management
- over 100.000 plugins
- a lot of flexibility over time to change the character of your site
For example, if we want to write a new revolutionary email app, we can write the interface of the email client with React, but for everything else use WordPress. So while you’re developing your app, you might want to:
- choose and install a theme
- create a landing page for your app
- add a form to collect email addresses
- create a blog and publish your posts on Twitter to promote your app
- optimize your site for search engines
Later it might be useful to
- offer a paid membership
- create a forum or FAQ
- create separate landing pages
- all this can easily be done with WordPress, without writing a single line of code.
Embedding a React app into WordPress with ReactPress
While there are other ways to integrate React with WordPress, the ReactPress plugin is the easiest to embed a React app into a WordPress page and lets you use create-react-app without any custom build configurations.
Not only does ReactPress automate the React integration for you, but it also ensures a seamless development experience, through running your local React dev server with the theme of your WordPress site.
Why not just using headless WordPress with SSR
I know that headless WordPress with Next.js, Gatsby, or Frontity is all the rage right now, but with these solutions, you add a layer of complexity to your app, and more importantly, you lose a lot of the benefits of the WordPress ecosystem (themes and plugins).
The steps from development to deployment are:
- Setup your local dev environment.
- Install ReactPress on your local WordPress installation
- Create a new React app from your WP installation
- Create a new React app
- Use create-react-app from the command line (recommended)
- Create a new React app from your local WP admin
- Develop your React app
- Build the app
- Install ReactPress on live WordPress site
- Create the same React app there
- Upload the build of your React app to your live site to deploy it.
Repeat steps 3, 4 and 7 until your app is finished.
Setup your local dev environment.
To develop React apps your WordPress installations needs access to:
- the PHP function
- and a POSIX compatible system
Optionally, to create React apps directly from the WordPress admin it needs also:
npmversion 6 or higher
This means you can’t create React apps from the WordPress admin if you use Local by Flywheel, but you can create your app from the command line with
npx create-react-app my-app.
For Windows users we recommend using WSL-2.
Install ReactPress on your local WordPress installation
In your local WordPress go to plugin installation, search for ReactPress, install and activate the plugin.
Create a new React app
There are 2 ways to create a new react app with ReactPress. You can use the command line or you can use the admin page of ReactPress.
We recommend using the command line because you more control and feedback about what is going and you can use WordPress development environments like Local without worrying about npm access of your WordPress instance.
Creating a React app from the WordPress admin is now deprecated and only present for legacy reasons.
Use create-react-app from the command line (recommended)
To create a new app from the command line open a terminal and navigate to
apps directory of the ReactPress plugin.
npx create-react-app [your-app-name] in the
Then go to the ReactPress page in your WordPress admin. If you already opened the ReactPress page before, reload it.
You then will see the new app in the admin. Next, choose a URL Slug for your app. This will create a new page with the given slug. Make sure the slug you choose is not taken by any other page/post of your site.
Go back to your console, start the React app with
npm start or
yarn start. Your React app should be running with the styles of your WordPress site.
Create a new React app from your local WP admin
In the admin of your WP installation, click on ReactPress in the sidebar. There you should see a form to create a new React app.
Enter a name (later you need to use the same name in the live system), enter a pageslug, choose the type “Develop a new app (Usually on a local machine).” and choose the create-react-app-template you would like to use and finally click the “Create React App” button.
After a short while your app should be created and running at port: 3000. If you click on the link you can see your local dev app running in the theme of your WP installation.
Develop your React app
Build the app
If you want to deploy to your live site, build your app from the command line or through the WP admin – if your system fulfills the requirments.
Again, we recommend using the command line, because you have access to error messages.
Install ReactPress on live WordPress site
Before you upload your React app, install ReactPress on your live site the same way you did on your local installation.
Create the same React app there
Create the same React app on your live system, that you created on your local system. Important! Use the exact same name and this time choose “Deploy an already build app (Usually on a server).” as the type.
Upload the build of your React app to your live site to deploy it.
Finally, upload the build of your React app. Upload the build folder of your local React app to the app on your live system.
The ReactPress admin shows where your apps are located. It will be something like this:
If you click on the URL slug of your React app in ReactPress you should see your React app on your live system.
Where to go from here?
You should be able to create React app embedded in WordPress now. If you have the next big app idea you are ready to start.
If you have any questions, let me know in the comments.