I have on many occasions used wp_enqueue_script
to pass some ‘query variables’ to scripts like this:
function my_enqueue_scripts(){
$tempurl = urlencode( get_bloginfo( 'template_url' ) );
wp_register_script( 'somescript', get_bloginfo( 'template_url' ) . '/js/somescript.php?url=' . $tempurl );
wp_enqueue_script( 'somescript' );
}
add_action( 'wp_enqueue_scripts', 'my_enqueue_scripts' );
And I am just wondering if this is a ‘best practice’ or ‘safe’ to do, I don’t see why it wouldn’t be but I guess you never know until you ask. I’m just wondering what are the downsides of doing this, or even if there is an alternative/better way to do this.
I also notice if I pass more than 1 variable, only the first one will actually be retrievable in the script. Is there something I need to do to make that work?
Some advice would be great, thanks.
wp_localize_script
is the âWordPress wayâ to pass data from php to javascript. See this post by Otto for more info.