Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

share|improve this question
    
what do you mean with "passing more than 1 variable"? also, why are you placing all ur scripts into "somescript.php"? I don't understand why you are passing the blogurl at the end. Normally there are not problems with this approach, basically is used to keep the header or footer template as clean as possible. –  andresmijares25 Jan 21 '12 at 20:56
    
I am just using the code above as an extremely generic example. What I mean by passing more than 1 variable is if this is in the URL: ?var1={somevar}&var2={anothervar}. My question is not about using wp_enqueue_scripts specifically it is about using a PHP file and passing data with it inside that function. –  Jared Jan 21 '12 at 21:01
add comment

1 Answer

up vote 2 down vote accepted

wp_localize_script is the “WordPress way” to pass data from php to javascript. See this post by Otto for more info.

share|improve this answer
    
Exactly what I needed, thanks! –  Jared Jan 21 '12 at 23:03
    
@ milo - thanks, I was also looking for that ! –  user13279 Jan 22 '12 at 3:04
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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