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.

del.icio.us:creating another plugin: reply to from admin panel  digg:creating another plugin: reply to from admin panel

24 Comments »

  1. Lorelle said,

    March 20, 2007 @ 5:57 pm

    * Slathering and drooling *

    >>>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…<<<

    I was hoping with AJAX this process of inline replying would be easier, but I knew when I put this out there that this would be a challenge, and now you know why. Kudos on you for for attacking this project. It is much needed - desperately needed. We need a way of responding to comments fast and easy.

    WordPress Hooks from Flat Earth http://wphooks.flatearth.org/ and, hmm, there’s another list…and I can’t find it. I know I found it searching “wordpress hooks” or “wordpress functions” on Google, but I can’t find it right now. It’s buried in development text records… I keep looking.

    Thanks for taking on this challenge. You are my hero!

  2. Lorelle said,

    March 20, 2007 @ 9:26 pm

    I found a couple of resources, though not the one I was looking for to help you find all the WordPress hooks. The Flat Earth WordPress Hooks list was the most updated for a long time. I don’t know if it still is. However, these may help.

    WordPress 2.x Hooks for Action - Comprehensive List for Plugin and Theme Developers

    WordPress 2.x Filter List for Plugin and Theme Developers

  3. Building a Better WordPress Comments Panel « Lorelle on WordPress said,

    March 21, 2007 @ 8:08 am

    […] a Better WordPress Comments Panel Cindy of Digital Ramble wants to build a better WordPress comments panel based upon a plea/whine/beg I made for WordPress Plugins I believe WordPress needs and I covet […]

  4.   Building a Better WordPress Comments Panel by Blogging Pro said,

    March 21, 2007 @ 11:35 am

    […] to take up her quest on making a better WordPress Comments area in the administration panel. Cindy of Digital Ramble has put up a post regarding the progress made so […]

  5. Jim said,

    March 21, 2007 @ 11:45 am

    None too shabby. I’ve wished for something like this for a while now. The inline AJAX addition would definitely be even sweeter.

  6. Aaron said,

    March 21, 2007 @ 1:44 pm

    Inserting the comment into the database is fairly easy. Just let Wordpress do all the work for you:
    This is the code I use for my Inline Ajax Page plugin.

     
      // this line is WordPress' motor, do not delete it.
      $comment_author = (isset($_COOKIE['comment_author_' . COOKIEHASH])) ? trim($_COOKIE['comment_author_'. COOKIEHASH]) : '';
      $comment_author_email = (isset($_COOKIE['comment_author_email_'. COOKIEHASH])) ? trim($_COOKIE['comment_author_email_'. COOKIEHASH]) : '';
      $comment_author_url = (isset($_COOKIE['comment_author_url_'. COOKIEHASH])) ? trim($_COOKIE['comment_author_url_'. COOKIEHASH]) : '';
      if ('open' == $post-&gt;comment_status) {
     
      include ("inap-add-comments.php");
     
        }
       }elseif($_POST['type'] == "submit_form"){
     
      nocache_headers();
      $comment_post_ID = (int) $_POST['comment_post_ID'];
     
      $status = $wpdb-&gt;get_row("SELECT post_status, comment_status FROM $wpdb-&gt;posts WHERE ID = '$comment_post_ID'");
     
      if ( empty($status-&gt;comment_status) ) {
        do_action('comment_id_not_found', $comment_post_ID);
        exit;
      } elseif ( 'closed' ==  $status-&gt;comment_status ) {
        do_action('comment_closed', $comment_post_ID);
        wp_die( __('Sorry, comments are closed for this item.') );
      } elseif ( 'draft' == $status-&gt;post_status ) {
        do_action('comment_on_draft', $comment_post_ID);
        exit;
      }
     
      $comment_author       = trim($_POST['author']);
      $comment_author_email = trim($_POST['email']);
      $comment_author_url   = trim($_POST['url']);
      $comment_content      = trim($_POST['comment']);
     
      // If the user is logged in
      $user = wp_get_current_user();
      if ( $user-&gt;ID ) :
        $comment_author       = $wpdb-&gt;escape($user-&gt;display_name);
        $comment_author_email = $wpdb-&gt;escape($user-&gt;user_email);
        $comment_author_url   = $wpdb-&gt;escape($user-&gt;user_url);
      else :
        if ( get_option('comment_registration') )
          wp_die( __('Sorry, you must be logged in to post a comment.') );
      endif;
     
      $comment_type = '';
     
      if ( get_settings('require_name_email') &amp;&amp; !$user-&gt;ID ) {
        if ( 6 &gt; strlen($comment_author_email) || '' == $comment_author )
          wp_die( __('Error: please fill the required fields (name, email).') );
        elseif ( !is_email($comment_author_email))
          wp_die( __('Error: please enter a valid email address.') );
      }
     
      if ( '' == $comment_content )
        wp_die( __('Error: please type a comment.') );
     
      $commentdata = compact('comment_post_ID', 'comment_author', 'comment_author_email', 'comment_author_url', 'comment_content', 'comment_type', 'user_ID');
      $comment_id = wp_new_comment( $commentdata );
     
      if ( !$user-&gt;ID ) :
        $comment = get_comment($comment_id);
        setcookie('comment_author_' . COOKIEHASH, $comment-&gt;comment_author, time() + 30000000, COOKIEPATH, COOKIE_DOMAIN);
        setcookie('comment_author_email_' . COOKIEHASH, $comment-&gt;comment_author_email, time() + 30000000, COOKIEPATH, COOKIE_DOMAIN);
        setcookie('comment_author_url_' . COOKIEHASH, clean_url($comment-&gt;comment_author_url), time() + 30000000, COOKIEPATH, COOKIE_DOMAIN);
      endif;

    Of course since only the Admin is going to be submitting comments a better version would be:


      nocache_headers();
      $comment_post_ID = (int) $_POST['comment_post_ID'];
     
      $status = $wpdb-&gt;get_row("SELECT post_status, comment_status FROM $wpdb-&gt;posts WHERE ID = '$comment_post_ID'");
     
      if ( empty($status-&gt;comment_status) ) {
        do_action('comment_id_not_found', $comment_post_ID);
        exit;
      }
      $comment_content = trim($_POST['comment']);
      // If the user is logged in
      $user = wp_get_current_user();
     
      if ( $user-&gt;ID ) :
        if( current_user_can('moderate_comments')){
          $comment_author       = $wpdb-&gt;escape($user-&gt;display_name);
          $comment_author_email = $wpdb-&gt;escape($user-&gt;user_email);
          $comment_author_url   = $wpdb-&gt;escape($user-&gt;user_url);
        }else{
          wp_die( __('Sorry, you do not have the correct permission level to post a comment.') );
        }
      else :
        wp_die( __('Sorry, you must be logged in to post a comment.') );
      endif;
      
     
      $comment_type = '';
     
      if ( '' == $comment_content )
        wp_die( __('Error: please type a comment.') );
     
      $commentdata = compact('comment_post_ID', 'comment_author', 'comment_author_email', 'comment_author_url', 'comment_content', 'comment_type', 'user_ID');
      $comment_id = wp_new_comment( $commentdata );

    The latter code will allow anyone with the ability to moderate comments to post onto any post regardless of status, and limits the use of the form only to those same people when they are already logged in. This will prevent the plugin from opening security holes.

  7. Cindy said,

    March 21, 2007 @ 2:11 pm

    Right, that will go into the hook on the submit handler. It’s coming along :-) Thanks!

  8. Alister Cameron // Blogologist said,

    March 21, 2007 @ 11:14 pm

    REALLY looking forward to this, Cindy. It’s kind of amazing that this is not in core… but you could say that about a lot of things, I suppose.

    - Alister

  9. Brandon Philips said,

    March 21, 2007 @ 11:30 pm

    Is this an AJAX comment?

  10. WordPress Plugins: Code Marathon Party and Plugins I Wanted Released « Lorelle on WordPress said,

    March 24, 2007 @ 12:04 am

    […] Cindy of Digital Ramble is working to build a better WordPress comments panel with her new Admin Panel Comment Reply WordPress Plugin. The idea is to enable replying to comments by the blog administrator/owner from within the WordPress Administration Comments Panel, not from the post itself. This should speed up the response process and save a few bandwidth chunks and database hits. […]

  11. Weekly Digest: Introduction, ProBlogger, Blue Collar Bloggers, PayPerPost, and More « Lorelle on WordPress said,

    March 24, 2007 @ 8:41 am

    […] very excited about the work of Cindy of Digital Ramble helping to build a better WordPress comments panel. In Wanted: WordPress Plugins For Me, I pleaded […]

  12. Jenny said,

    March 24, 2007 @ 12:01 pm

    I don’t see any kind of reply area. Do I have to modify any files?

  13. Cindy said,

    March 28, 2007 @ 7:13 am

    I’m emailing you privately for more info…

  14. PLUS.DSHOCK » Blog Archive » Building a Better WordPress Comments Panel said,

    April 5, 2007 @ 3:05 am

    […] to take up her quest on making a better WordPress Comments area in the administration panel. Cindy of Digital Ramble has put up a post regarding the progress made so […]

  15. WordPress Wednesday News: Security Release, Hot Tips for WordPress Bloggers, and a WordPress Plugin for Your Next of Kin at The Blog Herald said,

    April 11, 2007 @ 4:03 pm

    […] Reply Comments WordPress Plugin: The Reply To From Admin Panel WordPress Plugin will revolutionize your ability to quickly reply to comments from the WordPress Comments panel […]

  16. WordPress Wednesday News: WordPress 2.2 Due Monday, Sponsored WordPress Themes, WordPress.com Splog Fighter, and More at The Blog Herald said,

    April 18, 2007 @ 3:09 pm

    […] Reply Comments WordPress Plugin: The Reply To From Admin Panel WordPress Plugin will revolutionize your ability to quickly reply to comments from the WordPress Comments panel […]

  17. Brian Layman said,

    April 25, 2007 @ 3:52 am

    Congrats on making the WordPress PodCast!

    There’s a nice section all about you! (I get a quick mention too.)

    http://wp-community.org/2007/04/24/episode-23-wordpress-22-delayed-gsoc-workers-aaron-brazell/

  18. WordPress Wednesday News: WordPress 2.2 Released, Top Bloggers Blogging With WordPress, More WordPress Videos, and WordPress News : The Blog Herald said,

    May 17, 2007 @ 1:31 pm

    […] WordPress Comments: While Reply To From Admin Panel WordPress Plugin by Digital Ramble is still in development to make replying from your WordPress Administration […]

  19. Bucky said,

    August 22, 2007 @ 5:21 am

    I have tried both versions of this plugin, but neither work for me. I installed the first one and activated it. Nothing new in the comments section of my dashboard.

    I installed the next version, and the same….nothing new in the comments section.

  20. The Big Chorizo said,

    August 24, 2007 @ 1:28 am

    Hi Cindy, thanks for a great plugin! It was just what I was looking for this morning. One of the things I love about Wordpress is that there are so many very clever people working on extras who have almost always thought of things before you need them :-) This one will save me lots of time, which is the most precious gift someone can give.

    The plugin works great for me, and i don’t know if you have changed it since these comments but I’m using it with Wordpress 2.2 and it open a comment box for me inline on the admin comments page and puts my comment back at the top of the list straight away.

  21. Graham said,

    August 24, 2007 @ 8:21 am

    Hi Cindy, I’m so pleased that someone has started to develop this plugin because it’s long overdue and I can’t wait to use it for my site but unfortunately I am having similar problems to Jenny as I cannot see anything to let me reply. If you could let me know how this can be remedied, I’d really appreciate it. Keep up the good work! Thanks, Graham.

  22. www.r10.net küresel ısınmaya hayır seo yarışması said,

    September 7, 2007 @ 2:10 pm

    thanks

  23. carl said,

    September 15, 2007 @ 8:52 am

    I upgraded to 2.2 and the plugin no longer works no matter how many times i install it.

    Loved using it before though.

  24. hiutopor said,

    September 16, 2007 @ 10:02 pm

    Hi

    Very interesting information! Thanks!

    G’night

RSS feed for comments on this post · TrackBack URI

Leave a Comment

Bad Behavior has blocked 953 access attempts in the last 7 days.