Wordpress - Annoying "JQMIGRATE: Migrate is..." in console after update to WordPress 4.5

WordPress uses the jQuery migrate script to ensure backwards compatibility for any plugins or themes you might be using which use functionality removed from newer versions of jQuery.

With the release of WordPress 4.5, it appears they have upgraded the version of jQuery migrate from v1.2.1 to v1.4.0 - Having a quick scan through the code reveals that v1.4.0 logs that the script is loaded regardless of whether or not the migrateMute option is set, in both the uncompressed and minified versions.

The only way to remove the notice is to ensure all your plugins/theme code don't rely on any old jQuery functionality, and then remove the migrate script. There's a plugin out there to do this, but it's quite a simple method that can just be placed in your theme's functions file or similar:

add_action('wp_default_scripts', function ($scripts) {
    if (!empty($scripts->registered['jquery'])) {
        $scripts->registered['jquery']->deps = array_diff($scripts->registered['jquery']->deps, ['jquery-migrate']);
    }
});

Please note that this is not considered best practice for WordPress development and in my opinion the migrate script should not be removed just for the sake of keeping the developer console clean.


You could change the log message text to blank in jquery-migrate.min.js but this will not be preserved on core update.

The alternative is to add passthrough/filter function copy of console.log to just before the migrate script is loaded, and tell it to ignore logging messages that contain 'Migrate is installed'. Doing it this way will preserve other Migrate warnings too:

// silencer script
function jquery_migrate_silencer() {
    // create function copy
    $silencer = '<script>window.console.logger = window.console.log; ';
    // modify original function to filter and use function copy
    $silencer .= 'window.console.log = function(tolog) {';
    // bug out if empty to prevent error
    $silencer .= 'if (tolog == null) {return;} ';
    // filter messages containing string
    $silencer .= 'if (tolog.indexOf("Migrate is installed") == -1) {';
    $silencer .= 'console.logger(tolog);} ';
    $silencer .= '}</script>';
    return $silencer;
}

// for the frontend, use script_loader_tag filter
add_filter('script_loader_tag','jquery_migrate_load_silencer', 10, 2);
function jquery_migrate_load_silencer($tag, $handle) {
    if ($handle == 'jquery-migrate') {
        $silencer = jquery_migrate_silencer();
        // prepend to jquery migrate loading
        $tag = $silencer.$tag;
    }
    return $tag;
}

// for the admin, hook to admin_print_scripts
add_action('admin_print_scripts','jquery_migrate_echo_silencer');
function jquery_migrate_echo_silencer() {echo jquery_migrate_silencer();}

The result is a one line of HTML script added to both frontend and backend that achieves the desired effect (prevents the installed message.)


Just a little test here.

I peeked into jquery-migrate.js and noticed this part:

// Set to true to prevent console output; migrateWarnings still maintained
// jQuery.migrateMute = false;

so I tested the following with the newly wp_add_inline_script(), introduced in version 4.5:

add_action( 'wp_enqueue_scripts', function()
{   
    wp_add_inline_script( 
        'jquery-migrate', 'jQuery.migrateMute = true;',
        'before' 
    );
} );

This will change:

JQMIGRATE: Migrate is installed with logging active, version 1.4.0

to:

JQMIGRATE: Migrate is installed, version 1.4.0

So it doesn't actually prevent all console output, like this part in jquery-migrate.js:

// Show a message on the console so devs know we're active
if ( window.console && window.console.log ) {
    window.console.log( "JQMIGRATE: Migrate is installed" +
        ( jQuery.migrateMute ? "" : " with logging active" ) +
        ", version " + jQuery.migrateVersion );
}

Tags:

Jquery

Notices