I am wondering why I can't read the updated value of $_TEST. It seems that $_TEST is being reset with each Ajax call.

// functions.php $_TEST = 0; // the variable I want to update with each Ajax call if ( is_admin() ) { add_action( 'wp_ajax_get_global_val', 'get_global_val'); add_action( 'wp_ajax_nopriv_get_global_val', 'get_global_val'); } function get_global_val() // my Ajax function { global $_TEST; // the value I want to update echo $_TEST; // displaying 0 instead of 1 $_TEST = 1; // update my variable }


Each time you're making an ajax call, $_TEST = 0; is being evaluated again. You use the options API to manipulate your variable.

function get_global_val() 
   $count = get_option( 'mycount' );
   $count++; //or whatever you want to do with it
   update_option( 'mycount', $count );
   die("New value is $count");
  • 1
    @P-S each http request is entirely separate with no data persisted between. the moment the last byte of your page request left the server, that request was gone from memory, your new ajax request, and each subsequent request, has no access to anything you do in php unless you persist it by writing to database or file. – Milo Dec 3 '14 at 15:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.