📝 The Ajax Handler That Only Worked While Logged In
admin-ajax.php routes every incoming Ajax request to an action hook based on the logged-in status of whoever’s making the request: a logged-in user’s request fires wp_ajax_{action}, while a logged-out visitor’s identical request fires wp_ajax_nopriv_{action} instead – a completely separate hook that requires its own explicit add_action call. A developer who registers only the wp_ajax_ version, almost always because development and testing happened while logged in as an administrator, ships code that works perfectly in every manual test and then returns the literal string ‘0’ for every single logged-out visitor, because WordPress’s default admin-ajax behavior is to die with that output whenever no matching action hook is registered for the current request’s privilege level.
🔎 The Problem
// Registered only for logged-in users - works perfectly every
// time it's tested, because testing happened while logged in as
// an administrator.
add_action('wp_ajax_submit_newsletter', 'handle_newsletter_signup');
// Missing entirely: the hook that fires for a LOGGED-OUT visitor
// making the exact same request.
// add_action('wp_ajax_nopriv_submit_newsletter', 'handle_newsletter_signup');
function handle_newsletter_signup() {
// ... signup logic ...
wp_send_json_success(['message' => 'Subscribed!']);
}
// JS makes an identical request regardless of login state:
fetch(ajaxurl, {
method: 'POST',
body: new URLSearchParams({ action: 'submit_newsletter', email })
});
// Logged-in admin testing it: works.
// Any real, logged-out visitor on the live site: the response
// body is the literal text "0" - no JSON, no error, nothing
// matching what handle_newsletter_signup() was supposed to return.
✅ Fix: Register Both Hooks Whenever Logged-Out Visitors Need Access
- Register the matching wp_ajax_nopriv_{action} hook alongside wp_ajax_{action} for every Ajax action that logged-out visitors are meant to be able to trigger, pointing both at the same handler function when the logic doesn’t actually need to differ.
- Test every Ajax endpoint specifically in a logged-out browser session or private window, not only while logged in as an administrator, since the two privilege levels genuinely fire different hooks and a logged-in-only test will never catch this.
- Treat a raw ‘0’ response body from an Ajax request as a specific, recognizable signal that no matching action hook fired for the current request’s privilege level, rather than a generic unexplained failure to debug from scratch each time.
⚠️ Why This Is Easy to Miss
- Development and QA both typically happen while logged into WordPress as an administrator, so wp_ajax_submit_newsletter fires correctly every single time anyone on the team actually tests the feature.
- The literal response ‘0’ gives no indication of what actually went wrong – no error message, no stack trace, nothing pointing specifically at a missing hook registration for logged-out visitors.
admin-ajax.php doesn’t route a logged-in user’s request and a logged-out visitor’s identical request through the same hook – registering only one of the two silently leaves out half your actual audience.
