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.

I'm trying to use the method presented here:

Restricting a Plugin to Only Load its CSS and JS on Selected Pages?

In the plugin 'syntax-highlighter-compress' I've got the following code:

function wp_shc_head_scripts() {    
    wp_register_script( 'shCore',       plugins_url('syntax-highlighter-compress/scripts/shCore.js') );
    wp_register_script( 'shAutoloader', plugins_url('syntax-highlighter-compress/scripts/shAutoloader.js') );
    wp_enqueue_script('shCore');
    wp_enqueue_script('shAutoloader');
}
add_action('wp_print_scripts', 'wp_shc_head_scripts');

And in functions.php I have:

function remove_shc() {
    remove_action('wp_print_scripts', 'wp_shc_head_scripts');
}

if ( is_single( array( 17, 19, 1, 11 ) ) ) {
    add_action('wp_print_scripts', 'remove_shc');
}

I've tried various priorites after each add/remove and have tried various hooks (wp_head, shutdown, init) but I can't seem to get this to work!

(Whilst testing, I have the is_single() condition commented out so that the plugin should never load, but it always does.)

What am I missing? Thanks, Tim

(In future, I'll prob use get a post_meta value to trigger plugin activation, ideally, the plugin should set this on use.. but first things first, eh?)

share|improve this question
    
You forgot the 'if' in your conditional - assuming that's just a typo. –  anu Nov 10 '11 at 7:48
    
yup - typo - i've abbreviated the code for the example - cheers! –  memeLab Nov 14 '11 at 2:50

1 Answer 1

up vote 0 down vote accepted

You have two issues here:

  1. Conditional tags (is_single()) should not be used so early in functions.php.
  2. Your removal function is getting hooked to run after function it's meant to remove.

It should be something like this:

function remove_shc() {

    if ( is_single( array( 17, 19, 1, 11 ) ) )
        remove_action( 'wp_print_scripts', 'wp_shc_head_scripts' );
}

add_action( 'wp_enqueue_scripts', 'remove_shc' );
share|improve this answer
    
great! Your code solved the posted issue.. but i haven't quite understood why.. the other hook used is add_action('wp_footer','wp_shc_footer'); and I don't understand at which point I should remove this action.. (I tried removing it at wp_enqueue_scripts and it didn't seem to work) –  memeLab Nov 14 '11 at 4:11
    
@ptim that depends on when is it added, you can't remove something before it is there –  Rarst Nov 14 '11 at 9:40

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.