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?

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.

  • 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 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... – Krzysiek Dróżdż Sep 25 at 21:34

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]);
  • 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 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 at 17:04

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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