Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

WordPress

Frustrating problem with wp_enqueue_script..

After typing in the code to enqueue the js files, I found that my website would give me an internal error (500). I double-checked my code with what was in the video, and could find no errors with it, so I went on to start commenting it out piece by piece. Eventually I was able to determine exactly what line of code were making the website break.

function wpt_theme_js() {
    wp_enqueue_script( 'modernizr_js', get_template_directory_uri() . '/js/modernizr.js', 'array()', '', false ); 

/*
    wp_enqueue_script( 'foundation_js', get_template_directory_uri() . '/js/foundation.js', 'array('jquery')', '', true ); 
    wp_enqueue_script( 'main_js', get_template_directory_uri() . '/js/app.js', 'array('jquery', 'foundation_js')', '', true );
*/

}

add_action( 'wp_enqueue_scripts', 'wpt_theme_js' ); 

The commented out section was breaking my website. I discovered, however, that removing the '' around "jquery" and "foundation_js" (in the arrays) made the whole thing work.

Is there a way to catch this problem without it breaking the site entirely? It took me a while to figure out what was going on, as I had not been regularly testing my changes to the site (all from the videos), and the only way for me to get access to it again was removing my theme from the directory!

So again, is there a way to catch this problem without it breaking the site?

2 Answers

Sue Dough
Sue Dough
35,800 Points

You are close but this won't work. The issue is your trying to treat arrays like strings.

bad:

'array('jquery')'

good:

array( 'jquery' )

Wrapping an array in single quotes won't work and it will be treated like a string.

function wpt_theme_js() {
  wp_enqueue_script( 'modernizr_js', get_template_directory_uri() . '/js/modernizr.js', array(), '', false );
  wp_enqueue_script( 'foundation_js', get_template_directory_uri() . '/js/foundation.js', array( 'jquery' ), '', true );
  wp_enqueue_script( 'main_js', get_template_directory_uri() . '/js/app.js', array( 'jquery', 'foundation_js' ), '', true );
}

Next time I reccomend to turn wp-debug on to true and tail -f your error log.

Konrad Pilch
Konrad Pilch
2,435 Points

I don't know..

But you could compare to my working script

function theme_js() {

    global $wp_scripts;

    wp_register_script( 'html5_shiv', 'https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js', '', '', false );
    wp_register_script( 'respond_js', 'https://oss.maxcdn.com/respond/1.4.2/respond.min.js', '', '', false );


    $wp_scripts->add_data( 'html5_shiv', 'conditional', 'lt IE 9' );
    $wp_scripts->add_data( 'respond_js', 'conditional', 'lt IE 9' );

    wp_enqueue_script( 'bootstrap_js', get_template_directory_uri() . '/js/bootstrap.min.js', array('jquery'), '', true );
    wp_enqueue_script( 'main_js', get_template_directory_uri() . '/js/main.js', array('jquery'), '', true );


}
add_action( 'wp_enqueue_scripts', 'theme_js');

If thats any help.

Your working script looks just like what I originally had. I also just tried adding the ' around the items in the arrays again, and it caused the internal error. Could that be because I am using MAMP (windows) to host? I have not tried with a hosted site yet.

No big deal, since I have it working, but I figured I'd toss that idea out there.

Konrad Pilch
Konrad Pilch
2,435 Points

I use MAMP on mac. It's hard to tell for me, I'm not as experienced in this, but id could guess there could be something wrong with the way you did it - maybe.

I'd have to see it to tell, the whole environment.