How to Pass PHP Data and Strings to JavaScript in WordPress

This one post by TutsPlus is JUST what I was needing this morning.  I got so excited when I found that there is a way to pass info from WordPress to the enqueued JavaScripts, that I could not help and put here the code that will do the trick and of course the original post.  For my (o other’s) future reference.

wp_enqueue_script( 'my_js_library', get_template_directory_uri() . '/js/myLibrary.js' );
 
$dataToBePassed = array(
    'home'            => get_stylesheet_directory_uri(),
    'pleaseWaitLabel' => __( 'Please wait...', 'default' )
);
wp_localize_script( 'my_js_library', 'php_vars', $datatoBePassed );

Original Post

UPDATE: I had to use this example instead from Pippins Plugins: Use wp_localize_script, It Is Awesome

Leave a Reply

Your email address will not be published. Required fields are marked *