Skip to content
TheChatArchiveThe Chat Archive
Qwenqwen3-235b-a22b-no-thinking· Alibaba (Qwen)Hugging Face LMArena Human Preference 140K

make me a wordpress plugin that will easily let me use React

From a Chatbot Arena battleThe same person sent this prompt to two anonymous models at once. Compare it with o3-2025-04-16’s answer.
  1. User

    make me a wordpress plugin that will easily let me use React

  2. Qwen Assistant

    Creating a WordPress plugin that lets you easily use React is a great way to bring modern JavaScript (JSX/React) into your WordPress site.

    Below is a simple WordPress plugin boilerplate that:

    • Enqueues a React app
    • Renders it in a shortcode [react_app]
    • Uses the React and ReactDOM libraries from WordPress core, so no need to install or bundle them yourself

    ✅ Step 1: Create Plugin Folder

    In your WordPress plugins folder:

    /wp-content/plugins/react-app-shortcode/
    

    Create the following files inside that folder:

    1. react-app-shortcode.php (Main plugin file)

    <?php
    /*
    Plugin Name: React App Shortcode
    Description: Easily embed a React app using a shortcode [react_app]
    Version: 1.0
    Author: Your Name
    */
    
    defined('ABSPATH') or die('No script kiddies please!');
    
    // Register shortcode
    function react_app_shortcode() {
        ob_start();
        ?>
        <div id="react-app"></div>
        <?php
        return ob_get_clean();
    }
    add_shortcode('react_app', 'react_app_shortcode');
    
    // Enqueue scripts and styles
    function enqueue_react_app() {
        // Only load on pages that have the shortcode
        if (has_shortcode(get_post()->post_content, 'react_app')) {
            wp_enqueue_script(
                'react-app',
                plugins_url('build/index.js', __FILE__),
                ['react', 'react-dom'],
                filemtime(plugin_dir_path(__FILE__) . 'build/index.js'),
                true
            );
    
            wp_enqueue_style(
                'react-app-style',
                plugins_url('build/index.css', __FILE__),
                [],
                filemtime(plugin_dir_path(__FILE__) . 'build/index.css')
            );
        }
    }
    add_action('wp_enqueue_scripts', 'enqueue_react_app');
    

    2. Create src/index.js (Your React code)

    This is where you write your React components.

    import React from 'react';
    import ReactDOM from 'react-dom';
    
    const App = () => {
        const [count, setCount] = React.useState(0);
        return (
            <div style={{ padding: '20px', border: '1px solid #ccc' }}>
                <h2>Hello from React!</h2>
                <p>You clicked {count} times</p>
                <button onClick={() => setCount(count + 1)}>Click me</button>
            </div>
        );
    };
    
    document.querySelectorAll('#react-app').forEach(container => {
        ReactDOM.render(<App />, container);
    });
    

    3. Optional: src/index.css

    You can add some basic styling for your React app.

    body {
        font-family: sans-serif;
    }
    

    ✅ Step 2: Build Your React App

    To make this work, we need to build our React app with a bundler like Vite, Webpack, or Parcel.

    We'll use Vite here as an example because it's fast and easy.

    Install Vite and dependencies (inside your plugin folder):

    cd /path/to/wp-content/plugins/react-app-shortcode
    npm create vite@latest . --template react
    # When asked, say NO to TypeScript, CSS pre-processors, etc.
    npm install
    

    Then update the vite.config.js to output the build correctly:

    Update vite.config.js:

    import { defineConfig } from 'vite';
    import react from '@vitejs/plugin-react';
    
    export default defineConfig({
      plugins: [react()],
      build: {
        outDir: 'build',
        emptyOutDir: true,
        rollupOptions: {
          input: {
            main: './src/main.jsx'
          }
        }
      }
    });
    

    If you're using the standard index.js, rename main.jsx to index.jsx.


    Add a package.json script to build:

    "scripts": {
      "dev": "vite",
      "build": "vite build"
    }
    

    Now run:

    npm run build
    

    This will generate a /build folder containing index.js and index.css.


    ✅ Step 3: Activate and Use the Plugin

    1. Go to WordPress Admin → Plugins
    2. Find React App Shortcode and activate it.
    3. In any post/page, insert the shortcode:
      [react_app]
      

    🧠 Notes

    • This setup uses WordPress’ built-in React (wp.element) when available (since WP 5.8+).
    • For advanced apps, consider using create-react-app or Vite + custom bundling.
    • You can also use JSX directly by setting up Babel in your build process.

    ✅ Final File Structure

    react-app-shortcode/
    ├── react-app-shortcode.php       ← Main plugin PHP file
    ├── package.json                  ← npm config
    ├── vite.config.js                ← Vite bundler config
    ├── src/
    │   ├── main.jsx                  ← React entry point
    │   ├── App.jsx                   ← Your React component
    │   └── index.css                 ← Styles
    └── build/
        ├── index.js                  ← Built JS
        └── index.css                 ← Built CSS
    

    Let me know if you'd like:

    • A version using Create React App
    • To pass data from PHP to React (e.g., user info)
    • A block-based editor integration (Gutenberg)

    Happy coding! 🚀

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.