Ok, my previous post is ok, but it doesn’t make a usage of a fabulous function built into WP core – wp_localize_script

What I did wrong previously was that I hardcoded the variable into the footer, hence if the file I in which I wanted to use the variable

would be called in the header instead I would simply be unable to make use of it. This is where localize script comes in handy. It basically allows using that variable no matter where you have actually included your script,

For example (taken from CODEX)

wp_enqueue_script( ‘some_handle’ );
$translation_array = array( ‘some_string’ =>; __( ‘Some string to translate’ ), ‘a_value’ =>; ’10’ );
wp_localize_script( ‘some_handle’, ‘object_name’, $translation_array );

It will basically create a javascript object, with two values, being accessible via object_name.some_string or object_name.a_value 🙂

Please check my previous post to picture what was I doing wrong.