WP-Mix

A fresh mix of code snippets and tutorials

WordPress Disable jQuery Migrate

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.

Various versions of WordPress make use of the jQuery Migrate plugin. To check if your WordPress is including the script on the front-end of your site, you can examine the source code of your web pages. If WordPress is including jQuery Migrate, it will look something like this in the source code:

<script type='text/javascript' src='https://example.com/wp-content/themes/plugin-planet/lib/js/jquery-migrate.js' id='jquery-migrate-js'></script>

That’s all fine and well, but there are scenarios where the Migrate script may be included when it is not needed for anything. For example, on one of my sites, the pages required a specific (older) version of jQuery for development purposes. But WordPress kept including jQuery Migrate, which made it impossible to do the testing that needed done.

Disable jQuery Migrate

So for anyone who needs to disable the automatically included jQuery Migrate plugin/script, here is the magic code that will do it:

function remove_jquery_migrate( $scripts ) {
	
	if ( ! is_admin() && isset( $scripts->registered['jquery'] ) ) {
			
		$script = $scripts->registered['jquery'];
		
		if ( $script->deps ) { 
			$script->deps = array_diff( $script->deps, array( 'jquery-migrate' ) );
		}
	}
}
add_action( 'wp_default_scripts', 'remove_jquery_migrate' );

No changes are necessary. Simply add to your site via theme (or child theme’s) functions.php file or add via simple custom plugin. Once added, this script tells WordPress to NOT load jQuery Migrate on the front-end of your site. Similar functionality may be added to disable jQuery Migrate on the backend Admin Area, but it is not recommended to do so.

Note: I forget where I found this code snippet. I *think* it was posted at WordPress.org in the Codex or whatever they’re calling it these days.

Learn more

Digging Into WordPressWordPress Themes In DepthWizard’s SQL Recipes for WordPress