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 9 of the Intermediate WordPress Plugins: REST API & React Admin course
WordPressJune 25, 20263 min read

Creating POST Endpoints for Data Submission in WordPress REST API

Master the WordPress REST API by creating POST endpoints. Learn to extract request bodies, sanitize data, and insert new posts into the database securely.

WordPressREST APIPHPDevelopmentAPIBackendplugin-development

Previously in this course, we explored handling GET requests in REST API to retrieve our Knowledge Base entries. Now that we can fetch data, it's time to allow our React admin interface to push new content to the server.

In this lesson, we will implement the "Create" part of our CRUD operations by building a custom POST endpoint.

Mapping Callbacks to HTTP POST

When you register a route using register_rest_route, you define the allowed HTTP methods in the $args array. While a GET request retrieves data, a POST request is intended for creating new resources.

To handle POST requests, we set the methods key to WP_REST_Server::CREATABLE. This constant maps to the POST verb.

PHP
register_rest_route( 'kb/v1', '/entry', [
    'methods'  => WP_REST_Server::CREATABLE,
    'callback' => 'kb_handle_create_entry',
    'permission_callback' => 'kb_user_can_create',
] );

If you need a refresher on setting up the route structure, see anatomy of a REST API endpoint. Additionally, always ensure you have implemented proper REST API permission callbacks to verify that only authorized users can submit new entries.

Extracting Request Body Data

Unlike GET requests where data usually arrives via URL parameters, POST requests carry data in the request body, typically as JSON. In your callback function, the WP_REST_Request object provides a helper method called get_json_params() or simply get_param() to access this payload.

Before processing, we must validate and sanitize the input. I highly recommend using the schema validation features covered in validating and sanitizing API arguments.

PHP
function kb_handle_create_entry( WP_REST_Request $request ) {
    #6A9955">// Extract parameters from the request body
    $title   = sanitize_text_field( $request->get_param( 'title' ) );
    $content = sanitize_textarea_field( $request->get_param( 'content' ) );

    if ( empty( $title ) ) {
        return new WP_Error( 'missing_title', 'The title is required.', [ 'status' => 400 ] );
    }

    #6A9955">// Proceed to insertion...
}

Inserting Data into the Database

Once the data is cleaned, we use the standard WordPress function wp_insert_post. This function handles the complex logic of creating a row in the wp_posts table and assigning metadata.

Crucially, wp_insert_post returns the ID of the new post on success or a WP_Error object on failure. We must check this return value to provide a proper response to our React frontend.

PHP
function kb_handle_create_entry( WP_REST_Request $request ) {
    $title   = sanitize_text_field( $request->get_param( 'title' ) );
    $content = sanitize_textarea_field( $request->get_param( 'content' ) );

    $post_id = wp_insert_post( [
        'post_title'   => $title,
        'post_content' => $content,
        'post_type'    => 'knowledge_base',
        'post_status'  => 'publish',
    ] );

    if ( is_wp_error( $post_id ) ) {
        return new WP_REST_Response( [ 'message' => 'Failed to create post' ], 500 );
    }

    return new WP_REST_Response( [ 'id' => $post_id, 'message' => 'Entry created' ], 201 );
}

Hands-on Exercise

  1. Open your plugin's main REST registration file.
  2. Add a new route for POST /kb/v1/entry.
  3. Implement the callback to extract title and content.
  4. Use wp_insert_post to save the entry as a knowledge_base post type.
  5. Test your endpoint using Postman or curl. Send a JSON payload: {"title": "New Entry", "content": "Hello World"}. Verify that a new post appears in your WordPress admin dashboard.

Common Pitfalls

  • Missing JSON headers: When sending data from your React client, ensure your fetch request includes Content-Type: application/json. Without this, WordPress may not parse the request body correctly.
  • Forgetting to sanitize: Never trust input from the WP_REST_Request object. Always pass data through sanitize_text_field or appropriate HTML filters before it touches the database.
  • Ignoring return types: wp_insert_post can return 0 or a WP_Error. Always handle these cases to prevent silent failures or fatal errors.

Recap

We have successfully extended our API to handle data submission. By mapping our callback to WP_REST_Server::CREATABLE, extracting sanitized input, and utilizing wp_insert_post, we've built a robust foundation for our Knowledge Base entry creation. This pattern of REST API data insertion is the backbone of any interactive React-powered WordPress plugin.

Up next: We will explore how to handle updates to these resources using ID-based routing and HTTP PUT/PATCH methods.

Previous lessonValidating and Sanitizing API ArgumentsNext lesson Updating Existing API Resources
Back to Blog

Similar Posts

WordPressJune 25, 20263 min read

Updating Existing API Resources: REST API, PUT, and PATCH

Learn to update existing WordPress resources using REST API PUT and PATCH methods. Master ID-based routing and secure data modification for your plugins.

Read more
WordPressJune 25, 20263 min read

Handling AJAX Requests: A Guide to Asynchronous WordPress

Learn to handle AJAX requests in WordPress securely. Master wp_ajax hooks, JSON responses, and frontend communication to power your plugin's interactivity.

Part of the course

Intermediate WordPress Plugins: REST API & React Admin

intermediate · Lesson 9 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
Read more
Close-up of a vintage typewriter with a paper displaying 'WordPress', ideal for blogging and writing concepts.
WordPressJune 20, 20263 min read

Extending the WordPress REST API with Custom Endpoints

Extending the WordPress REST API with custom endpoints is the best way to decouple your frontend. Learn to build secure, scalable routes like a pro.

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

    Coming soon
  • 17

    Understanding WordPress Data Store Architecture

    Coming soon
  • 18

    Registering a Custom Data Store

    Coming soon
  • 19

    Writing Selectors for Data Access

    Coming soon
  • 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