Smarter WordPress Comment Status
Published on
I love WordPress more and more with each release, and 2.7 takes the cake; I’ve never been a happier designer, developer or user of a CMS/blogging platform. As awesome as it is 95% of the time, I did encounter a setback when developing this site’s theme, specifically in the behavior of comment links.
One of the goals of my site redesign was to open up opportunities for conversation, and journal comments are a significant part of that. As such, I wanted the links to the left of the journal items to behave in the following way:
- If comments are open, always display a link to them or to the comment form.
- If comments are closed, but the post has comments (presumably made before the closure), display the link.
- If comments are closed, and the post has no comments, there isn't any reason to maintain the link; don't show it at all.
I wanted to customize the style and destination of the comment link a bit more than the usual comments_popup_link
function would allow. I took some cues from the default theme and referenced $post->comment_status
, resulting in the following conditional:
if (get_comments_number() || 'open' == $post->comment_status) {
Comment stuff...
}
Unfortunately, this had one flaw; any posts where the comments were closed as a result of WordPress’ “automatically close comments after x number of days” setting retained the comments link no matter what. While $post->comment_status
appeared to work correctly in comments.php
, elsewhere it would still return 'open'
for those posts which had not been implicitly closed.
My solution was to create a function in my theme’s functions.php
file called showCommentLink
, which checks the number of comments, post status and the automatic closure preference before returning a boolean (true for show, false for hide). Take a look:
function showCommentsLink($num,$status,$date) {
// If number of comments is zero, check some other stuff
if ($num <= 0) {
// If comment status is not open, return false
if ($status != 'open') {
return false;
}
// If comment status is open, check if automatic closure is enabled
$close = get_option('close_comments_for_old_posts');
// If so, grab the number of days and compare
if ($close) {
$days = get_option('close_comments_days_old');
$cutoff = strtotime("-$days days");
$then = strtotime($date);
// If this post's comment status is "expired," return false
if ($then < $cutoff) {
return false;
}
}
}
// If you've made it this far, return true
return true;
}
And sample usage in The Loop:
if (showCommentsLink(get_comments_number(),$post->comment_status,$post->post_date)) {
// Comment stuff...
}
While definitely not the prettiest solution in the world, it appears to do the trick. Having $post->comment_status
behave differently in different contexts seems like either an oversight on the part of WordPress developers or, potentially more likely, a sign that I am doing something a bit backwards.
What’s your take? See something I’m doing wrong or have a way to improve the function? Want to see me port it to a plugin for more streamlined use in The Loop? Think all this WordPress stuff is a waste of time when I could use Drupal instead? Let me know in the comments!