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'm creating a twitter bootstrapped child theme based on TwentyEleven.I want to load bootstrap.js so I've written the following line to get the bootstrap.js file but it is not returning correct path.

<script type="text/javascript" src="<?php dirname( bloginfo('stylesheet_url') ) ?>/bootstrap/js/bootstrap.js"></script>

The path I'm getting:

http://localhost/wordpress/wp-content/themes/codemasala/style.css/bootstrap/js/bootstrap.js

The path I should get:

http://localhost/wordpress/wp-content/themes/codemasala/bootstrap/js/bootstrap.js
share|improve this question
add comment

1 Answer

up vote 4 down vote accepted

The reason why your code isn't working is because bloginfo() echoes the data where get_bloginfo() doesn't. So dirname() isn't really used. Using stylesheet_directory is better though since that won't need the dirname() call at all.

You could also use the wrapper get_stylesheet_directory_uri() which adheres to the filter stylesheet_directory_uri.

This is your code corrected:

<script type="text/javascript" src="<?php bloginfo('stylesheet_directory'); ?>/bootstrap/js/bootstrap.js"></script>

See: http://codex.wordpress.org/Function_Reference/bloginfo

However, the "WordPress-way" of adding scripts is by adding it the script queue. Doing it this way will ensure that your script is always loaded after it's dependencies and it will also make it possible for other scripts to use your script as a dependency.

To add a script to WordPress's queue you use wp_enqueue_script(). See the examples. Below is a script which should work for you. Add it to your theme's functions.php file.

function enqueue_bootstrap() {
  wp_enqueue_script('twitter-bootstrap', get_stylesheet_directory_uri() . '/bootstrap/js/bootstrap.js', array('jquery'));
}
add_action('wp_enqueue_scripts', 'enqueue_bootstrap');
share|improve this answer
    
Sorry, but that's a bad advice in so many directions... –  kaiser Sep 19 '12 at 12:31
    
In what way? As I understood it, he asked what he did wrong. Sure he should use wp_enqueue_script(), but that wasn't really the question. –  hampusn Sep 19 '12 at 12:33
    
Then simply show him how to make it better. –  kaiser Sep 19 '12 at 12:35
    
I'm a Wordpress newbie so can you tell where I should put that enqueue function? –  Rajat Saxena Sep 19 '12 at 12:47
1  
@kaiser Updated. Overall betterified answer. –  hampusn Sep 19 '12 at 13:06
show 2 more comments

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.