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 using a a plugin (Comments Evolved) on a recently built site.

The issue I'm having is that this plugin globally enqueueing and adding actions to the head and footer of all pages. I'd like to remove the actions except for single posts only.

I tried this in my functions.php file:

    // strip out the plugin junk slowing down pages it's not used on
    function strip_the_junk() {
        if (!is_single() {
            remove_action('wp_head', 'gplus_comments_enqueue_styles');
            remove_action('wp_footer', 'gplus_comments_enqueue_scripts');
        }
    )};
    add_action('wp_enqueue_scripts', 'strip_the_junk', 11);

That crashes my site. For Comments Evolved, this is what's being pumped in by hook.php:

    function gplus_comments_enqueue_styles()
    {
      wp_enqueue_style('gplus_comments_tabs_css');
    }
    add_action('wp_head', 'gplus_comments_enqueue_styles', 4269);

    function gplus_comments_enqueue_scripts()
    {
      print "\n<script>jQuery('#comment-tabs').tabs();</script>\n";
    /*
    <script type="text/javascript">
      (function() {
       var po = document.createElement('script'); po.type = 'text/javascript'; po.async = true;
       po.src = 'https://apis.google.com/js/client:plusone.js';
       var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(po, s);
      })();
    </script>
    */
    }
    add_action('wp_footer', 'gplus_comments_enqueue_scripts', 4269);

Any thoughts on where I'm going wrong with this?

Thanks!

share|improve this question
3  
enable debugging and fix your php errors. –  Milo Jul 24 '13 at 0:15
    
Agree with Milo. I can see you are missing a parens ` if (!is_single() {` –  helgatheviking Jul 24 '13 at 3:07
    
thanks for the heads up, will adjust –  Tiger Danky Jul 24 '13 at 5:00
    
I cleaned up the parenthesis, but still can't seem to remove the injected code. I tried @JPollock's add_action but not sure if and where I need to add it in functions.php. –  Tiger Danky Jul 25 '13 at 14:23

1 Answer 1

You need to remove the actions before they are called. In order to ensure this is the case call your function at init, like so:

add_action('init', 'strip_the_junk');

In general, its not a great idea to use the wp_enqueue_scripts hook for anything besides wp_enqueue_script or wp_enqueue_style functions.

share|improve this answer
    
Thanks @JPollock, I cleaned my code and added the add_action call instead, but the content from the plugin is still being inserted. I had the function and the call at the end of my functions.php file, tried moving it to the top. Is there any specific order I need to do for it? –  Tiger Danky Jul 25 '13 at 14:24
    
I think he needs to run the removal "after" the additions are run, so he'd need to hook onto a priority above 4269 as higher priorities are later in execution. –  Andrew Bartel Jan 30 at 23:18
    
From the codex: "It is also worth noting that you may need to prioritise the removal of the action to a hook that occurs after the action is added. You cannot successfully remove the action before it has been added." –  Nick Young May 15 at 8:31

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.