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.
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!
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)
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.
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
what functionality does this actually enable and what if i
opt not to use this script?
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.
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.
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: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? :)
thank you very much, it served me well. Although the best way is to place it within a ‘hook’