Site logo

Archived topic

Deregister Jquery on Specific Page

4 replies · Started by Zach on January 19, 2018

Viewing posts 1–5 of 5

Trying to deregister wp-includes jquery and jquery-migrate on specific pages to load a different jquery library. Searched all over but couldn't figure it out so I thought I'd ask you.

What handles should I use to deregister wordpress' jquery scripts? 'jquery-ui-core' doesnt work and 'jquery' deregisters all jquery libraries including the ones im trying to add.

Are the handles my issue or maybe i am not properly reregistering the scripts after deregistering 'jquery'

Here is my code: (feel free to laugh if it is terrible, I'm new to PHP)

// Loads necessary CCH javascript to CCH content pages

function wpdocs_dequeue_script() {
	if (is_page('tools')) 
   wp_deregister_script( 'jquery');
}
add_action( 'wp_print_scripts', 'wpdocs_dequeue_script', 100);

function load_scripts() {
    global $post;

    if( is_page() || is_single() )
    {
        switch($post->post_name) // post_name is the post slug which is more consistent for matching to here
        {
            case 'calendar':
				wp_register_script('jquery1.4', 'http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js', array('jquery'), '', true);
                wp_enqueue_script('jquery1.4');
				wp_register_script('jqueryui', 'http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.1/jquery-ui.min.js', array('jquery'), '', true);
                wp_enqueue_script('jqueryui');
				wp_register_script('cch_econtent', '/cch_js/cch_econtent_js.js', array('jquery'), '', true);
                wp_enqueue_script('cch_econtent');
                wp_register_script('jMonthCalendar', '/cch_js/jMonthCalendar.js', array('jquery'), '', true);
                wp_enqueue_script('jMonthCalendar');
                wp_register_script('cluetip', '/cch_js/jquery.cluetip.js',  array('jquery'), '', true);
                wp_enqueue_script('cluetip');
                wp_register_script('bigiframe', '/cch_js/jqpopup/jquery.bgiframe.min.js', array('jquery'), '', true);
                wp_enqueue_script('bigiframe');
                wp_register_script('popupdnr', '/cch_js/jqpopup/jqDnR.min.js',  array('jquery'), '', true);
                wp_enqueue_script('popupdnr');
                wp_register_script('jqpopup', '/cch_js/jqpopup/jquery.jqpopup.min.js',  array('jquery'), '', true);
                wp_enqueue_script('jqpopup');
                break;
            case 'tools':
                wp_register_script('jquery1.4', 'http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js', array('jquery'), '', false);
                wp_enqueue_script('jquery1.4');
				wp_register_script('jqueryui', 'http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.1/jquery-ui.min.js', array('jquery'), '', false);
                wp_enqueue_script('jqueryui');
				wp_register_script('cch_econtent', 'http://zjtmedia.com/cch_js/cch_econtent_js.js', array('jquery'), '', false);
                wp_enqueue_script('cch_econtent');
                break;
            case 'news-alerts':
                wp_register_script('jquery1.4', 'http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js', array('jquery'), '', false);
                wp_enqueue_script('jquery1.4');
				wp_register_script('jqueryui', 'http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.1/jquery-ui.min.js', array('jquery'), '', false);
                wp_enqueue_script('jqueryui');
				wp_register_script('cch_econtent', '/cch_js/cch_econtent_js.js', array('jquery'), '', false);
                wp_enqueue_script('cch_econtent');
                break;
        }
    } 
}

add_action('wp_enqueue_scripts', 'load_scripts', 92);

It looks like everything is working except for removing jQuery.

Have you tried adding wp_deregister_script( 'jquery'); into the second function along with enqueuing the other jQuery versions?

Also, changing the jQuery version like this usually isn't a great idea - it could cause conflicts with your plugins.

Ah I thought wp_deregister_script needed to be a separate function. I will try it as a single function.

And yes I know changing jquery version is a bad idea. Elementor is disabled on all pages ive tried this on. But client paid a ton for this suite of java plugins and content for his accounting firm and insists it integrates with his current wordpress site

Figured it out! Thanks to your help. Deregistering in the same function worked and made the code a lot cleaner. My problem was I deregistered 'jquery' then registered 'jquery1.4' but my other scripts dependencies were array('jquery').

Here's what worked:

function load_scripts() {
    global $post;

    if( is_page() || is_single() )
    {
        switch($post->post_name) // post_name is the post slug which is more consistent for matching to here
        {
case 'tools':
				wp_deregister_script('jquery');

				wp_register_script('jquery',  'http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js', array(), '', false);
				wp_register_script('jqueryui', 'http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.1/jquery-ui.min.js', array('jquery'), '', false);
				wp_register_script('cch_econtent', 'http://zjtmedia.com/cch_js/cch_econtent_js.js', array('jquery'), '', false);
				
				wp_enqueue_script('jquery');
				wp_enqueue_script('jqueryui');
				wp_enqueue_script('cch_econtent');
                break;

Glad you got it working! :)

This archived topic is closed to new replies.