Retrieve User Email From WordPress and Pass to javascript for API call

I have, in functions.php added following code:

    function wp12311_enqueue_scripts() {
     wp_enqueue_script( 'wp12311-scripts', get_stylesheet_directory_uri() . '/test/scripts.js', array( 'jquery' ), false, true );
     wp_localize_script( 'wp12311-scripts', 'userDetails', array(
         'current_user' => wp_get_current_user(),
     ) ) ;
}
add_action( 'wp_enqueue_scripts', 'wp12311_enqueue_scripts' );

Now, in the file ‘/test/scripts.js’ , I have the following code:

    var request = new XMLHttpRequest();



request.open('GET', 'http://XXXXXXX.com/getData?email=abc@gmail.com', true);
request.onload = function () {
 // Begin accessing JSON data here
 var data = JSON.parse(this.response);

 var userEmail = userDetails.current_user.username.data.user_email;
 console.log("display is " + userEmail);

 if (request.status >= 200 && request.status < 400){

   google.charts.load('current', {'packages':['corechart']});
   google.charts.setOnLoadCallback(function() { drawChart(data); });

 } else {
   console.log('error')
 }
}

request.send()

I get the error when the .js file loads:

Uncaught ReferenceError: userDetails is not defined at XMLHttpRequest.request.onload

What am I doing wrong? Please help.

71 thoughts on “Retrieve User Email From WordPress and Pass to javascript for API call”

  1. therapie cognitivo comportementale mulhouse therapie de couple pour se separer , pharmacie france argenteuil medicaments reflux gastrique , Comprar Sertralina genГ©rico, Comprar Sertralina medicamento. Comprar Sertralina sin receta Comprar Sertralina genГ©rico [url=https://kit.co/madzasu/sertralina-en-farmacia-espana-comprar-sertralina-generico#]Comprar Sertralina sin receta[/url] comprime arrow generique Comprar Ceftin sin receta, Comprar Ceftin medicamento. Comprar Ceftin sin receta Comprar Ceftin genГ©rico [url=https://kit.co/scalefsa/comprar-ceftin-sin-receta-ceftin-venta-online#]Ceftin en farmacia Chile[/url] , pharmacie de garde aujourd’hui toulon pharmacie ouverte sarcelles , pharmacie becirovski beauvais. pharmacie bordeaux test pcr pharmacie annesse et beaulieu Comprar Benicar genГ©rico, Comprar Benicar medicamento. Benicar en farmacia Guatemala Comprar Benicar genГ©rico [url=https://kit.co/littbasing/comprar-benicar-generico-comprar-benicar-sin-receta#]Comprar Benicar medicamento[/url] , pharmacie de garde nanterre. pharmacie uckange, pharmacie de garde zone agoe medicaments antidepresseurs . therapies breves definition, pharmacie angers jean vilar zyrtec pharmacie en ligne pharmacie argenteuil joliot curie .

    Reply

Leave a Comment