React Integration
Time to complete: ~25 minutes
Prerequisites: Completed SDK Integration
This section guides you through building a React-based license management page using PaidCommunities' React components. We'll create a modern, interactive interface for license management.
Project Setup
Initialize NPM
First, let's set up npm in your plugin's root directory:
npm init
Install Dependencies
After creating package.json, install the PaidCommunities React components:
npm install @paidcommunities-wp/components
Update your package.json with the required dependencies:
{
"name": "awesome-seo",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"@paidcommunities-wp/components": "^1.0.0"
},
"devDependencies": {
"@babel/cli": "^7.25.7",
"@babel/core": "^7.25.8",
"@babel/plugin-proposal-async-generator-functions": "^7.20.7",
"@babel/plugin-proposal-class-properties": "^7.18.6",
"@babel/plugin-proposal-object-rest-spread": "^7.20.7",
"@babel/preset-env": "^7.25.8",
"@babel/preset-react": "^7.25.7",
"@wordpress/browserslist-config": "^6.10.0",
"babel-loader": "^9.2.1",
"css-loader": "^7.1.2",
"mini-css-extract-plugin": "^2.9.1",
"postcss-loader": "^8.1.1",
"postcss-preset-env": "^10.0.7",
"sass": "^1.80.3",
"sass-loader": "^16.0.2",
"webpack": "^5.95.0",
"webpack-cli": "^5.1.4"
}
}
Install all dependencies:
npm install
Webpack Configuration
Create your webpack configuration file:
const path = require('path');
const config = {
devtool: 'source-map',
entry: {
adminApp: path.resolve(__dirname, './assets/js/admin-app.js')
},
output: {
path: path.resolve(__dirname, 'build'),
filename: 'admin-app.js'
},
module: {
rules: [
{
test: /\.m?jsx?$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader',
options: {
cacheDirectory: true,
presets: [
[
'@babel/preset-env',
{
modules: false,
targets: {
browsers: [
'extends @wordpress/browserslist-config'
]
}
}
],
'@babel/preset-react'
],
plugins: [
require.resolve('@babel/plugin-proposal-object-rest-spread'),
require.resolve('@babel/plugin-proposal-async-generator-functions'),
require.resolve('@babel/plugin-proposal-class-properties'),
]
}
}
}
]
},
resolve: {
extensions: ['.js', '.jsx']
},
externals: {
'react': 'React',
'react-dom': 'ReactDOM',
'@wordpress/components': 'wp.components',
'jquery': 'jQuery'
}
};
module.exports = config;
React Application
Create the directory for your React application:
mkdir -p assets/js
Create your main React application file:
import React from 'react';
import {createRoot} from 'react-dom/client';
import {LicenseActivation} from '@paidcommunities-wp/components';
const App = () => {
return (
<LicenseActivation config={awesomeSEOParams}/>
)
}
const root = createRoot(document.getElementById('app'));
root.render(<App/>);
Building the Application
The build process requires several key scripts:
clean:build: Removes any previous build filesbuild: Creates a production-ready bundle using webpack
Add these scripts to your package.json:
"scripts": {
"clean:build": "rm -rf ./build",
"build": "npm run clean:build && NODE_ENV=production webpack --mode=production"
},
During development, you might want to add a watch script to automatically rebuild when files change:
"scripts": {
"clean:build": "rm -rf ./build",
"build": "npm run clean:build && NODE_ENV=production webpack --mode=production",
"watch": "webpack --watch --mode=development"
}
Execute the build script:
npm run build
After building, your plugin directory should look like this (new files/directories highlighted):
├── awesome-seo/
│ ├── assets/
│ ├── build/
│ │ └── admin-app.js
│ ├── node_modules/
│ ├── src/
│ ├── vendor/
│ ├── .gitignore
│ ├── awesome-seo.php
│ ├── composer.json
│ ├── composer.lock
│ ├── package.json
│ ├── package-lock.json
│ └── webpack.config.js
WordPress Integration
Update your LicensePage class to integrate the React application:
<?php
namespace AwesomeSEO\Admin;
use PaidCommunities\WordPress\PluginConfig;
class LicensePage {
private $config;
public function __construct( PluginConfig $config ) {
$this->config = $config;
add_action( 'admin_init', [ $this, 'register_scripts' ] );
}
public function register_scripts() {
$url = plugin_dir_url( dirname( __DIR__ ), 2 );
wp_register_script(
'awesome-seo-admin-app',
$url . 'build/admin-app.js',
[ 'jquery', 'react', 'wp-components', 'paidcommunities-wp-api' ],
$this->config->getVersion(),
true
);
}
public function initialize() {
wp_enqueue_style( 'paidcommunities-wp-components' );
wp_enqueue_style( 'wp-components' );
wp_enqueue_script( 'paidcommunities-admin-license' );
wp_enqueue_script( 'awesome-seo-admin-app' );
wp_localize_script( 'awesome-seo-admin-app', 'awesomeSEOParams', $this->config->getPluginData() );
}
public function render() {
?>
<div class="awesome-seo-license-management">
<div class="awesome-seo-title">
<h2><?php esc_html_e( 'License Management', 'awesome-seo' ) ?></h2>
</div>
<div>
<p>
<?php esc_html_e( 'On this page you can activate or deactivate your Awesome SEO license.', 'awesome-seo' ) ?>
</p>
</div>
<?php $this->config->getLicenseSettings()->render(); ?>
<br>
<br>
<div>
<h3>React App</h3>
<div id="app"></div>
</div>
</div>
<?php
}
}
Note that the variable name in wp_localize_script ('awesomeSEOParams') must match the config variable name used in admin-app.js.
The screenshot above shows both license activation interfaces:
- Top: The PHP-rendered license activation field
- Bottom: Our new React-based implementation
This dual implementation demonstrates how you can gradually migrate to the React components or use them alongside traditional PHP rendering.
Next Steps
At this point, you have:
- Set up a React development environment
- Integrated PaidCommunities' React components
- Created a modern license management interface
Next Steps
At this point, you have:
- Set up a React development environment
- Integrated PaidCommunities' React components
- Created a modern license management interface
In the next section, we'll explore how to customize the license activation interface by:
- Creating custom PHP templates
- Modifying the default UI elements
- Adding your own branding and styling
- Implementing custom validation messages
This will allow you to create a seamless experience that matches your plugin's design while maintaining all the functionality of the PaidCommunities licensing system.