Editable Comments and WP Cache 2.0

Editable Comments and WP Cache 2.0

I’ve been making various improvements to my WordPress installation, hopefully making it a little easier to play here on Better Living through New Media. One long-standing peeve was the lack of user-editable comments. I had seen Andrew Sutherland’s Edit Comments Plugin before, but had put off installing it because the post warned of the “complex modifications” needed to your theme files. As it turns out it actually isn’t bad, but I had to fix a compatibility issue with WP Cache 2.0 to get it to work right on my install of WordPress 2.0.3.

Geeky notes follow.

The Installation

As I said, I found the mods actually aren’t too bad if you understand PHP and WordPress at an intermediate level. There are basically only two steps:

  1. Upload the plugin and activate it in WordPress’ plugin administration window.

  2. Edit your comments.php file. The variable is how your particular theme implements that file, so it is potentially confusing to those unfamiliar with WordPress internals. The readme.txt file that Andrew includes is very clear on what you need to edit. It’s a delight to come across a technical document that establishes both “what you need to do” with “what it does” and “how it works”. Excellent! There are about three things you need to change in the comments.php file, and it’s actually pretty straightforward. I put this all off for nothing!

There was one wrinkle, of course, otherwise I wouldn’t be writing this. WP Cache 2.0, the cache plugin I use to lighten the load on my shared host, caches the comment so your edits don’t show up after you’re done. They are saved, but you can’t see them until the cache expires or you edit again. Annoying.

The Modifications

First, I went to my WP Cache Options in the WordPress Admin panel, and added jal_edit_comments to the REJECTED URIS list. Save the strings. This prevents any page that uses the Edit Comment Plugin’s method for communicating with WordPress (a GET value called jal_edit_comments) from being cached. It occurs only when someone clicks the “edit” link for an editable comment. Second, I modified my copy of jal-edit-comments.php to detect WP-Cache 2.0 and delete the associated cache file for the current post being commented on. As far as I can tell it works. I added the following function:

// if wpcache is installed, delete cache file for this page
// .. added by david seah 07/14/06
// .. called by jal_edit_comment_init(), jal_do_edit()

function jal_purge_wpcache() {

  if (function_exists('wp_cache_get_cookies_values')) {
    // import wp-cache configuration
    if (@include(ABSPATH . 'wp-content/wp-cache-config.php') ) {
      // strip out everything after the ? in the uri
      $uri = preg_replace('/?.*$/', '',$_SERVER['REQUEST_URI']);
      // create wp-cache 2.0.x compatible filename
      $key = md5( $uri . wp_cache_get_cookies_values() );
      $cache_filename = $file_prefix . $key;
      // delete the cached file
      if (function_exists('wp_cache_clean_cache')) {
        wp_cache_clean_cache($cache_filename);
      }
    }
  }

}
Now you have to modify two other functions in jal-edit-comments.php:
  • Modify jal_edit_comment_init() by adding the following right after the first echo statement:
    echo '<p><input type="hidden" name=...
    // dseah: purge wp-cache file
    jal_purge_wpcache();
    
  • Modify jal_do_edit() by adding the cache-purge call before the first die() statement:
    // dseah: purge wp-cache file
    jal_purge_wpcache();
    die();
    
  • Now purge all the cached files using WP Cache’s option panel, and you should be set.

What’s Supposed to Happen

WP Cache 2.0 creates a unique filename for every web page it caches, and stores that in a special cache folder. The modifications I made to EditComments recreates that unique filename, and passes it to WP Cache’s “Delete Cache File” function at two critical moments:

  • Clicking the “edit” link adds a special GET variable called jal_edit_comments. This is intercepted by jal_edit_comment_init(), which now also calls our new function to purge the cached file. This makes sure that if there’s a timeout, the cached page is nuked, otherwise the “edit” link will still be served up.

  • Clicking the “submit” button invokes jal_do_edit (), which steals the comment text away from the usual comment handler (wp-comments-post.php), handling the database insertion itself. After that’s done, it redirects the browser back to the comment page before terminating the script; however, before we do that, we purge the cached page again, because we want to show the updated text (the edited comment) instead of the cached version (which has the pre-edited comment)

<

p>So that’s what I think I’m doing, anyway. It appears to work on my installation of WordPress 2.03, WP Cache 2.0.17, and Edit Comments 0.3 Beta. There may be a situation where some of the “You aren’t allowed to edit this comment” messages won’t show up if multiple people are accessing the same page and therefore thrashing the cache out from under each other, but we’ll see what happens.

Availability

I thought about making my modified version available, but I’ll just let the author of the original plugin know after I do a bit more testing. I don’t want to create a technical support headache with two versions of the same plugin, with him having to support my possibly-buggy code. And if you figured out how to install WP Cache and Editable Comments in the first place, you probably can apply these modifications yourself. Good luck!

6 Comments

  1. Dave Seah 18 years ago

    Testing…Yay, I got the regular editing box!
    But now I’m logged out, and can edit this comment based on my IP address. Hmm. I guess that’s the known weakness of Editable Comments as it’s currently implemented
    Yup, I am as an anonymous user able to edit the administrator’s comment. Hmm. This is not good. Will modify my comments.php further to prevent this.

    ——-

  2. Dave Test 18 years ago

    Now I’m posting a comment as a test user, logged out…I see my comment is now awaiting moderation, so I added this sentence.

    Good, that worked. Now for the hell of it I’ll see if I can edit it from another machine behind my IP address that’s being NAT’d. Nope, I don’t get that. The WordPress cookie might be unique even for anonymous users?

    Hmm, wp-cache isn’t caching reloads. Oh, duh, the post-slug has wp- in the URI string…fixing that.

  3. Dave Seah 18 years ago

    This should not be editable now since I added a simple registered user check, but it’s a hassle. This certainly isn’t ideal.

  4. _ck_ 18 years ago

    Nice work and very timely for me.
    I modified jal’s edit comments a little so I had to modify your routine slightly as well.

    Jal’s version relies on referers to return the visitor back to the right page which is a very bad idea since alot of people block them these days. Instead I let WP figure out where to send them based on looking up the commentID’s postID.

        $post_ID=$comment->comment_post_ID;
    $location =  get_permalink($post_ID);
          jal_purge_wpcache($location);
          $location.=”#comment-”.$comment_post_ID;
          wp_redirect( $location );

    and then of course I made your routine accept the passed location


    function jal_purge_wpcache($location=’‘) {

    and

    if (!$location) $location=$_SERVER[‘REQUEST_URI’];

    Seems to work now so I think I did that right.

    Thanks again for sharing that!

    As soon as I get the paged comments working (it’s giving me a nasty fatal error now) I think I will need to update that since it adds more path to the url.

  5. _ck_ 18 years ago

    Looks like you also need to add the plugin to notify users of a moderated comment, I had no idea if my previous post made it or not…

    http://txfx.net/2004/10/29/wordpress-hack-notify-users-of-moderation/

  6. Theron Parlin 18 years ago

    You might be interested to know that I updated Andrew’s edit comments plugin to work like the comments at DIGG.com. You can check it out here:

    http://www.theronparlin.com/2006/07/24/edit-comments-10/

    Since you’ve already made the mods to your comments.php file, you can probably just replace the plugin with mine and it will work…