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

  • 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!
    – ptim
    Nov 14 '11 at 2:50

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' );
  • 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)
    – ptim
    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

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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