Building a Modern WordPress Plugin with React and Vite

Building a Modern WordPress Plugin with React and Vite

WordPress plugin development has come a long way. While PHP still powers the backend, modern plugins often need interactive, dynamic frontends. From dashboards to job boards, handling complex user interfaces purely with PHP can become messy and hard to maintain. That’s where React comes in — it allows you to build responsive, state-driven UIs while WordPress continues managing the backend logic.

In this guide, we’ll explore how to build a modern WordPress plugin, following PSR-4 autoloading and PSR-12 coding standards, and integrate React using Vite with Hot Module Replacement (HMR) for a seamless development experience.

Why Use React in WordPress Plugins?

Many plugin developers default to PHP-rendered templates, which work for simple forms or static pages. However, for interactive features, such as job boards, dashboards, or real-time listings, PHP alone is limited.

React provides:

  1. Component-based architecture
    Each piece of your UI can be modularized as a React component, making it reusable and easier to maintain.
  2. Efficient state management
    Handling forms, filters, or live updates becomes much easier than juggling PHP and JavaScript manually.
  3. Developer experience with modern tooling
    Tools like Vite provide a fast dev server and HMR, so you can see UI changes instantly without full page reloads.
  4. Scalable architecture
    Separating frontend from backend logic makes your plugin maintainable, especially for complex or multisite projects.

By combining WordPress and React, you get the best of both worlds: WordPress handles data storage, routing, and security, while React handles the user-facing interface efficiently.

Setting Up the Plugin (PSR-4 and PSR-12 Compliant)

A professional plugin should follow PSR-4 autoloading for class organization and PSR-12 standards for consistent code formatting. Here’s how you can structure a plugin for a WordPress job board:

my-plugin/
├── src/
│   ├── Plugin.php
│   ├── Admin/
│   │   └── Dashboard.php
│   └── Frontend/
│       └── JobBoard.php
├── vendor/
├── composer.json
└── my-plugin.php

composer.json

{
    "autoload": {
        "psr-4": {
            "MyPlugin\\": "src/"
        }
    }
}

Main plugin file (my-plugin.php)

<?php
/**
 * Plugin Name: My Plugin
 * Description: A custom WordPress plugin.
 */

use MyPlugin\Plugin;

require __DIR__ . '/vendor/autoload.php';

Plugin::init();

Plugin class (src/Plugin.php)

<?php
namespace MyPlugin;

class Plugin {
    public static function init() {
        // Register admin pages
        Admin\Dashboard::register();

        // Register frontend features
        Frontend\JobBoard::register();
    }
}

By adhering to PSR-4 and PSR-12, your plugin is maintainable, readable, and professional, making it easier for teams or future developers to work on.

Integrating React

Once the plugin structure is ready, you can integrate React to handle interactive UI components. We’ll use Vite for development because it offers fast rebuilds and HMR, which significantly improves productivity.

React Project Setup with Vite

npm create vite@latest app
cd app
npm install
npm run dev

This starts a development server with HMR enabled. You can now start building your React components, like job listings or filters, that interact with your WordPress backend via REST API or custom endpoints.

Enqueue React and Vite Scripts in WordPress

In WordPress, React scripts need to be enqueued properly, especially in development, to load the HMR scripts. Here’s how to do it:

function enqueue_vite_hmr() {
    if ( defined('WP_ENV') && WP_ENV === 'development' ) {
        // Load Vite HMR client
        wp_enqueue_script(
            'vite-hmr-client',
            'http://localhost:5173/@vite/client',
            [],
            null,
            true
        );

        // Load the main React app
        wp_enqueue_script(
            'my-react-app',
            'http://localhost:5173/src/main.jsx',
            ['vite-hmr-client'],
            null,
            true
        );
    }
}
add_action('wp_enqueue_scripts', 'enqueue_vite_hmr');

Why this matters:

  • HMR client must load before your React entry point to enable live updates.
  • The check WP_ENV ensures these scripts don’t load in production.
  • This approach works seamlessly, even for complex plugins such as job boards or dashboards.

Benefits of This Setup

  • Fast iteration: HMR lets you see UI changes instantly without a page reload.
  • Separation of concerns: WordPress handles backend logic; React handles the frontend.
  • Scalable and maintainable: PSR-4 + component-based React architecture keeps code organized.
  • Future-proof: Adding more features, like filtering, live updates, or multisite support, becomes simpler.

Conclusion

Modern WordPress plugin development isn’t just about PHP anymore. By combining React for the frontend with PSR-4/PSR-12 compliant plugin architecture, and using Vite with HMR for development, you can build interactive, maintainable, and scalable plugins.

This setup is ideal for job boards, dashboards, or any plugin requiring complex user interfaces.

If you’re experimenting with React in WordPress, I’d love to hear your approach or tips. Leave a comment below!


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

Share via
Copy link