Search code examples
jquerywordpresswp-enqueue-scripts

Change JQUERY version on a single page Wordpress


I am not sure if this is possible or not and cannot find a solution.

I am trying to "deregister", "register" and "enqueue" the legacy JQUERY for a single page. This is not working when I add "&& is_page(251)" ?

Full code below and works great when I take out the "is_page()" condition.

Thanks in advance!

function wpb_modify_jquery() {

    //check if front-end is being viewed
    if (!is_admin() && is_page('251')) {

        // Remove default WordPress jQuery
        wp_deregister_script('jquery');

        // Register new jQuery script via Google Library    
        wp_register_script('jquery', 'https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js', false, '1.12.4');

        // Enqueue the script   
        wp_enqueue_script('jquery');
    }
}

// Execute the action when WordPress is initialized
add_action('init', 'wpb_modify_jquery');

Solution

  • try using the enqueue hook

    function wpb_modify_jquery() {
    
        //check if front-end is being viewed
        if (!is_admin() && is_page('251')) {
    
            // Remove default WordPress jQuery
            wp_deregister_script('jquery');
    
            // Register new jQuery script via Google Library    
            wp_register_script('jquery', 'https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js', false, '1.12.4');
    
            // Enqueue the script   
            wp_enqueue_script('jquery');
        }
    }
    
    add_action( 'wp_enqueue_scripts', 'wpb_modify_jquery' );