0

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?)

2
  • You forgot the 'if' in your conditional - assuming that's just a typo. Commented Nov 10, 2011 at 7:48
  • yup - typo - i've abbreviated the code for the example - cheers! Commented Nov 14, 2011 at 2:50

1 Answer 1

0

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' );
2
  • 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) Commented Nov 14, 2011 at 4:11
  • @ptim that depends on when is it added, you can't remove something before it is there Commented Nov 14, 2011 at 9:40

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.