1

I'm trying to load two JS files. But some how its not working. Tried both of these, and the console window doesn't complain of unable to load the file, it never was in the head.
Also, I'm having a weird issue (not sure if related) that CSS file would not be loaded if not specified explicitly in the header. However, if specified, it would be loaded twice.

version 1

add_action( 'after_setup_theme', 'theme_initialization' );

function theme_initialization() {
    add_action( 'wp_enqueue_scripts', 'enqueue_scripts' );
}

function enqueue_scripts() {
    $template_dir = get_template_directory_uri();

    wp_enqueue_script( 'xxxx', $template_dir . '/js/xxxx.js', array( 'jquery' ), '1.0', true );
    wp_enqueue_script( 'xxxx', $template_dir . '/js/xxxx.js', array( 'jquery' ), '1.0', true );
}

version 2

add_action( 'wp_enqueue_scripts', 'enqueue_scripts' );
function enqueue_scripts() {
    $template_dir = get_template_directory_uri();

    wp_enqueue_script( 'xxxx', $template_dir . '/js/xxxx.js', array( 'jquery' ), '1.0', true );
    wp_enqueue_script( 'xxxx', $template_dir . '/js/xxxx.js', array( 'jquery' ), '1.0', true );
}
10
  • What is the code in header.php, and what is the browser-rendered output of either/both of those alternatives? Commented May 2, 2013 at 16:44
  • 1
    @ChipBennett, it's in functions.php. Also I find it interesting that when I do get_template_directory_uri(), sometimes I get my template's path, but sometimes I get that of twentytwelve. Even though I'm not writing a child theme.
    – Angela
    Commented May 2, 2013 at 16:51
  • 2
    Hey Angela! Can you confirm that wp_head() is getting called in your header.php file right before the closing </head> tag? Also, could you confirm for me that jQuery is loading properly?
    – bosco
    Commented May 2, 2013 at 17:02
  • 1
    @bosco: exactly two of the things I was trying to clarify with the questions in my comment. Commented May 2, 2013 at 17:23
  • 1
    Glad it all worked out :) . Press on, Angela!
    – bosco
    Commented May 11, 2013 at 17:50

1 Answer 1

2

Whenever building a theme, you should always add wp_footer(); in the footer.php right before the closing body tag to ensure all JS files that are loaded at the footer can be loaded.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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