1

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!

3
  • 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() {` Jul 24 '13 at 3:07
  • 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. Jul 25 '13 at 14:23
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.

3
  • 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? 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. Jan 30 '14 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 '14 at 8:31

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.