I have an existing AJAX action that's already defined by the theme I'm using. The action deletes a search item request, and I'd like to use it as a direct link - like an Unsubscribe link - emailed to the user along with the search results...

Something like the following URL: https://domain.tld/wp-admin/admin-ajax.php?action=delete_saved_search_item=&search_item_id=17 but I get a 0 on the screen and a 400 Bad Request in the Headers.

Basically in the JS it's defined like this:

                    action: 'delete_saved_search_item',
                    search_item_id: search_item_id,
                function (response) {
                    response = JSON.parse(response);
                    if (response.success) {

so I suppose it expects a POST request, not a GET one... Is there any workaround to make this work? Like creating a page with a custom cURL that would imitate the POST form submission? Anyone done something like this? The good thing is I didn't notice any use of nonce, so I suppose this alone makes it easier to handle, doesn't it?

1 Answer 1


Use WordPress' generic POST/GET handler wp-admin/admin-post.php

Don't let the name confuse you - it accepts both GET/POST, and just like admin-ajax.php it accepts both authorised (admin) and non-authorised (public) requests.

function wpse_406199_custom_action() {
    // Do something!

    // Maybe redirect back to where they came from?
    wp_redirect( wp_get_referer() );

// Non-logged-in requests
add_action( 'admin_post_nopriv_wpse_406199_custom_action', 'wpse_406199_custom_action' );

// Logged-in requests
add_action( 'admin_post_wpse_406199_custom_action', 'wpse_406199_custom_action' );

And to get the URL:

$action_url = admin_url( 'admin-post.php?action=wpse_406199_custom_action' );
  • I will try it and report back (though it already looks like it'll do the job for my case). Thank you so much! :D
    – Faye D.
    May 30, 2022 at 1:22
  • I asked this question for one project, and I'll end up using it for another one that fits... I have one question though: The case I'll be using it takes place in the public side of website, but the user has to be logged in, in order to perform the action... So which of the two should I use? The nopriv or the other one?
    – Faye D.
    Jun 12, 2022 at 23:32
  • 1
    @FayeD. If you just want to handle logged-in users, then "the other one" (not nopriv) Jun 13, 2022 at 21:06
  • Thank you very much! :D
    – Faye D.
    Jun 13, 2022 at 22:47
  • 1
    @FayeD. Because you're probably still using the JS var ajaxurl that points to admin-ajax.php and not admin-post.php? Jun 14, 2022 at 23:26

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.