wp_register_script(… $in_footer = true) not working

Choose the right place in (rendering-)time

First off, you are hooking at init, while you want to add your callback on wp_enqueue_script.

Example:

add_action( 'wp_enqueue_script', function() {
    wp_deregister_script( 'jquery' );
    // Rest of logic
} );

Staying up to date with core

Also, you might want to use the correct version that core uses. I know, constantly looking at this is cumbersome and no one wants to monitor assets/ dependencies. Luckily you can use the information that is already present in core. The same goes for the needed dependencies for an asset has. Example:

add_action( 'wp_enqueue_script', function() {

    $version      = $GLOBALS['wp_scripts']->registered['jquery']->ver;
    $protocol="sprintf( "http%s://', is_ssl() ? 's' : '' );
    $dependencies = $GLOBALS['wp_scripts']->registered['jquery']->deps;

    wp_enqueue_script( 
        'jquery',
        "{$protocol}cdn.example.com", 
        $dependencies,
        $version,
        TRUE
    );
} );

Move registered scripts to the footer

There’s also an easier way to move an already registered script to the footer: Edit it’s information in the script registry directly. Consider the following (MU)plugin:

<?php
/* Plugin Name: Moves registered scripts to the footer */
add_action( 'wp_enqueue_scripts', function() {
    foreach ( apply_filters( 'wp_scripts.footer', [] as $script )
        $GLOBALS['wp_scripts']->add_data( $script, 'group', 1 );
}, PHP_INT_MAX -1 );

Now you can just attach an array to above filter and move scripts to the footer:

add_filter( 'wp_scripts.footer', function( Array $scripts ) {
    return [ 
        'jquery', 
        # Other scripts
    ];
} );

Additional notes, I could not stay back to write down…

There are also multiple very strange (but working) things to note about your code:

  • You are assigning variables when you do not need them. Especially in a function call, it’s not necessary to use myFunction( $var="foo" ). Just add the value and that’s it.
  • When you use //url.tld/script.js, then the client will be able to decide the route to go: HTTPS vs HTTP. This might be ok for distributed code (even then, it’s better to fetch the protocol up front and use it), but if you know the way that you are serving your site, then please just use it. It is faster and if there’s HTTPS available, then for gods sake: Use it!
  • Do not deregister WP core scripts. Use them. They are cached for logged in people already in their clients/ browsers. For all others, you will deliver a single, compatible version.
  • Relying on $_SERVER is only ok if your script will always only be executed via a client. On the command line, using data from this array is not reliable. In some cases it changes, in other’s it’s not set. WP has some fallbacks that you can use instead.