WPTavern has this cool thing where, without threaded comments, you still have a reply link, AND it creates an automatic link back to the original comment.
Let me rewind. If you have threaded comments, then you get a ‘reply’ link on the bottom of a comment, and it lets you make a threaded reply. Yay! There are problems with this, though, as after a while, if you get nested deep enough, you can’t reply under anymore, and have to go up to click reply on the previous post and on and on.
Back eons ago, WPTavern solved this with a function, and like all great sites documented it! The problem? The documentation was busted. Now, yes, I did ping Jeff about this and asked him how it went, but in the meantime, I was impatient and looked up a plugin that almost fit my bill.
Enter @ Reply (aka Reply To), which add in a reply link to all comments! Plus is gives you ‘Twitter like’ @replies, so when you comment, it starts “@foo:” automatically. This is just like what WPTavern has, perfect! Except… not quite.
My problems became two:
- I want all comments to have a ‘reply’ link.
- I don’t want the hover over image.
And I solved this with two code chunks: a plugin and a theme.
See, by default WP stops showing you the ‘reply’ link when you can’t nest anymore. To change that, you have to edit how your theme calls comments. Or rather, you have to change the comment_reply_link() call.
The Theme Code
I’m already customizing my comments in Genesis so this was surprisingly simple.
What was this:
<div class="comment-reply"> <?php comment_reply_link( array_merge( $args, array( 'depth' => $depth, 'max_depth' => $args['max_depth'] ) ) ); ?> </div>
Becomes this:
<div class="comment-reply"> <?php comment_reply_link( array_merge( $args, array( 'depth' => 1, 'max_depth' => 2 ) ) ); ?> </div>
Basically we’re lying to WP and saying we always show the link. I got that idea from this stackexchange post, and as I understand it, we’re tricking WP by saying that every post is depth of 1, and has a max of 2, instead of letting it figure it out on it’s own. This is probably inelegant, but I couldn’t find another way to have the reply link always on.
The Plugin Code
This was easier, in that I took the existing code, cleaned it up and removed the reply image, and that was pretty much it. Downside is that this does not work on every site. Notably, it doesn’t work unless threaded comments are turned on. When they’re not, it does a double-refresh.
That said, this is probably never going to be a plugin for the masses, so I’m content with having it work this way for me. Even if I only thread one comment at a time, it would let me group them together and get the @-reply. It’s already rather popular on the site I intended it for, and people like it.
So here’s the code:
class AtReplyTwoHELF { public function __construct() { add_action( 'init', array( &$this, 'init' ) ); } public function init() { if (!is_admin()) { add_action('comment_form', array( $this, 'r2_reply_js')); add_filter('comment_reply_link', array( $this,'r2_reply')); } } public function r2_reply_js() { ?> <script type="text/javascript"> //<![CDATA[ function r2_replyTwo(commentID, author) { var inReplyTo = '@<a href="' + commentID + '">' + author + '<\/a>: '; var myField; if (document.getElementById('comment') && document.getElementById('comment').type == 'textarea') { myField = document.getElementById('comment'); } else { return false; } if (document.selection) { myField.focus(); sel = document.selection.createRange(); sel.text = inReplyTo; myField.focus(); } else if (myField.selectionStart || myField.selectionStart == '0') { var startPos = myField.selectionStart; var endPos = myField.selectionEnd; var cursorPos = endPos; myField.value = myField.value.substring(0, startPos) + inReplyTo + myField.value.substring(endPos, myField.value.length); cursorPos += inReplyTo.length; myField.focus(); myField.selectionStart = cursorPos; myField.selectionEnd = cursorPos; } else { myField.value += inReplyTo; myField.focus(); } } //]]> </script> <?php } public function r2_reply($reply_link) { $comment_ID = '#comment-' . get_comment_ID(); $comment_author = esc_html(get_comment_author()); $r2_reply_link = 'onclick=\'return r2_replyTwo("' . $comment_ID . '", "' . $comment_author . '"),'; return str_replace("onclick='return", "$r2_reply_link", $reply_link); } } new AtReplyTwoHELF();
Most of the javascript stuff is copy/pasta for me (just left of witchcraft), and I left it barebones (no configurations) on purpose. Simple is as simple does.
Suggestions and tweaks welcome!