Custom REST Endpoint In Plugin
-
I am working on an application that serves an Angular app in a WP plugin. The flow works like so:
-User answers questions in a brief survey on the Angular front-end, which makes a POST request to the WP custom POST endpoint with the survey answers.
-The callback function attached to the WP POST endpoint takes those answers, filters them (the plugin is essentially a Woocommerce product filter), and returns a filtered product list.
-The Angular app makes a GET request to the WP endpoint, and renders that filtered product list.
I’m doing something wrong, because all the parts are working individually: The Angular HTTP requests, filtering logic, and custom endpoints (when tested via Insomnia or with hard-coded arguments) are doing what they’re supposed to when tested in isolation, but when I test the app out in staging, the render just silently fails.
I’m pretty new to WordPress, but I’ve pored over the REST API docs, and they left me with a LOT of questions, namely:
-How do I test this setup and ensure that the proper request data is indeed reaching the endpoint?
-Am I doing this correctly? Should I have separate POST and GET logic, or is just having the POST more than enough? My code looks like this:
add_action('rest_api_init', function() { register_rest_route('awe/v1', '/awesomeproducts', array( 'methods' => 'POST', 'callback' => 'get_awesome_params', 'args' => array(), 'permission_callback' => function () { return true; } )); }); function get_awesome_params( WP_REST_Request $request ) { $parameters = $request->get_params(); // Very long filtering logic takes the $parameters and turns it // into $finalProducts, an array of objects return new WP_REST_Response($final_products, 200); }I’ve spent a lot of time looking for ansers on here, Stack Overflow, WordPress Stack Exchange, etc. Any help would be greatly appreciated.
The topic ‘Custom REST Endpoint In Plugin’ is closed to new replies.