WP REST API Custom Endpoint with EDITABLE method not returning values
-
So far I have been having fun with the REST API while at the same time running into a couple of frustrations.
So in this specific use case, I have a form on the frontend trying to pass data to a custom endpoint once user submits via the javascript Fetch API.
Javascript
fetch( this.route + '?account_info=' + account_changes, { method: 'POST' } ) .then( function( fetch_response ) { // Success if( response.ok ) { console.log( fetch_response ); } // Error else { console.log( 'Response Error', response ); } } ) .then( function( wp_response ) { console.log( wp_response ); } );Here I am passing all of the form data as a javascript object assigned to the
account_changesvariable. Thefetch_responseis showing up fine, giving me a 200 status which means that the endpoint is available, and I am assuming my issue has to do on the WordPress end of things.PHP
class API extends \WP_REST_Controller { /** * Register the routes for the objects of the controller. */ public function register_routes() { register_rest_route( Custom_Class::$rest_namespace, '/users', [ [ 'methods' => WP_REST_Server::EDITABLE, 'callback' => [ $this, 'update_user' ], 'show_in_index' => false, 'args' => [], ] ] ); } /** * Update user across all sites */ public function update_user( $request ) { return 'heeey'; } }Here is the relevant code in question. First, don’t worry about
Custom_Class::$rest_namespaceplaceholder. I just inserted it for here. The actual variable that goes here is working fine. From the docs I see thatWP_REST_Server::EDITABLEshould be work with POST requests to the endpoint and run the callback, but I can’t get it to work. In theory thewp_responsein the fetch call should be showing the ‘heeey’ returned by the callback function, but instead it is showing up as undefined.I’ve tried a bunch of different variations with no success. I’m also not sure how authentication is supposed to work here, but figured if that was the issue then it would be showing up within the
server_response.Anyway hopefully this is a good place to get some advice. Thanks.
The topic ‘WP REST API Custom Endpoint with EDITABLE method not returning values’ is closed to new replies.