Including WordPress’s comment-reply.js (the right way)

Since threaded comments were enabled in WordPress 2.7, most themes include the following line in header.php

<?php if ( is_singular() ) wp_enqueue_script( 'comment-reply' ); ?>

This code checks if the visitor is browsing either a page or a post and adds the JavaScript required for threaded comments if they are.

I prefer a slight variation

<?php
if ( is_singular() && comments_open() && get_option('thread_comments') )
  wp_enqueue_script( 'comment-reply' );
?>

My variation checks if the visitor is browsing either a page or a post, if comments are open for the entry, and finally, if threaded comments are enabled. If all of these conditions are met, the JavaScript required for threaded comments is added.

If you run your wp_enqueue_script calls in functions.php, as I do, this is the code to use:

<?php
function theme_queue_js(){
if ( (!is_admin()) && is_singular() && comments_open() && get_option('thread_comments') )
  wp_enqueue_script( 'comment-reply' );
}
add_action('wp_print_scripts', 'theme_queue_js');
?>

The call is added to the wp_print_scripts action as is_singular and comments_open are unknown during the init action.

 

Note: I’ve written a plugin to make the comment-reply JavaScript unobtrusive, it’s call Rapid Comment Reply.

By Peter Wilson

Peter has worked on the web for twenty years on everything from table based layouts in the 90s to enterprise grade CMS development. Peter’s a big fan of musical theatre and often encourages his industry colleagues to join him for a show or two in New York or in the West End.

10 comments

  1. Hi Peter,

    Thanks a lot for your tips. I just started using WordPress for my own site and I’m glad that there is useful information like yours out there.

    Bye, Christine (aks @stinie)

  2. This is really helpful, but when i while my source on browser with firebug it will not show up until i remove “(get_option(‘thread_comments’) == 1)” statement.

    I am using it without “(get_option(‘thread_comments’) == 1)” statement and it’s working for

    Thanks.

    1. The (get_option(‘thread_comments’) == 1) tests if the enable threaded comments checkbox in discussion settings is on. In the situation you describe, it’s likely threaded comments are disabled (or WordPress erroneously thinks they are).

      Without threaded comments enabled comment-reply.js is not required.

      Pete

    1. Without the JavaScript, a reload is required when a visitor clicks an earlier comment’s reply link. By including the JavaScript, no reload is required.

  3. Instead of:

    if ( is_singular()

    one should use:

    if ( is_single()

    otherwise the script will be called on all pages, which defeats the purpose. Took me a while to figure this out, but better late than never.

    1. I’ve used if_singular as WordPress allows comments on the default page & post content types. They are also permitted on custom content types.

      Using if_single will not insert the script on pages with comments enabled.

      I’ve seen themes disable comments for pages by leaving out the call to comments_template() in the page template. In addition a filter should be set up to indicate comments are closed to the WordPress code base:

      function noPgComments($open,$post_id) {
        if (get_post_type($post_id) == 'page') {
          $open = false;
        }
        return $open;
      }
      
      add_filter( 'comments_open', 'noPgComments', 10, 2 );
  4. This looks so easy….if you’re using the basic themes..
    I am using Theron Lite theme to be specific, and I don’t really have a clue on where to start.
    Is anyone in the mood of helping? :)

Comments are closed.