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


8 Comments
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!
September 16, 2009 at 3:31 am
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)
July 18, 2010 at 1:45 am
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.
August 7, 2010 at 11:59 pm
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
August 8, 2010 at 6:40 pm
what functionality does this actually enable and what if i
opt not to use this script?
January 6, 2011 at 5:11 am
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.
January 6, 2011 at 3:13 pm
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.
June 27, 2011 at 11:04 am
I’ve used
if_singularas WordPress allows comments on the default page & post content types. They are also permitted on custom content types.Using
if_singlewill 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:June 27, 2011 at 12:41 pm