2

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' );
        }
    }

1 Answer 1

6

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 );
4
  • 1
    When you say "remove" the script, you mean wp_dequeue_script not wp_deregister right? wp_deregister is when you want to change the handle and probably the style/script src? Jun 20, 2013 at 1:43
  • 1
    yes, that's correct
    – Milo
    Jun 20, 2013 at 1:45
  • One final question, the last number "100" what different variations can go in there? 12345 or 10 50 100 etc etc? what is each variation for. thanks Jun 20, 2013 at 1:52
  • 1
    any whole number is valid there. the actions will be executed in order from lowest to highest numbers, and actions that have the same number will be executed in the order they were added.
    – Milo
    Jun 20, 2013 at 2:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.