12

WordPress is loading the following 2 files in wp_head():

<script type='text/javascript' src='http://example.com/wp-includes/js/jquery/jquery.js?ver=1.10.2'></script>
<script type='text/javascript' src='http://example.com/wp-includes/js/jquery/jquery-migrate.min.js?ver=1.2.1'></script>

In an attempt to stop this from happening, I have tried deactivating all plugins and deregistering jQuery in functions.php, but nothing seems to get rid of it.

Any ideas how I can stop this?

I'm using Wordpress v3.6.

Paulo Boaventura
  • 1,365
  • 1
  • 9
  • 29
user2227359
  • 317
  • 1
  • 4
  • 13

4 Answers4

11

You might want to use this in your functions.php

add_filter( 'wp_default_scripts', 'remove_jquery_migrate' );

function remove_jquery_migrate( &$scripts){
    if(!is_admin()){
        $scripts->remove( 'jquery');
        $scripts->add( 'jquery', false, array( 'jquery-core' ), '1.2.1' );
    }
}

1.2.1 = latest version of jquery-migrate

If you want to check whether your site requires jquery-migrate, open wp-config.php and this line of code: define('SCRIPT_DEBUG', true);. That way you can monitor any errors. Don't forget to remove this when you put your site live!

Hope that helps.

Omar Abdirahman
  • 721
  • 9
  • 23
  • Hey, thanks for this - it looks good. How do you inspect whether you have error without jquery-migrate? – panza Jan 10 '17 at 17:07
  • 4
    Submitted an edit to remove the version number as it's not reasonable to maintain that and it may be omitted. Also worth noting this approach works because the `jquery` handle has dependencies on `jquery-migrate` and `jquery-core`. If either of the later are renamed, or if something else is added, this function will no longer have the desired effect. – vhs Jun 10 '17 at 10:17
  • I think the version is only a cache buster, so you can put everything you want there – Marco Panichi Nov 23 '18 at 11:29
3

Use the wp_enqueue_script function of Wordpress instead of defining the scripts in your template. This way you won't import different versions of javascript libraries.

http://codex.wordpress.org/Function_Reference/wp_enqueue_script

Joren
  • 3,068
  • 25
  • 44
3

Wordpress has the jquery library and you should take advantage of this. If you want to remove this you can do something like this:

function deregister_qjuery() {  
    if ( !is_admin() ) {
        wp_deregister_script('jquery');
    }
}  

add_action('wp_enqueue_scripts', 'deregister_qjuery'); 

But consider how to load your JS files properly.

Further reading:

Dear theme developers, this is how you add scripts in WordPress themes http://wpcandy.com/teaches/how-to-load-scripts-in-wordpress-themes/

Loading jQuery correctly http://beneverard.co.uk/blog/wordpress-loading-jquery-correctly-version-2/

How to add a backup javascript file in Wordpress that initially loads off internet How to add a backup javascript file in Wordpress that initially loads off internet

Community
  • 1
  • 1
Miguel Garrido
  • 1,121
  • 11
  • 23
1
function dequeue_jquery_migrate($scripts){
    if(!is_admin() && !empty($scripts->registered['jquery'])){
        $jquery_dependencies = $scripts->registered['jquery']->deps;
        $scripts->registered['jquery']->deps=array_diff($jquery_dependencies,array('jquery-migrate'));
    }
}
add_action( 'wp_default_scripts', 'dequeue_jquery_migrate' );
XNicON
  • 153
  • 1
  • 6
  • This also works from within the `wp_print_scripts` hook when replacing the `$scripts` variable with the global `$wp_scripts` variable. – kontur Nov 21 '18 at 14:55