At the moment I have an ajax that works (I get a success [200] response), but having an issue with the action hooks on the response. The JSON object is not coming back, instead I’m getting a 0.
I have the die(); after my return so I’m thinking the issue is the hook not working.
I’ve tried multiple methods within the Class constructor but I’m not sure if this approach is right, I’ve done it before with a plugin I made, but this is within the theme.
Form.php
(included in functions.php with add_action('after_setup_theme')
public function __construct(){
// Test #1
add_action( 'wp_ajax_nopriv_process_reservation', array( &$this, 'process_reservation' ) );
// Test #2
add_action( 'wp_ajax_process_reservation', &$this->process_reservation ); //with and without '&' before $this
// Test #3 (this is incorrect)
add_action( 'wp_ajax_nopriv_process_reservation', $this->process_reservation );
//If I wrap this with **add_action('init',function(){... *** then it does not load
wp_enqueue_script('ajax_script',THEME_MODULES_URL.'/Reservations/form.js',array('jquery'),TRUE);
wp_localize_script( 'ajax_script', 'myAjax', array(
'ajaxurl' => admin_url( 'admin-ajax.php' ), //don't change this
'itemNonce' => wp_create_nonce("ajax_nonce"), //don't change this
));
}
Just in case it is needed here is the test of my callback function at the moment as well
private function process_reservation(){
check_ajax_referer( 'process_reservation_nonce', 'nonce' );
if( true )
wp_send_json_success( 'ok' );
else
wp_send_json_error( array( 'error' => $custom_error ) );
}
Form data in the XHR console shows both the action and nounce passed
action:process_reservation
ajax_nonce:6f155a1e17
I’ve done enough Ajax to know what to expect so I’m pretty sure its a hooking issue here, perhaps something with the theme scope that I don’t understand, either way any suggestions or help from the community would be great! Thanks in advance
The errors I’ve spotted in your code:
One was pointed by @helgatheviking in a comment: the Ajax callback has to be a
public
method, notprivate
.Not sure how are you initializing this class, but
wp_enqueue_script
&_style
(in singular) has to be encapsulated inside awp_enqueue_scripts
hook (WP_DEBUG dumps a notice).You can drop the
&
from$this
, that’s PHP 4. Follows a working example:test.js
The issue was that in my functions.php I was loading a file (reservations.php) using the
after_setup_theme
hookAnd then from within the file that was loaded I was requiring my class files with the
init
hook. I thought this would be ok sinceinit
loads afterafter_setup_theme
, it looked like this”The problem as mentioned in @brasofilo thread was that any action hook I attempted once in the class file would no longer work, because at this point the
init
had already been triggered.To fix this I simple removed the loaded_classes() as shown above, and instead just
required_once
within the file itself (which is loadedafter_setup_theme
). Then @brasofilo answer worked like a charm so make sure to +1 his response.Please check your ajax call
if you use make sure you put dataType : “json”
or use
$.getJSON()
function …Hope this fix the issue.