3

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 );
11
  • 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
    Commented Feb 6, 2012 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
    Commented Feb 6, 2012 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
    Commented Feb 6, 2012 at 5:58
  • Ok :) Here's a pastebin. Tell me if the link isn't working. (And thanks for taking the time).
    – kaiser
    Commented Feb 6, 2012 at 6:00
  • 1
    To be more clear, I created this revised pastebin based on your pastebin: pastebin.com/r0Ud14aB
    – tollmanz
    Commented Feb 6, 2012 at 6:11

1 Answer 1

3

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 );
6
  • 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
    Commented Feb 7, 2012 at 3:43
  • :) Thanks for this comment. I'm currently more than angry about this issue and writing on wp-hackers :P
    – kaiser
    Commented Feb 7, 2012 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
    Commented Sep 11, 2017 at 14:40
  • @QStudio mind to clarify in an answer?
    – kaiser
    Commented Sep 11, 2017 at 15:20
  • 1
    @QStudio haha! No worries. Happens to the best!
    – kaiser
    Commented Sep 11, 2017 at 16:36

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.