1

I am removing jQuery migrate in WordPress in this way:

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');
    }
}

I'm confused at how you set the in_footer property with the WP_Dependencies::add function, I tried the below but it didn't work:

$scripts->add( 'jquery', false, array( 'jquery-core' ), '1.2.1', ['in_footer' => true]);

What is the correct way to do this?

2 Answers 2

2
+50

There is much safer way of removing jquery-migrate... Your code is almost correct, but first you remove jQuery and then add it again. If dependencies for jQuery will change, then your code will cause problems.

But you don't have to remove script to change its dependencies. You can do exactly that:

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' );

It's much safer, since we remove only one dependency that we want to be removed.

And if you want to move jQuery to footer, then you can use this code:

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' ) );
        }

        $scripts->add_data( 'jquery', 'group', 1 );
        $scripts->add_data( 'jquery-core', 'group', 1 );
    }
} 
add_action( 'wp_default_scripts', 'remove_jquery_migrate' );

Again - we don't remove any scripts and add our own, but modify existing scripts only, so it's much more secure approach.

2
  • That's excellent - thank you. It certainly removes jQuery Migrate, but jQuery still remains in the header; however, during some recent reading I discovered this is probably because it has other dependencies that are in the header and hence it can't put it in the footer below them? (Dependencies are coming from plugins).
    – Brett
    Sep 25, 2018 at 21:31
  • Yes, if there are some dependencies that are enqueued in header, then jQuery will be enqueued in header as well... it would break dependent scripts otherwise... Sep 25, 2018 at 21:34
1

That is correct but you did supply a new source.

Replace:

$scripts->add( 'jquery', false, array( 'jquery-core' ), '1.2.1', ['in_footer' => true]);

With:

$scripts->add( 'jquery', 'http://new-url/', array( 'jquery-core' ), '1.2.1', ['in_footer' => true]);
2
  • So I have to give it the actual path to WP's jQuery? It can't be referenced by it's handle it was registered with?
    – Brett
    Sep 25, 2018 at 16:47
  • See the docs. It obviously state that you must provide a new url: @param string $src Full URL of the item, or path of the item relative to the WordPress root directory.. Try that and see if it works.
    – Ian
    Sep 25, 2018 at 17:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.