How to load a script in footer in WordPress

Viewed 70

I want to add scripts to the footer of my WordPress page. However, they are still placed in the header section. Here is my code:

function prd_load_latest_jquery() {
    $wp_admin = is_admin();
    $wp_customizer = is_customize_preview();

    if ( $wp_admin || $wp_customizer ) {
        return;
    }
    else {

        wp_deregister_script( 'jquery' );
        wp_deregister_script( 'jquery-core' );
        wp_deregister_script( 'jquery-migrate' );


        wp_register_script( 'jquery-core', 'https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.0/jquery.min.js', '', null, true );
        wp_register_script( 'jquery-migrate', 'https://cdnjs.cloudflare.com/ajax/libs/jquery-migrate/3.3.0/jquery-migrate.min.js', '', null, true);

        wp_register_script( 'jquery', false, array( 'jquery-core', 'jquery-migrate' ), null, true );
        wp_enqueue_script( 'jquery' );
    }
}
add_action( 'wp_enqueue_scripts', 'prd_load_latest_jquery' );

I thought the last parameter in wp_register_script will take care of it and place the jQuery in the footer. However, this does not happen and the script is still in the header. What should I do?

Thanks.

0 Answers
Related