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')) {

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
  • Modify jal_do_edit() by adding the cache-purge call before the first die() statement:
    // dseah: purge wp-cache file
  • 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.


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!