WP deregister, register and enqueue dequeue

a plugin that I bought is loading a few js and css files on all pages, so I wanted to write a new function to deregister the handles, and enqueu them where I wanted. however I am not sure If I have to deregister => register and then enqueue. or do I just dequeue them and enqueue them where I want

// deregister scripts and load them where required
add_action( 'wp_print_scripts', 'my_deregister_javascript', 100 );
function my_deregister_javascript() {
    wp_deregister_script( 'contact-form-7' );
    wp_deregister_script( 'upme_tipsy' );
    wp_deregister_script( 'upme_google_fonts' );
    wp_deregister_script( 'upme_font_awesome' );
    wp_deregister_script( 'upme_css' );
    wp_deregister_script( 'upme_style' );
    wp_deregister_script( 'upme_responsive' );
    wp_deregister_script( 'upme_validate' );
    wp_deregister_script( 'upme_validate_pass' );
    wp_deregister_script( 'upme_password_meter' );
    if ( is_page('contact-us') ) {
        wp_enqueue_script( 'contact-form-7' );
    }
        elseif (is_page(array(680, 671, 675, 688, 'profile'))) {
    wp_enqueue_script( 'upme_tipsy' );
    wp_enqueue_script( 'upme_google_fonts' );
    wp_enqueue_script( 'upme_font_awesome' );
    wp_enqueue_script( 'upme_css' );
    wp_enqueue_script( 'upme_style' );
    wp_enqueue_script( 'upme_responsive' );
    wp_enqueue_script( 'upme_validate' );
    wp_enqueue_script( 'upme_validate_pass' );
    wp_enqueue_script( 'upme_password_meter' );
        }
    }

Related posts

1 comment

  1. You should be able to just dequeue the script or style if it’s not the pages you want it on.

    function wpa_scripts() {
        // if this is not the contact page, remove the script
        if( ! is_page( 'contact-us' ) ){
            wp_dequeue_script( 'contact-form-7' );
        }
    }
    // adjust priority to make sure this runs after the plugins add their scripts/styles
    add_action( 'wp_enqueue_scripts', 'wpa_scripts', 100 );
    

Comments are closed.