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.

The goal- include my-script.js (dependant on jquery) in my WP theme.

The problem- When I use this code,(in the head element of the header template file) my entire page goes blank when loaded:

<?php wp_enqueue_script( 'jquery' ); ?>

<?php wp_enqueue_script(
      'my-script',
     "<?php echo get_template_directory_uri() . "/my-script.js"?>",
    array('jquery'),
    '1.0.0'
);
?>

Have commented out all of my javascript except for an alert, and the javascript works fine on non-WP webpage. So the problem is not there.

I have tried putting the code in functions.php, wrapping the enqueue scripts in a function triggered by init, a la source 1 and other sources,

that approach failed to load the script at all.

More confusingly, according to the codex here, the usual approach of calling the script as I would in an HTML document is fine. And this approach, (skipping wp_enqueue_script), does load my-script.js just fine.

Do I need to use the wp_enqueue_script to include javascript files in my theme, and if so, What am I doing wrong that causes the whole page to disappear on load?

First-time poster, thanks for your patience!

share|improve this question

1 Answer 1

up vote 0 down vote accepted

You don't need to enqueue jquery if it's already being referenced in my 'my-script'. Also you're running php within php, so you can change this:

"<?php echo get_template_directory_uri() . "/my-script.js"?>",

to

get_template_directory_uri() . "/my-script.js",
share|improve this answer
    
I think that was the issue, thank you for the fast response! –  Paradasia Jan 12 '12 at 0:15

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.