0

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!

1 Answer 1

0

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",
1
  • I think that was the issue, thank you for the fast response!
    – Paradasia
    Jan 12, 2012 at 0:15

Your Answer

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

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