I'm currently developing a plugin that uses ajax functionality.

Problem: The callback isn't fired = no data gets processed.

if ( ! is_admin() )
    add_action( 'after_setup_theme', array( 'wpseAjaxClass', 'init' ), 10 );
  • Last time I had this issue was due to the callback function not actually being accessible during the AJAX request. For example, I buried the call to instantiate the class under a function like is_single(). It was called when the page was being viewed, but not when the AJAX request was made. I see that one of your comments says the code is part of the class. Any chance you can show the rest of the class? – tollmanz Feb 6 '12 at 5:17
  • @tollmanz Thanks for the comment. jQuery mobile loads everything with one request, so basically it's like a js-slider/-carousel. The my_action_cb() fn itself isn't hidden behind something. Just sits right in the middle of the class. As shown in the, the enqueue happens right in the __construct() of the class. I added a screenshot from the console. Please see Edit. Any other ideas? – kaiser Feb 6 '12 at 5:44
  • Sorry to harp on this, but clearly the script cannot find the function. How is the class itself instantiated? When I have these issues, it's usually something little that I've missed. I'd love to look over the code to see if I could spot something. – tollmanz Feb 6 '12 at 5:58
  • Ok :) Here's a pastebin. Tell me if the link isn't working. (And thanks for taking the time). – kaiser Feb 6 '12 at 6:00
  • 1
    To be more clear, I created this revised pastebin based on your pastebin: pastebin.com/r0Ud14aB – tollmanz Feb 6 '12 at 6:11

The answer was as stupid as simple: I wrapped the init for the class in a ! is_admin() call. This successfully prevented the callback from beeing fired.

Rule for AJAX loading

  • Load it in public
  • Don't hide it behind a ! is_admin() call.

The 2nd line from admin-ajax.php

define( 'WP_ADMIN', true );
| improve this answer | |
  • 2
    I knew it was going to be something like this. Whenever I get the dreaded "0" response with WP AJAX, I instantly think that my function is inaccessible! Congrats on finding the issue and getting your life back ;) – tollmanz Feb 7 '12 at 3:43
  • :) Thanks for this comment. I'm currently more than angry about this issue and writing on wp-hackers :P – kaiser Feb 7 '12 at 3:48
  • Also - don't pass a parameter called "action" inside your ajax callback routine - unless you want to seriously mess your own mind up! – Q Studio Sep 11 '17 at 14:40
  • @QStudio mind to clarify in an answer? – kaiser Sep 11 '17 at 15:20
  • 1
    @QStudio haha! No worries. Happens to the best! – kaiser Sep 11 '17 at 16:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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