WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I’d like to know how to use the get_bloginfo( 'template_directory' ) tag in an external JS file.

I just found out that it’s possible using the wp_localize_script function but I didn’t manage to get it to work …


wp_enqueue_script( 'custom_js' );
wp_localize_script('custom_js', 'wp_urls', array( 'template_dir' => get_bloginfo( 'template_directory' ) )); 

header.php (beneath wp_head();)

share|improve this question
Maybe changing to 'wp_urls' might work? – akTed Jan 21 '13 at 14:46
I changed every hyphen to an underscore—but it still doesn’t work. – user1706680 Jan 21 '13 at 14:59
Is the .js referenced by 'custom_js' loading in the <head>? – akTed Jan 21 '13 at 15:03
... and is it loading in the <head> before the alert? – s_ha_dum Jan 21 '13 at 15:18
wp-head(); is called before the alert but there’s nothing added in the header by wp-head();. – user1706680 Jan 21 '13 at 16:06
up vote 1 down vote accepted

It doesn't work because you haven't enqueued your script properly. If the script isn't printed, neither are the variables set by wp_localize_script. Please read a WP Codex entry on wp_localize_script function. You have to include a path to the script after the handle.

share|improve this answer
Yep, I manage to get it to work with wp_enqueue_script( 'custom_js', get_bloginfo( 'template_directory' ) . '/js/google-map.js' ); wp_localize_script('custom_js', 'wp_urls', array( 'template_dir' => get_bloginfo( 'template_directory' ) )); – user1706680 Jan 21 '13 at 20:03

Your Answer


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.