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

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:

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.

Published by Peter Wilson

Peter Wilson is a Senior WordPress Engineer at Human Made and contributor to WordPress core. 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 theater and often encourages his WordPress community colleagues to join him for a show or two in New York or in the West End.

10 replies on “Including WordPress’s comment-reply.js (the right way)”

  1. 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


    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.


  2. 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 );
  3. 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.