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?

2 Answers
2

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.

Tags:

Leave a Reply

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