WordPress + Create React App Integration (2024)

The last few years have revealed a trend of improved UI development in the WordPress environment with Frontend frameworks such as React and Vue. Bear that in mind when you see a React error in the console of your website. The source of the error may be a React-app 3rd party plugin and not your own development efforts!

React has long been a go-to for 3rd party plugins, for example, Yoast’s SEO plugin renders widgets in WordPress admin pages with a React App. Now, the ability to develop React apps in WordPress has been eased with the launch of Gutenberg. The Gutenberg block editor has React at its core.

This article will walk through how you can add a separate React Create App into your WordPress site, not about Gutenberg React blocks.

You may have read about WordPress & React headless projects that enable a website to render as a React app from WordPress API endpoints. That process should not be confused with the subject of this article. Here we focus on rendering a React Create App inside a specific WordPress page in the WordPress admin or the site frontend. For this demo, we “take over” a div that already is defined inside a page.

So, how does a typical React-App-deployment setup compare to a React App embedded into WordPress? In a common app, the server will serve an index.html file from the “build” folder, which contains a script tag to load the app JavaScript bundle. However, inside the WordPress environment, the HTML is generated dynamically from PHP files.

That requires two steps to create a typical WordPress & React integration:

  1. Adding a script tag to the HTML for loading the application’s JavaScript files.
  2. Adding a div with a selector that we can match to the app root selector to render within. (However, we can skip this step if we hijack an existing div as we do in this demo).
WordPress + Create React App Integration (1)

We can add functionality to our WordPress installation in 1 of 2 ways:

  1. Add the code to a child theme (or the main theme if we maintain it).
  2. Write a custom plugin.

In this article, we will create a custom plugin. However, this concept could be applied to customize a theme, as well.

Lets run this lines it our Terminal:

The plugin folder structure should look similar to the image below after the React plugin and React App creation and build have completed.

Before moving on, let’s ensure that the plugin’s screen for the plugin we created was activated on our dashboard.

WordPress + Create React App Integration (3)

Let’s start by loading the React App on the main dashboard page in the WordPress admin panel.

The selector '#wpbody .wrap’ will place the app in your dashboard and replace the default WordPress widgets.

WordPress + Create React App Integration (4)

React app outputs the assets CSS and JavaScript files in chunks in the build/assets/jsand build/assets/css folders. As we can see in the build folder, we have assets chunks files with random names.

WordPress + Create React App Integration (5)

This creates challenges such as, how can we load the correct asset files if each build’s names may change? Would we need to update after each build with the names of the new assets files?

One way around this is by disabling code splitting by modifying the Webpack build settings. This way, we always load one CSS file and one JavaScript file with the names we defined in the Webpack settings.

Since we can get the list of the files from the react-app/build/asset-manifest.jsonfile, we will keep code-splitting on, so we won’t need to change any default Webpack settings.

Task Summary:

  1. We now we want to load the `entryload` files from the react-app/build/asset-manifest.json file.
  2. Set our React App to load in `#wpbody .wrap` in the WordPress main dashboard page.

React-Plugin.php

Our code will be added after the `Your code starts here` comment.

Add logic to parse the asset-manifest.json file, loop over the entry files found, and load them in the main page of WordPress admin:

react-app/src/index.js

Modify the file to get the selector root from the window.rpReactPlugin:

react-app/package.json

If we build the app at the stage withyarn build, the app would load, but our image will not. Since the app is looking for the image in the main domain folder instead of the React App folder.

WordPress + Create React App Integration (7)

To fix this, we should add inreact-app/package.jsonthe entry :

`homepage`: `/wp-content/plugins/wp-create-react-app-concept/react-app/build`

Now we can run yarn buildand the app should display correctly.

WordPress + Create React App Integration (8)

For this demo, we will load the app it the footer of the site’s homepage.

The path will be very similar with a few changes.

  1. We load the files inwp_enqueue_scriptshook instead of admin_enqueue_scripts hook we used in WordPress dashboard.
  2. The condition to check if we are in the correct page is the is_front_end()WordPress helper function, instead of the admin hook we used to check current admin screen in WordPress dashboard.
  3. We will update the selector in main.js to the correct app root selector #site-footer

After yarn buildwe should see the app in the footer of the main frontend page of the site.

WordPress + Create React App Integration (9)

Since we may want to reuse this concept in different sites with minimal changes to the code, we can wrap everything in one class with parameters. This class will now load the React app on the frontend home page and the main WordPress admin page.

There are two main benefits of wrapping our code in a class plugin:

  1. Our code is encapsulated in the class, so we don’t need to add custom prefixes to our function’s names.
  2. It is simpler to reuse the same class to load code in the frontend and the WordPress admin.

Notes about the code:

  • Code was written in PHP 7.4
  • The “WordPress way” for fetching the files would be wp_remote_get. We used here file_get_contents for keeping the demo simple.

Below are listed 3 branches with the 3 code versions. Remember to run yarn && yarn build in the react-app folder to build the bundle.

Github wp-admin function version

https://github.com/BenBroide/wp-create-react-app-concept/tree/function-version-wp-admin

Github front end function version

https://github.com/BenBroide/wp-create-react-app-concept/tree/front-end-function-version

Github Class version

https://github.com/BenBroide/wp-create-react-app-concept/tree/class-plugin

WordPress + Create React App Integration (2024)
Top Articles
What to stream: Post Malone goes country, Sydney Sweeney plays a nun and Madden 25 hits the field
Fototour verlassener Fliegerhorst Schönwald [Lost Place Brandenburg]
Forozdz
Amc Near My Location
His Lost Lycan Luna Chapter 5
Asian Feels Login
Bucks County Job Requisitions
Directions To 401 East Chestnut Street Louisville Kentucky
Decaying Brackenhide Blanket
Katie Boyle Dancer Biography
Campaign Homecoming Queen Posters
Florida (FL) Powerball - Winning Numbers & Results
Camstreams Download
Osrs Blessed Axe
Shariraye Update
Newgate Honda
David Turner Evangelist Net Worth
Betonnen afdekplaten (schoorsteenplaten) ter voorkoming van lekkage schoorsteen. - HeBlad
Classic Lotto Payout Calculator
D10 Wrestling Facebook
Diesel Mechanic Jobs Near Me Hiring
Bahsid Mclean Uncensored Photo
Uc Santa Cruz Events
Eva Mastromatteo Erie Pa
Las 12 mejores subastas de carros en Los Ángeles, California - Gossip Vehiculos
Sadie Proposal Ideas
Poe Str Stacking
Dewalt vs Milwaukee: Comparing Top Power Tool Brands - EXTOL
Trivago Myrtle Beach Hotels
Lovindabooty
Orange Park Dog Racing Results
What is Software Defined Networking (SDN)? - GeeksforGeeks
LG UN90 65" 4K Smart UHD TV - 65UN9000AUJ | LG CA
Puffin Asmr Leak
Christmas Days Away
Jeep Cherokee For Sale By Owner Craigslist
Restaurants Near Calvary Cemetery
6143 N Fresno St
Petsmart Distribution Center Jobs
B.k. Miller Chitterlings
Mississippi State baseball vs Virginia score, highlights: Bulldogs crumble in the ninth, season ends in NCAA regional
Autozone Locations Near Me
Hannibal Mo Craigslist Pets
Myanswers Com Abc Resources
Craigslist Malone New York
Deepwoken: How To Unlock All Fighting Styles Guide - Item Level Gaming
Az Unblocked Games: Complete with ease | airSlate SignNow
Pgecom
'The Nun II' Ending Explained: Does the Immortal Valak Die This Time?
Every Type of Sentinel in the Marvel Universe
Cognitive Function Test Potomac Falls
Mazda 3 Depreciation
Latest Posts
Article information

Author: Terrell Hackett

Last Updated:

Views: 6126

Rating: 4.1 / 5 (72 voted)

Reviews: 87% of readers found this page helpful

Author information

Name: Terrell Hackett

Birthday: 1992-03-17

Address: Suite 453 459 Gibson Squares, East Adriane, AK 71925-5692

Phone: +21811810803470

Job: Chief Representative

Hobby: Board games, Rock climbing, Ghost hunting, Origami, Kabaddi, Mushroom hunting, Gaming

Introduction: My name is Terrell Hackett, I am a gleaming, brainy, courageous, helpful, healthy, cooperative, graceful person who loves writing and wants to share my knowledge and understanding with you.