WordPress Enqueue Script Chaos (jQuery & Foundation.js)

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 ‘$’.

Read More

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.

Links:

Chrome Console Error Message

jQuery & Plugins shown loading in Chrome Console

SCRIPT CALLS:

function register_js() {
if (!is_admin()) {
    wp_deregister_script('jquery');
    wp_deregister_script('foundation');
    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') );
    wp_enqueue_script('jquery');
    wp_enqueue_script('jquery-ui');
    wp_enqueue_script('superfish');
    wp_enqueue_script('supersubs');
    //wp_enqueue_script('foundation');
    wp_enqueue_script('custom');
}
}
add_action('init', 'register_js');

CUSTOM JS FILE:

$(document).ready(function() {

/* Menu - Superfish */
$('ul.menu').supersubs({ 
    minWidth:    11,
    maxWidth:    30,
    extraWidth:  1    
}).superfish({ 
    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'}] }); 

});

Related posts

Leave a Reply

2 comments

  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.

  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.