0

I would like to delete a row from a table in the database using a custom endpoint via the API. Using an example in the WordPress REST API Handbook, I put together the following code and placed it in my functions.php file:

    function delete_custom_row( $id ) {
    global $wpdb;                           

    return $wpdb->delete(
        'xyz_custom_table',         
        ['id' => $id],                  
        ['%d'],                         
    );
} 

    add_action( 'rest_api_init', function () {
        register_rest_route( 'lic-api/v1', '/deletecustomrow/(?P<id>\d+)', array(
        'methods' => 'POST',
        'callback' => 'delete_custom_row'
        ) );
    } );

I know that I have a row in the table with a column 'id' of 3. So when I try to run the code using the endpoint URL of:

/wp-json/lic-api/v1/deletecustomrow/3

The row is not deleted and I get the following error code:

{"code":"rest_no_route","message":"No route was found matching the URL and request method.","data":{"status":404}}

Any help would be greatly appreciated.

11
  • That's not how parameters are passed to the callback function. Your function is expecting $id as a single value, but you need to get the parameters from within the data that's passed to the function. See the example from the docs: developer.wordpress.org/rest-api/extending-the-rest-api/… Sep 30, 2022 at 13:43
  • You should also always set a permission_callback, particularly in your case where your callback is deleting something from the database. As for the 404 error, try visiting /wp-json/lic-api/v1 and see if your endpoint is in the list shown on that page - if not, or if that page itself shows a 404 error, then that means your endpoint is not actually registered.
    – Sally CJ
    Sep 30, 2022 at 16:33
  • Thanks @Sally I did visit /wp-json/lic-api/v1 and it did return my endpoint in the list. So I know the endpoint is registered. Sep 30, 2022 at 19:40
  • Thanks @Jacob. I looked at the example in your link. I am not sure I understand the issue with the parameters. Do you think my function is incorrectly formatted? Sep 30, 2022 at 19:51
  • @JohnKeidel In that case, then how did you actually make your REST API request? Did you use custom JS (like fetch()) or PHP, and if so, what's your code? Are you aware of the differences between GET and POST requests, and that your endpoint is only accepting POST request? Are you completely certain you used the POST request method? Try using cURL - curl -X POST https://your-domain/wp-json/lic-api/v1/deletecustomrow/3 and confirm if you still get the 404 error?
    – Sally CJ
    Oct 1, 2022 at 1:45

1 Answer 1

0

A few things to try... Confirm you are using POST and not GET when hitting the endpoint. Postman could be helpful here.

It appears you are testing your endpoint using Pretty Permalinks. Confirm you have Post Name permalink structure setup. This might be the solution to your problem.

If that works however you are wanting to use Plain Permalinks format add the rest_route query parameter.

Example: /wp-json/lic-api/v1/deletecustomrow/3 becomes /?rest_route=/lic-api/v1/deletecustomrow/3

4
  • Thank you for your help Liam. I tried all of your suggestions, but I am getting the same error returned. Sep 30, 2022 at 3:40
  • What PHP and WordPress version are you using? Are you on pretty or plain permalinks? Disable all plugins and try again to confirm there isn't an issue with a third party plugin. Sep 30, 2022 at 13:57
  • Thanks @Liam. WordPress version: 6.0.2, PHP version 8.0.23. My permalinks are set at "Post name". I disabled all plugins, then tried to access my endpoint, but I'm still getting the same error. Sep 30, 2022 at 20:10
  • How are you testing the post request? Oct 3, 2022 at 15:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.