I asked this over at the WordPress side of Stackexchange, but ran out of answers that led me anywhere, so was hoping someone here could help. Sorry if this cross-post breaks a rule. The original was here. https://wordpress.stackexchange.com/questions/95075/jquery-ajax-call-not-executing-in-plugin
I’ve been working on a class for my free plugins that outputs a “Donate” form. The “No thanks” dismissal link hides the form and runs an AJAX call to update a WP Option keeping track of the dismissal.
I’m having issues getting the called PHP function to actually fire. I added the “success” function to output an echo that I added to the PHP function called by the AJAX (typically, the function has no output, only a WP Option update), but “data” is always 0 and nothing happens in the database.
I have several other AJAX calls in this plugin, all of which work, but this is the only one that is inside of a class. Earlier in development of the class, it worked, but I can’t figure out what I could’ve done to break it. I’ve even tried stripping it down to just a declaration of the AJAX registration and a function that outputs a test message with no success.
I’m guessing that WP isn’t actually able to use the function for some reason.
Here’s the pertinent part of my jQuery AJAX call
// Create the AJAX data
ajax_call_data = {
'action': 'dismiss_ppd_dmgr',
'nonce': $nonce
};
// Call the AJAX function to update the options
jQuery.ajax({
url: ajaxurl,
type: 'post',
data: ajax_call_data,
success: function(data) { alert( data ) }
});
In the class, I have an attribute ajax_call, which is set at class instantiation and I’m adding my call to WordPress like so. I’ve verified that $this->ajax_call is populated properly and also tried hardcoding it, instead of using the variable.
add_action( 'wp_ajax_' . $this->ajax_call , array( &$this , 'dismiss_form' ) );
$this->ajax_call outputs the “dismiss_ppd_dmgr”. I’ve echoed wp_filter and found wp_ajax_dismiss_ppd_dmgr registered pointing to dismiss_form() in my class. But the result is always 0 and nothing is updated in the database.
Things I’ve tried:
- with and without &
- hard-coded instead of with $this->ajax_call
- calling the add_action directly in the constructor as well as putting it inside of a function that’s called by an admin_init action
-
creating the AJAX add_action outside of the class as
add_action( 'wp_ajax_dismiss_ppd_dmgr' , array( $ppd_dmgr , 'dismiss_form' ) );
Does anyone have any ideas as to why WP doesn’t like my AJAX call?