Filter out some plugin action in wp head / wp_footer

I am using a plugin called “syntaxhighlighter”.

I know not to ask for plugin specific questions but rather how to filter out action added to the wp_head / wp_footer conditionally…

Read More

This is a part of the plugin script:

// Outputting SyntaxHighlighter's JS and CSS
add_action( 'wp_head', array( $this, 'output_header_placeholder' ), 15 );
add_action( 'wp_footer', array( $this, 'maybe_output_scripts' ), 15 );

Now, i would like to add a filter that says:

if(is_home() || is_category()) {
   // REMOVE THOSE SCRIPS
}

.
I dont want to change that plugin code becouse i would have
to remmember to do it again when it updates…

How can i filter those actions out when needed?

Related posts

1 comment

  1. You can try this (untested):

    add_action( 'wp_head', 
       function(){
           // your conditions:
           if( is_home() || is_category() )
           {
               // access the global SyntaxHighlighter object instantiated at 'init'.
               global $SyntaxHighlighter;
    
               // remove your action hooks:
               remove_action( 'wp_head',    
                               array( $SyntaxHighlighter, 'output_header_placeholder' ),
                               15 );
               remove_action( 'wp_footer', 
                               array( $SyntaxHighlighter, 'maybe_output_scripts' ),
                               15 );
           }
       }
    );
    

    to remove these action hooks conditionally with template tags. We use the wp_head action with the default priority 10.

    You can use other hooks but they must fire earlier than wp_head with priority 15 and after the $SyntaxHighlighter object creation via the init hook.

    You must also make sure that your template tags, you want to use in your conditional checks, are available in the hook you choose.

Comments are closed.