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
X-Debug-Ajax-1:File "C:\...\wp-content\themes\bp\functions.php" was called on an AJAX request.
X-Debug-Ajax-2:Function "t5_debug_test" was called and the user is not logged in.
X-Frame-Options:SAMEORIGIN
X-Powered-By:PHP/5.4.7
X-Robots-Tag:noindex
I don't think its an ajax issue, thinking just the action hook not firing.process_reservation()
is public? Just guessing here.