Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm having the exact same problem described in this post: add_action('wp_ajax_[action name]', myfunction) problem

I set add_action('wp_ajax_my_function_call','my_ajax_function'). If I print_r($wp_filter) immediately after this line, my action is in the $wp_filter array.

However, by the time I do my jQuery.post call to admin-ajax.php, my action has disappeared from $wp_filter. I verified this by adding this to line 354 of wp-includes/plugin.php:

if($tag=='wp_ajax_my_function_call') print_r($wp_filter);

According to user1567 in the above-cited post, "the problem was the loading order and the location of the add_action calls." Unfortunately this hasn't helped me track down the problem. I tried setting the priority of the add_action to 99 and to 1. Neither helped.

Any other ideas on what might be causing this situation, or at an explanation of why, in general, an action that was definitely in $wp_filter at one point would inexplicably disappear from it without even being triggered?

share|improve this question
    
Try placing your 'add_action' in the themes functions.php see if it make any difference and also make sure that you add action parameter to your ajax post. But without seeing your full setup its hard to tell. –  Bainternet Apr 14 '11 at 10:00
1  
What file/files are you putting this code in? –  John P Bloch Apr 14 '11 at 11:43
add comment

2 Answers

There are only three explanations for why this isn't working:

First, something could be unregistering your action. That doesn't seem very likely, though.

Second, your code never runs in the ajax initialization.

Third, the wp_ajax_my_function_call hook never runs.

I wouldn't worry about the possibility of the first.

As far as the second, just make sure your code is run before wp_loaded, as anything after that (i.e. template_redirect, wp, pre_get_posts) isn't run in an ajax load.

For the third, just make sure you're posting your ajax request to wp-admin/admin-ajax.php.

Also, if you haven't already, I'd suggest giving a quick read to admin-ajax.php and wp-settings.php.

share|improve this answer
    
It's definitely posting to admin-ajax.php properly. That line of code that I put in at 354 in plugin.php gets fired off when admin-ajax.php runs the do_action that I specified. Problem is, the action gets killed at line 371 because for some reason, at the time of the ajax call, it's no longer in $wp_filter. That's why I'm thinking your first explanation, that something is unregistering the action, seems most likely at this point. Any ideas? –  trumpetblast Apr 14 '11 at 15:03
    
Try changing your action hook name. –  John P Bloch Apr 14 '11 at 16:29
    
I'm afraid that won't do it. Obviously the action hook name I gave here was generic. It's quite specific in the actual code, so there's no way that's causing the conflict. I'll run through the entire plugin.php file, track down the bug and post back the findings here. Thanks for your thoughts. –  trumpetblast Apr 14 '11 at 19:38
    
Don't waste your time looking through plugin.php. It works. I've been doing AJAX development with no problems since 2.9 (that's when I started, not when any problems stopped). What would be more helpful is pasting your code. That's the unknown variable here. –  John P Bloch Apr 14 '11 at 21:02
    
Sorry. I didn't mean to imply that plugin.php has a bug. I'm sure it does not. But ploughing through it would certainly reveal where the problem in my code is, and would be a good thing to get more familiar with anyway. I agree that no one can really help any further at this point without pasting up the code, but it's a rather complex theme and, honestly, I think tracking the problem down myself would be more fruitful at this point. Thank you for the offer, though. –  trumpetblast Apr 15 '11 at 1:50
add comment

I just ran into a similar problem in one of my plugins (and stumbled on this question). In my case, I tracked it down to a conditional statement in my admin_init function. In attempt to keep from loading my plugin's code (and javascript and CSS) on every admin page, I had included the following check:

$uri = isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : NULL ;
if ($uri && (strpos($uri,'post.php') OR strpos($uri,'post-new.php')) ) { 
   add_action( 'wp_ajax_my_plugin', 'my_plugin_callback' );
   // load scripts etc.
}

This works fine to add my action to $wp_filter when the post-new.php page loads. But when the AJAX script posts its data to admin-ajax.php, of course it runs the admin_init function again (since it's run for every admin page load) and at that point the URI doesn't include 'post.php' or 'post-new.php'.

I moved the wp_ajax add_action statement outside of my URI test, and now it works great.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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