Learn to update existing WordPress resources using REST API PUT and PATCH methods. Master ID-based routing and secure data modification for your plugins.
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.
While both methods update resources, they serve different semantic purposes:
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.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.
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.
PHPregister_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.
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.
PHPfunction 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 ); }
kb_update_entry_handler to support both PUT and PATCH.edit_post capability for that specific ID.PATCH request to /wp-json/kb/v1/entry/{id} with only the title field in the body. Ensure the content remains unchanged.current_user_can( 'edit_post', $id ) inside your handler, even if you have a global permission callback defined.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.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.
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 moreMaster API security by defining argument schemas in WordPress. Learn to validate and sanitize incoming REST API requests to ensure robust data integrity.
Updating Existing API Resources
Understanding WordPress Data Store Architecture
Registering a Custom Data Store
Writing Selectors for Data Access
Defining Actions and Reducers
Implementing Resolvers for Data Fetching
Optimizing Performance with Selectors
Handling Complex State Dependencies
Implementing Nonce Verification
Advanced Sanitization Techniques
Input Validation and Error Handling
Protecting Admin Screens
Production Build Pipeline
Debugging React in the WordPress Admin
Building Search and Filter Functionality
Internationalization in React
Managing File Uploads via REST API
Optimizing API Response Times
Working with Date and Time in React
Implementing Drag-and-Drop Sorting
Creating Custom Hooks for API Logic
Integrating with Gutenberg Blocks
Handling Conflict Resolution
Building a Modal Confirmation System
Implementing Activity Logging
Using Webpack Aliases
Unit Testing API Endpoints
Unit Testing React Components
Handling Large Datasets with GraphQL
Implementing Real-time Updates with Web