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 10 of the Intermediate WordPress Plugins: REST API & React Admin course
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.

WordPressREST APIPHPDevelopmentBackendplugin-development

Previously in this course, we explored creating POST endpoints for data submission to add new entries to our Knowledge Base. Now, we expand our API's capabilities by implementing the logic required to modify existing resources.

In a RESTful architecture, the ability to update data is just as critical as the ability to create it. We achieve this by using the PUT and PATCH HTTP methods, which allow us to target specific resources via their unique identifiers.

Understanding PUT vs. PATCH

While both methods update resources, they serve different semantic purposes:

  • PUT: Represents a complete replacement of the resource. If you send a PUT request, you are expected to provide the full set of required data. If a field is missing, the API should ideally treat it as an empty value or an error.
  • PATCH: Represents a partial update. You only send the fields that need to be changed, and the server updates only those specific attributes.

When building WordPress plugins, we often use WP_REST_Request to handle these. While WordPress doesn't strictly enforce the structural difference between PUT and PATCH in the database layer, following these conventions makes your API predictable for other developers.

Implementing ID-based Routing

To update a resource, our endpoint must accept an ID parameter. We define this in our register_rest_route call by including a capture group in the route path.

PHP
register_rest_route( 'kb/v1', '/entry/(?P<id>\d+)', [
    'methods'  => [ 'PUT', 'PATCH' ],
    'callback' => 'kb_update_entry_handler',
    'permission_callback' => 'kb_check_permissions',
    'args' => [
        'id' => [
            'validate_callback' => 'is_numeric',
        ],
    ],
] );

By using (?P<id>\d+), we ensure the id is passed directly into our callback function as part of the $request object.

Worked Example: Updating an Entry

In our handler, we need to retrieve the post, verify it exists, and then perform the update using wp_update_post. We must also perform validating and sanitizing API arguments before committing changes to the database.

PHP
function kb_update_entry_handler( WP_REST_Request $request ) {
    $id = $request->get_param( 'id' );
    $post = get_post( $id );

    if ( ! $post || $post->post_type !== 'knowledge_base' ) {
        return new WP_Error( 'not_found', 'Entry not found', [ 'status' => 404 ] );
    }

    $args = [
        'ID'           => $id,
        'post_title'   => sanitize_text_field( $request->get_param( 'title' ) ),
        'post_content' => wp_kses_post( $request->get_param( 'content' ) ),
    ];

    #6A9955">// Remove null values for partial updates(PATCH behavior)
    $args = array_filter( $args, function( $value ) {
        return ! is_null( $value );
    } );

    $result = wp_update_post( $args, true );

    if ( is_wp_error( $result ) ) {
        return $result;
    }

    return new WP_REST_Response( [ 'success' => true, 'id' => $result ], 200 );
}

Hands-on Exercise

  1. Modify your existing kb_update_entry_handler to support both PUT and PATCH.
  2. Add a check to ensure that if a user attempts to update a post, they have the edit_post capability for that specific ID.
  3. Test your endpoint using Postman or cURL by sending a PATCH request to /wp-json/kb/v1/entry/{id} with only the title field in the body. Ensure the content remains unchanged.

Common Pitfalls

  • Ignoring Permission Callbacks: Never assume that because a user is logged in, they can edit every post. Always use current_user_can( 'edit_post', $id ) inside your handler, even if you have a global permission callback defined.
  • Overwriting Data: When implementing PATCH, avoid updating fields that weren't provided in the request. Using array_filter or checking $request->has_param() is essential to avoid overwriting existing data with null or empty strings.
  • Race Conditions: If two users edit the same resource simultaneously, the last one to save wins. For high-concurrency plugins, consider implementing ETag headers or "last modified" timestamps to prevent data loss.

Recap

Updating resources requires precise ID-based routing and careful handling of the request body. By distinguishing between the intent of PUT (full replacement) and PATCH (partial update), you create a robust API. Always sanitize your inputs and verify user permissions against specific post objects to keep your Knowledge Base secure.

Up next: We will begin managing the client-side experience by handling asynchronous state in React.

Previous lessonCreating POST Endpoints for Data SubmissionNext lesson Handling Asynchronous State in React
Back to Blog

Similar Posts

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.

Read more
WordPressJune 25, 20264 min read

Validating and Sanitizing API Arguments in WordPress REST API

Master API security by defining argument schemas in WordPress. Learn to validate and sanitize incoming REST API requests to ensure robust data integrity.

Part of the course

Intermediate WordPress Plugins: REST API & React Admin

intermediate · Lesson 10 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
WordPressJune 25, 20263 min read

Handling GET Requests in REST API: Retrieving Knowledge Base Data

Learn to map REST API callbacks to HTTP GET methods, return structured JSON responses, and format WordPress post data for your custom Knowledge Base plugin.

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

    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