Skip to content
  • Home
  • About
  • No Access
  • Privacy Policy
Bits of .NET
Bits of .NET
  • Asp.Net Core
  • C#
  • SQL
  • JavaScript
  • CSS
  • About
  • ErcanOPAK.com
  • No Access
  • Privacy Policy

Bits of .NET

Daily micro-tips for C#, SQL, performance, and scalable backend engineering.

  • Asp.Net Core
  • C#
  • SQL
  • JavaScript
  • CSS
  • About
  • ErcanOPAK.com
  • No Access
  • Privacy Policy
Wordpress

WordPress: Fix an admin-ajax Request That Returns 0 for Logged-Out Visitors Because Only wp_ajax_, Not wp_ajax_nopriv_, Was Registered

- 10.10.26 - ErcanOPAK

📝 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.

— WordPress Admin

Related posts:

WordPress: Master Database Health Check for Peak Performance

WordPress: Master Database Health Check for Peak Performance

WordPress: Fix oEmbed Previews Breaking in the Block Editor Because a Strict Content Security Policy...

WordPress: Fix oEmbed Previews Breaking in the Block Editor Because a Strict Content Security Policy...

WordPress: Fix REST API Requests Returning 401 Because a Security Plugin Disabled Application Passwo...

WordPress: Fix REST API Requests Returning 401 Because a Security Plugin Disabled Application Passwo...

Views: 5

Post navigation

WordPress: Fix a Custom Taxonomy Archive That 404s Only After You Change the Taxonomy’s Rewrite Slug
C#: Fix a CompareExchange Retry Loop That Spins Forever Because the Comparand Was Captured Before the Loop Started

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

November 2026
M T W T F S S
 1
2345678
9101112131415
16171819202122
23242526272829
30  
« Oct    

Most Viewed Posts

  • How to make theater mode the default for Youtube (1,055)
  • Get the User Name and Domain Name from an Email Address in SQL (980)
  • How to add default value for Entity Framework migrations for DateTime and Bool (977)
  • Get the First and Last Word from a String or Sentence in SQL (857)
  • How to select distinct rows in a datatable in C# (853)
  • How to enable, disable and check if Service Broker is enabled on a database in SQL Server (653)
  • Add Constraint to SQL Table to ensure email contains @ (608)
  • Average of all values in a column that are not zero in SQL (570)
  • How to use Map Mode for Vertical Scroll Mode in Visual Studio (546)
  • Find numbers with more than two decimal places in SQL (475)

Recent Posts

  • C#: Fix a ConditionalWeakTable Entry Vanishing Too Soon
  • Docker: Fix a Stale COPY Layer After Editing .dockerignore
  • Photoshop: Fix a Pattern Overlay Scaling Wrong on a High-DPI File
  • SQL Server: Fix a Computed Column Index Skipped by Implicit Cast
  • HTML5: Fix a required Input Inside a Hidden Tab Blocking Submit
  • SQL Server: Fix ROW_NUMBER Ties Reordering Between Identical Runs
  • C#: Fix an Enum HasFlag Returning True for the Zero Value
  • WordPress: Fix Featured Images Vanishing After a CDN Migration
  • Photoshop: Fix Content-Aware Fill Smearing a Repeating Pattern
  • Asp.Net Core: Fix Antiforgery Tokens Rejected Behind a Reverse Proxy

Most Viewed Posts

  • How to make theater mode the default for Youtube (1,055)
  • Get the User Name and Domain Name from an Email Address in SQL (980)
  • How to add default value for Entity Framework migrations for DateTime and Bool (977)
  • Get the First and Last Word from a String or Sentence in SQL (857)
  • How to select distinct rows in a datatable in C# (853)

Recent Posts

  • C#: Fix a ConditionalWeakTable Entry Vanishing Too Soon
  • Docker: Fix a Stale COPY Layer After Editing .dockerignore
  • Photoshop: Fix a Pattern Overlay Scaling Wrong on a High-DPI File
  • SQL Server: Fix a Computed Column Index Skipped by Implicit Cast
  • HTML5: Fix a required Input Inside a Hidden Tab Blocking Submit

Social

  • ErcanOPAK.com
  • GoodReads
  • LetterBoxD
  • Linkedin
  • The Blog
  • Twitter
© 2026 Bits of .NET | Built with Xblog Plus free WordPress theme by wpthemespace.com