In Page or Post header: Test for comments so I can deregister unnecessary scripts

I’m working hard to shoo away the bloat on my WordPress theme, and unregistering (wp_deregister_script) plugins that need not be included is a priority.

I’ve got a comment editing script that need only be included sometimes (but always for logged in users — just me)

Read More

My first attempt (in the header):

  if (!is_user_logged_in() AND !comments_open()) {
    wp_deregister_script('dialog');
    wp_deregister_script('editableComments');
  }

Now this is adequate, but the script still remains included unnecessarily for users who are quickly visiting a page or post and have nothing to edit.

I could use your brain power…

The visitor comment conditions are:

  • Has just submitted a comment (the page will refresh)
  • The visitor left comment, navigated away and returned to the page to edit

I’m removing the time conditions: as long as the comment is unmoderated it can be edited (quickly done by hiding the edit link if comment approved).

I was thinking…

Is there a way to test for comment submission page refresh (referrer — or not sure if any POST data makes it back to the page)?

Is there a quick way to test if any comments on the page/post are awaiting moderation.

Both of those working together would probably be good enough.

Thoughts?

Related posts

Leave a Reply

1 comment

  1. I believe I’ve crafted a solution to place in the header.php

    $comments_count = wp_count_comments($post->ID);
    if (!is_user_logged_in() AND ($post->comment_status == 'closed' OR $comments_count->moderated == 0)) {
      // if the visitor isn't logged in, and the comments are closed or there are no comments in moderation:
      wp_deregister_script('dialog');
      wp_deregister_script('editableComments');
    }
    

    This should certainly work well enough, so long as I continue to keep the number of comments in moderation low (as any moderated comments for that particular post or page means all visitors will get the scripts included, not just the commenter).

    Relevant: http://codex.wordpress.org/Function_Reference/wp_count_comments