1

I'm fixing a wordpress plugin that uses a 1.0.0 version of jquery. The jquery version automatically included in wordpress is 1.11.0. In a test site, the plugin is dependent on the 1.0.0 jquery and doesn't work on the latest version.

However, it works on a test site because the plugin is loaded BEFORE the wp-includes/js/jquery/jquery.js and it uses it's own jquery library version before the script is overwritten by the latest jquery version. It doesn't produce any errors in the console.

But in the production site, it loads all the plugins after the wp-includes/js/jquery/jquery.js So, how do I force the plugin to load, before wordpress loads the wp-includes/js/jquery/jquery.js file?

This is the code in the plugin:

add_action('init', 'myplugin_init');

function myplugin_init() {
wp_enqueue_script('jquery.min', plugins_url(MYPLUGIN_FOLDER . '/js/jquery.min.js'), array(), '1.0.0', false);
wp_enqueue_script('highcharts', plugins_url(MYPLUGIN_FOLDER . '/js/highcharts.js'), array(), '1.0.0', false);
wp_enqueue_script('exporting', plugins_url(MYPLUGIN_FOLDER . '/js/exporting.js'), array(), '1.0.0', false);
}

I already tried, for testing purposes, to force the enqueue of plugin scripts right before wp_head(); but it still loads the plugin files AFTER the wp-includes/js/jquery/jquery.js.

<?php
    wp_enqueue_script('jquery.min', plugins_url(MYPLUGIN_FOLDER . '/js/jquery.min.js'), array(), '1.0.0', false);
    wp_enqueue_script('highcharts', plugins_url(MYPLUGIN_FOLDER . '/js/highcharts.js'), array(), '1.0.0', false);
    wp_enqueue_script('exporting', plugins_url(MYPLUGIN_FOLDER . '/js/exporting.js'), array(), '1.0.0', false);

wp_head();
    ?>

Any help is much appreciated. Thanks.

2

1.0.0? Really?! Anyway you could try the print_scripts_array filter:

// Hack of wp_prototype_before_jquery() in "wp-includes/script-loader.php"
function wpse157295_print_scripts_array( $js_array ) {
    if ( false === $jquery = array_search( 'jquery-core', $js_array, true ) ) // Now 'jquery-core', not 'jquery'
        return $js_array;

    $keys = array( 'jquery.min', 'highcharts', 'exporting' );

    foreach ( $keys as $key ) {
        if ( ( $idx = array_search( $key, $js_array, true ) ) && $idx >= $jquery ) {

            unset($js_array[$idx]);

            array_splice( $js_array, $jquery, 0, $key );
        }
    }

    return $js_array;
}
add_filter( 'print_scripts_array', 'wpse157295_print_scripts_array' );
  • I know, it's a pain to use an ancient jquery version. I'm not the one who developed the plugin - it was just passed to me for fixing. Your script is great! I just don't understand it much there's very little documentation about that filter. How do I include exporting.js and highcharts.js before loading the jquery latest version as well? Your code only puts the old jquery first, but I need those 2 other files too. Thanks! – Pau Aug 10 '14 at 6:50
  • Okay I updated the answer to deal with the 3. Re filter it looks like legacy code that's just been left there. Re old jquery yeah, more legacy, the problem here though is that installing jquery 1.0.0 will probably break anything else that uses jquery... – bonger Aug 10 '14 at 7:26
  • Thank you so much! Well, problem with clients, they don't like updating their plugins as long as it currently works. :) Anyway, the jquery 1.0.0 didn't break things anymore, as long as the latest jquery is loaded and overwrites the old version before it breaks anything. Thanks a lot. – Pau Aug 10 '14 at 8:17
-2

Have you tried jQuery Migrate? It provides backwards compatibility to deprecated jQuery functions.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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