WordPress Enqueue Script Chaos (jQuery & Foundation.js)

The question:

I’ve been working on building a theme for the past couple days now, and I’ve run into a wall. While I originally included foundation.js as an enqueued script, I couldn’t remember why I had added it. Today, when I tried removing it (to improve page-load speed) from my functions.php file, it messed up all of my jQuery plugins.

After some researching, I discovered that Foundation already includes jQuery, so I was loading two instances of it at a time (I also loaded from the theme’s js folder). So, now that I’ve removed foundation, my custom.js file commands don’t work unless I prepend ‘jQuery’ instead of the simpler ‘$’.

I think there’s some part of my WordPress install that I made bug out, even if I keep the ‘Foundation’ name reference with the jQuery file earlier called, the theme works. It’s like it has to have that ‘Foundation’ name.

Below I’ve included both my script calls, as well as my custom js file, in hopes that someone will be able to aid me in diagnosing this problem.


Chrome Console Error Message

jQuery & Plugins shown loading in Chrome Console


function register_js() {
if (!is_admin()) {
    wp_register_script('jquery', get_template_directory_uri() . '/js/libraries/jquery-1.7.1.min.js');
    wp_register_script('jquery-ui', get_template_directory_uri() . '/js/libraries/jquery-ui-1.8.16.min.js', 'jquery');
    wp_register_script('superfish', get_template_directory_uri() . '/js/plugins/jquery.superfish.js', 'jquery');
    wp_register_script('supersubs', get_template_directory_uri() . '/js/plugins/jquery.supersubs.js', 'jquery');
    //wp_register_script('foundation', get_template_directory_uri() . '/js/plugins/jquery.foundation.js', 'jquery');
    wp_register_script('custom', get_template_directory_uri() . '/js/custom.js', array('jquery','jquery-ui') );
add_action('init', 'register_js');


$(document).ready(function() {

/* Menu - Superfish */
    minWidth:    11,
    maxWidth:    30,
    extraWidth:  1    
    hoverClass: "sfHover", 
    speed: 'fast', 
    dropShadows: false, 
    delay: 0,
    autoArrows: false,
    animation: {height:'show',opacity:'show'}

/* Blog Tabs */
$('#tab_controls').tabs({ fx: [ {opacity:'toggle', duration:'normal'},{opacity:'toggle', duration:'slow'}] }); 


The Solutions:

Below are the methods you can try. The first solution is probably the best. Try others if the first one doesn’t work. Senior developers aren’t just copying/pasting – they read the methods carefully & apply them wisely to each case.

Method 1

Try changing the first line of your custom JS file.

jQuery(document).ready(function($) {

jQuery operated within WordPress should be run in compatibility mode. That line allows this to happen, as per point #5 here.

Method 2

First, if your Theme is intended to be publicly distributed, do not deregister core-bundled scripts and replace them with your own. This includes jQuery, jQuery UI, and several other scripts.

Second, yes: WordPress runs with jQuery no-conflict, which means that you must account for no-conflict in your scripts. The Codex-recommended method is here:

jQuery(document).ready(function($) {
    // $() will work as an alias for jQuery() inside of this function

These two probably have nothing to do with your issues, but are best practice:

  1. If calling both wp_register_script() and wp_enqueue_script() in the same context/function, simply omit wp_register_script(), and use wp_enqueue_script().
  2. Hook into wp_enqueue_scripts, rather than init.

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Comment