admin管理员组

文章数量:1430083

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.

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.

Share Improve this question asked Aug 10, 2014 at 5:33 PauPau 1132 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 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' );

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

本文标签: functionsHow to load plugin before the wordpress jquery