Only let plugin add actions to wp_head & wp_footer on single posts

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.

Read More

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!

Related posts

1 comment

  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.

Comments are closed.