Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

So I am using the http://woothemes.com/woocommerce/ plugin for eCommerce and it's registering jQuery local and conflicting with my default jQuery call from Google CDN.

It took a long time but I isolated the issue to the following lines of code within woocommerce.php:

    $suffix = defined('SCRIPT_DEBUG') && SCRIPT_DEBUG ? '' : '.min';
    $lightbox_en = (get_option('woocommerce_enable_lightbox')=='yes') ? true : false;
    $chosen_en = (get_option('woocommerce_enable_chosen')=='yes') ? true : false;
    $jquery_ui_en = (get_option('woocommerce_enable_jquery_ui')=='yes') ? true : false;
    $scripts_position = (get_option('woocommerce_scripts_position') == 'yes') ? true : false;

    // Woocommerce.min.js is minified and contains woocommerce_plugins
    wp_enqueue_script( 'woocommerce', $this->plugin_url() . '/assets/js/woocommerce'.$suffix.'.js', array('jquery'), '1.0', $scripts_position );
    if ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) {
        wp_enqueue_script( 'woocommerce_plugins', $this->plugin_url() . '/assets/js/woocommerce_plugins'.$suffix.'.js', array('jquery'), '1.0', $scripts_position );
    }

Deleting them cancels out the duplicate jQuery call which is causing me all my headaches. I have the following in functions.php to dequeue jQuery from the header, but for some reason, it persistsand refuses to go away! :/ Any ideas how to kill the local jQuery call from within functions.php? Is there something wrong with my code? :

// dequeue jquery from header

function theme_slug_dequeue_header_jquery() {
     wp_dequeue_script( 'jquery' );
}
add_action( 'wp_header', 'theme_slug_dequeue_header_jquery', 11 );
share|improve this question
add comment

3 Answers

up vote 2 down vote accepted

Actually it looks like your calling jquery wrong. You should always use wp_enqueue_script, otherwise your asking for conflicts. Don't call it manually.

The lines you pasted in from WooCommerce don't load jquery directly - they load it by setting the dependencies for the other scripts. WordPress handles the rest.

If you want to use jQuery from a CDN, deregister it and re-register it with the CDN version. Rather than paste you the code I'll direct you to the following example which explains how:

http://www.wpbeginner.com/wp-themes/replace-default-wordpress-jquery-script-with-google-library/

share|improve this answer
    
Thanks Mike, you're absolutely right. –  Brian Mar 5 '12 at 0:47
    
Can one of you state the steps to take to get this problem solved? I'm still a little unclear. Thanks! –  user14699 Mar 29 '12 at 18:50
add comment

Use this code to dequeue jQuery from any plugin, assuming that is enqueues the script using the wp_enqueue_script() function.

add_action( 'wp_enqueue_scripts', 'dequeue_jquery', 999999 );

function dequeue_jquery() {
    wp_deregister_script( 'jquery' );
    wp_register_script( 'jquery', 'http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js');
    wp_enqueue_script( 'jquery' );
}

The priority of 999999 causes the function to be called last, hopefully, in most cases.

share|improve this answer
    
The currently shipped version of jQuery (with WP core) is 1.8.x. Your recommendation will break. Maybe you want to edit your answer and include the version from the registered script array. –  kaiser Nov 21 '12 at 18:56
add comment

I was having a similar problem and stumble into this thread.

My issue was that WooCommerce 2.0.5 was registering jQuery by himself and that was breaking my own jQuery scripts, creating a conflict with my functions.php deregister/register/enqueue process.

After several attempts I noticed that I wasn't including the jQuery version in my function. After specifically adding it, the problem was solved.

This is what woocommerce/woocommerce.php does (line 1214):

public function check_jquery() {
    global $wp_scripts;

    // Enforce minimum version of jQuery
    if ( isset( $wp_scripts->registered['jquery']->ver ) && $wp_scripts->registered['jquery']->ver < '1.7' ) {
        wp_deregister_script( 'jquery' );
        wp_register_script( 'jquery', '/wp-includes/js/jquery/jquery.js', array(), '1.7' );
        wp_enqueue_script( 'jquery' );
    }
}

And this is how I override it in my functions.php file

function my_scripts_method() {
    wp_deregister_script( 'jquery' );
    wp_register_script( 
        'jquery', 
        'http://code.jquery.com/jquery.js',
        false, 
        '1.9.1' // IMPORTANT: declare jQuery version
        );
    wp_enqueue_script( 'jquery' );
}  

add_action('wp_enqueue_scripts', 'my_scripts_method');

Hope this could help someone else :)

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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