How to Disable jQuery-migrate in WordPress (using code)

If you open the source code of your site in WordPress, in the header of the site you can find about these lines:

<script src='http://example.com/wp-includes/js/jquery/jquery.js'></script>
<script src='http://example.com/wp-includes/js/jquery/jquery-migrate.min.js'></script>

As you can see, jquery-migrate is plugged in along with jquery.

The jQuery Migrate plugin is used to help sites upgrade to the latest version of jQuery. The script restores APIs that have been removed, and the development version shows warnings in the browser console for any deprecated or missing APIs. This enables developers and admins to more easily upgrade from older to newer versions of jQuery.

This is all good, but sometimes jQuery Migrate is just not needed, for example:

  • You need an older version of jQuery for your site. But WordPress kept including jQuery Migrate, which made it impossible to do the testing that needed done.
  • Your site's javascript code is written according to the latest standards, so you don't need to check for outdated features.

Disable jquery-migrate with the plugin

To disable jquery-migrate you can install a very small plugin Remove jQuery Migrate.

For the plugin to start working it just needs to be activated. It does not have any settings.

Disable jQuery Migrate with code

add_filter( 'wp_default_scripts', 'remove_jquery_migrate' );

function remove_jquery_migrate( $scripts ) {

	if ( empty( $scripts->registered['jquery'] ) || is_admin() ) {
		return;
	}

	$deps = & $scripts->registered['jquery']->deps;

	$deps = array_diff( $deps, [ 'jquery-migrate' ] );
}

This code does the same thing as the above plugin. To make it work, just add it to your theme's functions.php file or somewhere else where you include you php code snippets.

Once added, this script tells WordPress to NOT load jQuery Migrate on the front-end of your site.