• skustes

    (@skustes)


    I’ve been working on a class for a plugin that outputs a form with a dismissal link. The 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 three 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, registering the .js file, 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. The function is declared as public.

    Here’s the pertinent part of my jQuery AJAX call (I’ve used both ‘ajaxurl’ and redefined it when doing wp_localize_script as below…neither work):

    jQuery('#ppd_dismiss').click( function() {
    		// Hide the form
    		//jQuery('#ppd_wrap').hide();
    
    		jQuery.post( ppdajax.ajaxurl, {
    				action: 'dismiss_ppd_dmgr',
    				ppd_nonce: ppdajax.ppd_nonce
    			},
    			function( data ) {
    				alert( data );
    			}
    		);
    
    		return false;
    	} );

    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 used a debug script to output all wp_filter hooks and found my AJAX call registered:

    >>>>>	wp_ajax_dismiss_ppd_dmgr
    10	(object) ppd_dmgr -> dismiss_form (1)

    Other things I’ve tried:

    1. with and without &
    2. hard-coded instead of with $this->ajax_call
    3. 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
    4. 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?

Viewing 1 replies (of 1 total)
  • Thread Starter skustes

    (@skustes)

    Admins, sorry, I didn’t realize there was a Hacks sub-forum where this should be posted. Can you move this or should I repost there?

Viewing 1 replies (of 1 total)
  • The topic ‘jQuery AJAX call in plugin class not working’ is closed to new replies.