creating another plugin: reply to from admin panel
Lorelle at Wordpress blogged about a plugin (or WordPress feature) she wants: the ability to reply directly to a comment from the Admin comments page. When I spotted that, I thought I’d give it a go, because it seems like a short and sweet plugin to have. My first concept of the design is to add a Reply To link that sends you directly to the proper spot on the post page to put in the reply. This does not resolve the “backing up” problem to return to the original Admin page; I will address that in a second version. For now the problem can be worked around simply by opening the reply to link in a new tab and then closing that tab when returning to the admin page. Since all the major browsers now have tab functionality, this seems reasonable.
However, as I looked through Wordpress’ structure, I realised that the anchor, or name for the post comment section of a post is dependent on the name that the theme’s author gave it. In other words, we don’t always know what the name/id of the reply form (comment form) is for a post in a given theme. Mine is #postcomment; the popular ones seemed to be #respond or some variation of #postcomment (including a #postComment). So the first step I need to take is to guarantee that, if a comment is allowed for the post in question, a particular anchor name exists. We’ll use this snippet to do that:
function add_admin_comment_reply_anchor() {
<a name="admin_comment_reply_anchor"></a>
}
add_action('comment_form', 'add_admin_comment_reply_anchor');
I played around directly with the wp-admin/edit-comments.php file and confirmed that this works (provided the above anchor name exists):
<a href="<?php echo get_permalink($comment->comment_post_ID); ?>#admin_comment_reply_anchor" title="<?php echo $post_title; ?>"><?php _e('Reply to Post') ?></a>
I tested this by directly editing my edit-comments.php file. Do not do this at home! This is for professionals only!
. My point in doing this was to be sure this approach would even work. There’s roadblocks ahead, though: testing this segment is how I realized that the theme is an issue, needing the above anchor hook. At this point, I’d like to note that it would be easy for WordPress to implement this: all they would need to do is add the automatic anchor as I did, and modify the base edit-comments.php file as above. But since we don’t have that, I’ll proceed with how to do the desired tweaking.
At this point, I looked at two existing plugins: Paged Comment Editing and Commenter Spy. I looked at Paged Comment Editing first since it seemed to be the most similar plugin: a plugin that somehow reworked the edit-comments.php. I was especially hoping to discover a useful WordPress hook to add the Reply To link.
But the more I looked through this plugin the less I liked the overall approach. The problem with it is two fold. First of all it’s directly incompatible with any other plugins that might affect edit-comments.php because it basically takes out edit-comments.php, and regenerates it itself. Second, this approach makes it a potential problem if there’s ever a security patch or even just plain upgrade from WordPress involving this file: the plugin will continue to put out the old code preserved within it. I don’t mean to come off completely criticizing this plugin because it’s very clever with how it reaches in and reworks the file. But I decided I didn’t really want to do it this way.
So it was back to Commenter Spy. This one’s not without its pitfalls either, of course. Since it uses javascript to modify the page output on the fly I had originally rejected it because it won’t work if the user has disabled javascript. However, javascript is ubiquitous enough it seems reasonable to use especially given the alternatives above. The premise of this plugin is as follows: Scan through the DOM of the given page, and do a regular expression substitution in order to sneak in the desired new link. So I put together the following snippets:
Define the desired text (and then escape it; since I’m localizing the term, I have no idea what this might become, so I protect it by escaping the text first).
$admincommenttext = __('View Post');
$eadmincommenttext = str_replace('/', '\/', preg_quote($admincommenttext));
Plus, define the Reply To to add in:
$admincommentreply = __('Reply to Post');
$eadmincommentreply = str_replace('/', '\/', preg_quote($admincommentreply));
Now define the regular expression to search on:
$ViewPostBaseURLEscaped = "(<a[ \\n\\r]*href=\"([^\"]*)\"([^>]*)>$eadmincommenttext<\/a>)";
With these pieces in place, I can now use the following snippet of javascript:
<script type="text/javascript">
//<![CDATA[
var links;
links = document.getElementsByTagName('p');
for (var i = 0; i < links.length; i++) {
var curr;
curr = links[i];
curr.innerHTML = curr.innerHTML.replace(/<?php echo $ViewPostBaseURLEscaped; ?>/, "$1 | <a href=\"$2#admin_comment_reply_anchor\" $3><?php echo $eadmincommentreply; ?></a>");
}
//]]>
</script>
Now, to wrap this all up and to make sure it gets added into the output HTML of the proper page (edit-comments.php), I put the above code into a function called adminCommentReplyModifyViewPostURLs and hooked it in as follows:
/* activate only on correct page */
if (basename($_SERVER['SCRIPT_FILENAME']) === 'edit-comments.php')
{
add_action('admin_footer', 'adminCommentReplyModifyViewPostURLs');
}
In the next version of this plugin, I would like to change the Reply To link to open up an inline text area for the reply which then adds it in properly, without ever leaving the page. However, I need to dig in more deeply to see how comments are added, so I’ve left this for a future improvement. It would also mean not seeing the full content of the comment being replied to although presumably the admin of the site has already seen that in the email notification. (Perhaps a button to expand the excerpt to a full version and back?)
You might be wondering how do I do this, how do I know what filters to apply, where to look. The answer is really I don’t. I do know that I can add in actions and filters, and I peruse the WordPress Codex site’s list of hooks for anything that looks like it might do the trick. I also browse the various files in the themes and in the wp-admin directories: I actually found the the comment form hook in the theme’s comment.php file. I also look for plugins that do something similar to what I want to do, or at least knock about in the same general area. In this case, I found two, one of which gave me the bulk of my material.
Thank god for GPL ![]()
OK, at this point, I’m using it on my installation and it seems to work. It is, however still fairly rough. I’ve got the download page set up here.


