Mahamudul Hasan Rubel
HomeBlogCoursesAboutProjectsSkillsExperiencePhotosContact
Mahamudul Hasan Rubel

Senior Software Engineer crafting high-performance web applications and SaaS platforms.

Navigation

  • Home
  • Blog
  • Courses
  • About
  • Projects
  • Skills
  • Experience
  • Photos
  • Contact

Get in Touch

Available for senior/lead roles and consulting.

bd.mhrubel@gmail.comHire Me

© 2026 Mahamudul Hasan Rubel. All rights reserved.

Built with using Next.js 16 & Tailwind v4

Back to Blog
Lesson 15 of the Intermediate WordPress Plugins: REST API & React Admin course
WordPressJune 25, 20263 min read

Creating a React Form for Submissions in WordPress

Learn to build interactive React forms in WordPress using controlled components. Master state management and submission events for your admin dashboard.

ReactWordPressFormsControlled ComponentsJavaScriptphpplugin-development

Previously in this course, we covered working with @wordpress/components to build the visual shell of our admin dashboard. Now that we have the layout, it's time to make it functional. In this lesson, we are building the "Add Entry" form, which is the heart of our plugin's data entry interface.

To build effective user interfaces, we must master the concept of Forms and Controlled components. In React, a controlled component is a form element whose value is driven entirely by state, making the data flow predictable and easy to debug.

Understanding Controlled Components

In vanilla HTML, form elements like <input> or <textarea> maintain their own internal state (the text currently typed into the box). In React, we shift that responsibility to the component's state.

When you use a controlled component, the input's value attribute is tied to a variable in your useState hook. Every time the user types, an onChange event fires, and we update that state variable. React then re-renders the component with the new value. This cycle—State → UI → Event → State—is the foundation of modern React development.

Building the Knowledge Base Entry Form

Let’s implement a form to capture a title and description for our Knowledge Base. We will use the useState hook to track the input values and a handler function to manage form submission.

JSX
import { useState } from CE9178">'@wordpress/element';
import { Button, TextControl, TextareaControl } from CE9178">'@wordpress/components';

const KnowledgeBaseForm = () => {
    // Initialize state for form fields
    const [title, setTitle] = useState(CE9178">'');
    const [content, setContent] = useState(CE9178">'');

    const handleSubmit = (event) => {
        // Prevent the browser from performing a standard page reload
        event.preventDefault();
        
        console.log(CE9178">'Submitting:', { title, content });
        
        // Reset the form after submission
        setTitle(CE9178">'');
        setContent(CE9178">'');
    };

    return (
        <form onSubmit={handleSubmit}>
            <TextControl
                label="Entry Title"
                value={title}
                onChange={(value) => setTitle(value)}
            />
            <TextareaControl
                label="Entry Content"
                value={content}
                onChange={(value) => setContent(value)}
            />
            <Button isPrimary type="submit">
                Add Entry
            </Button>
        </form>
    );
};

export default KnowledgeBaseForm;

Breaking Down the Implementation

  1. State Initialization: We use useState('') to initialize our fields as empty strings.
  2. Binding: The value prop of the TextControl is explicitly set to our state variable (title). If you remove the onChange handler, the input would become read-only because the state never updates to reflect the user's typing.
  3. Event Handling: The handleSubmit function includes event.preventDefault(). This is critical; without it, the browser will attempt a traditional GET/POST request, triggering a page refresh that destroys your React application's state.

Hands-on Exercise: Adding a Category Field

Now it's your turn to extend the form.

  1. Add a new TextControl to the form above specifically for a "Category" field.
  2. Add a new state variable called category using useState.
  3. Update the handleSubmit function to include the category in the console.log output.
  4. Verify that the input clears after you click the "Add Entry" button.

Common Pitfalls

When building Forms with Controlled components, developers often trip over these three issues:

  • Forgetting event.preventDefault(): As mentioned, this is the #1 cause of "why does my page reload when I submit?". Always include it as the first line in your submit handler.
  • Missing onChange handlers: If you define a value prop on an input but omit the onChange handler, the input will appear "stuck" and the user won't be able to type anything.
  • Over-complicating State: For simple forms, individual useState calls are fine. However, if your form grows to 10+ fields, consider using a single state object with useState({ title: '', content: '', category: '' }) and a single change handler that updates the object using the spread operator: setFormData({ ...formData, [name]: value }).

By mastering these patterns, you ensure that your plugin's data layer remains consistent with the UI. In the next lesson, we will connect this form to our REST API to persist data to the database.

Up next: Implementing CRUD in the Admin UI

Previous lessonWorking with @wordpress/componentsNext lesson Implementing CRUD in the Admin UI
Back to Blog

Similar Posts

WordPressJune 25, 20263 min read

Scaffolding the React Admin Dashboard for WordPress Plugins

Learn how to scaffold your React admin dashboard by registering a WordPress menu, creating a root container, and mounting your application into the DOM.

Read more
WordPressReact DevelopmentJune 25, 20263 min read

Building the Knowledge Base Service Layer | WordPress REST API

Part of the course

Intermediate WordPress Plugins: REST API & React Admin

intermediate · Lesson 15 of 45

  1. 1

    Setting up the WordPress Development Environment

    3 min
  2. 2

    Introduction to @wordpress/scripts

    3 min
  3. 3

    Configuring ESLint and Prettier

    3 min

Stop scattering fetch calls across your React components. Learn to build a clean service layer to centralize API logic and simplify your WordPress plugin.

Read more
WordPressJune 25, 20263 min read

Handling Asynchronous State in React for WordPress Plugins

Master React state management for asynchronous API requests. Learn to implement loading, error, and success states to create a seamless WordPress admin UI.

Read more
4

Localizing Data for JavaScript

3 min
  • 5

    Anatomy of a REST API Endpoint

    3 min
  • 6

    Implementing REST API Permission Callbacks

    3 min
  • 7

    Handling GET Requests in REST API

    3 min
  • 8

    Validating and Sanitizing API Arguments

    4 min
  • 9

    Creating POST Endpoints for Data Submission

    3 min
  • 10

    Updating Existing API Resources

    3 min
  • 11

    Handling Asynchronous State in React

    3 min
  • 12

    Building the Knowledge Base Service Layer

    3 min
  • 13

    Scaffolding the React Admin Dashboard

    3 min
  • 14

    Working with @wordpress/components

    3 min
  • 15

    Creating a React Form for Submissions

    3 min
  • 16

    Implementing CRUD in the Admin UI

    3 min
  • 17

    Understanding WordPress Data Store Architecture

    4 min
  • 18

    Registering a Custom Data Store

    3 min
  • 19

    Writing Selectors for Data Access

    3 min
  • 20

    Defining Actions and Reducers

    Coming soon
  • 21

    Implementing Resolvers for Data Fetching

    Coming soon
  • 22

    Optimizing Performance with Selectors

    Coming soon
  • 23

    Handling Complex State Dependencies

    Coming soon
  • 24

    Implementing Nonce Verification

    Coming soon
  • 25

    Advanced Sanitization Techniques

    Coming soon
  • 26

    Input Validation and Error Handling

    Coming soon
  • 27

    Protecting Admin Screens

    Coming soon
  • 28

    Production Build Pipeline

    Coming soon
  • 29

    Debugging React in the WordPress Admin

    Coming soon
  • 30

    Building Search and Filter Functionality

    Coming soon
  • 31

    Internationalization in React

    Coming soon
  • 32

    Managing File Uploads via REST API

    Coming soon
  • 33

    Optimizing API Response Times

    Coming soon
  • 34

    Working with Date and Time in React

    Coming soon
  • 35

    Implementing Drag-and-Drop Sorting

    Coming soon
  • 36

    Creating Custom Hooks for API Logic

    Coming soon
  • 37

    Integrating with Gutenberg Blocks

    Coming soon
  • 38

    Handling Conflict Resolution

    Coming soon
  • 39

    Building a Modal Confirmation System

    Coming soon
  • 40

    Implementing Activity Logging

    Coming soon
  • 41

    Using Webpack Aliases

    Coming soon
  • 42

    Unit Testing API Endpoints

    Coming soon
  • 43

    Unit Testing React Components

    Coming soon
  • 44

    Handling Large Datasets with GraphQL

    Coming soon
  • 45

    Implementing Real-time Updates with Web

    Coming soon
  • View full course