I cannot load jQuery successfully in custom WordPress theme

Cannot believe I stumbled across this issue again after so many years. We developed a custom WordPress theme that needs jQuery (both contact form 7 requires it, looks for it as jQuery and my own custom code looking for it as $). I can find contradicting code examples everywhere, some say that jQuery is included anyway and that you can simply enqueue it, others that you need a whole function to hold all your scripts etc.

Code tested so far that does not work: (outputs $ is not defined and jQuery is not defined)

Read More

Code example 1:

wp_enqueue_script( 'jquery' ); (does not work, get above error, in header.php)

Code example 2:

function loadScripts() {
    wp_enqueue_script( 'jquery' );
wp_register_script('jqueryCycle',get_template_directory_uri().'/js/jquery.cycle.js', 'jquery');
    wp_enqueue_script( 'jqueryCycle' );                 
    wp_register_script('javascript', get_template_directory_uri().'/js/javascript.js', 'jquery');
    wp_enqueue_script( 'javascript' );
wp_register_script('jqueryCookie', get_template_directory_uri().'/js/jquery.cookie.js', 'jquery');
    wp_enqueue_script( 'jqueryCookie' );
    wp_register_script('jqueryCookiecuttr', get_template_directory_uri().'/js/jquery.cookiecuttr.js', 'jquery' );
    wp_enqueue_script( 'jqueryCookiecuttr' );
}

add_action( 'wp_enqueue_scripts', 'loadScripts' );  (code i got from older theme, same errors)

Code example 3:

function td_load_js() {
    wp_enqueue_script( 'jquery' );
    wp_enqueue_script( 'javascript', get_template_directory_uri() . '/js/javascript.js', array( 'jquery' ), 1, false);

}
add_action(‘init’, ‘td_load_js’); (in fuctions.php, still same errors)

So I’m not sure what is wrong at the moment, even leaving the header alone with no script declaration is not working, so I assume I have to load jQuery and the other scripts somehow! I know I’m not supposed to use jQuery as $ but only as jQuery alone, but I’m leaving my code requesting $ just to look at the error messages until i can get it loaded.

UPDATE: Deregistering the local version and running my own definitely works, however I’m still trying to understand why the local version is not loaded automatically, as I cannot see any registration of the jQuery library in other theme’s files and neither is it enqueued!

Related posts

Leave a Reply

1 comment