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.

Updated Aug 15 ’09: Fixed two typos in the final code sample

Updated Jun 13 ’10: Changed action in final code block from get_header to wp_print_scripts. Either action works but wp_print_scripts has a closer relationship with the task being performed than the former.

Updated Jul 29 ’10: Removed == 1 from option check, it’s not needed; merged two if statements in final code block into one.

Props to Digging Into WordPress for the …the right way… terminology:)

About Peter Wilson

Peter Wilson is a Web developer based in Melbourne, Australia, and started making Websites in 1994.

Peter co-founded web production studio Soupgiant in 2009 and forms opinions on all things web at Big Red Tin.

8 Comments

  1. Matt Hill says:

    Very handy. I’ve been transferring my base theme from each new version of WP to the next, and didn’t realise I wasn’t including this!

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

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

    • Peter Wilson says:

      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

  4. kate says:

    what functionality does this actually enable and what if i
    opt not to use this script?

    • Peter Wilson says:

      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.

  5. Albert says:

    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.

    • Peter Wilson says:

      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 );

Leave a Reply

Your email is never shared.