0

I have some trouble with loading external javascript files inside my WordPress theme. Use the following markup:

<script src="<?php bloginfo('template_directory'); ?>/js/jquery-1.8.1.min.js" type="text/javascript"></script>
<script src="<?php bloginfo('template_directory'); ?>/js/custom.js" type="text/javascript"></script>

The above code is set inside the <head> of my header.php file of my WordPress theme.

Why is this not working? Or should I also use the following for link external scripts?

function my_scripts() {
    wp_enqueue_script( 'my-sweet-script', get_bloginfo('template_directory') . '/script.js', array('jquery') );
}

Thank you. Casper

1 Answer 1

1

You should always use wp_enqueue_script() only, so other scripts can rely on yours or bundle all scripts in one file.

Do not load another jQuery; WordPress comes with a built-in jQuery, and other plugins and the core depend on exactly that version.

wp_enqueue_script() accepts any valid URL as source file, external or internal. If something doesn't work check your browser's error console. Note you have to use jQuery, not $ in your scripts to access the global jQuery object.

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.