Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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' );
        }
    }
share|improve this question

1 Answer 1

up vote 2 down vote accepted

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 );
share|improve this answer
    
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? –  Jose David Garcia Llanos Jun 20 '13 at 1:43
1  
yes, that's correct –  Milo Jun 20 '13 at 1:45
    
Wow, thats awesome, I never knew this! thanks –  Jose David Garcia Llanos Jun 20 '13 at 1:46
    
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 –  Jose David Garcia Llanos Jun 20 '13 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 '13 at 2:12

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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