Here is the blogname in php <?php $blog_title = get_bloginfo('name'); ?> I want to convert it to javascript. The file I want the blog name is in a .js file so I cant create the variable with php.

  • Take a look at this answer. – Charles Clarkson May 20 '13 at 22:49
  • I looked at it but I don't understand how to implement. – Jess May 20 '13 at 23:34

Use wp_localize_script

function set_js_var() {
  $translation_array = array( 'blog_name' => get_bloginfo('name') );
  wp_localize_script( 'jquery', 'my_data', $translation_array );

If you look at the source of the page you will see something like:

<script type='text/javascript'>
/* <![CDATA[ */
var my_data = {"blog_name":"WordPress 3.5.1 Release"};
/* ]]> */

That is your Javascript variable.

You have to "register" that on some existing Javascript slug. That is, some slug you've already used to register a script. That is why I used jquery, which is the slug used by WordPress to load its bundled jQuery library.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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