Replacing Data in Comments

I noticed today that I had a bunch of markup in my comments. I had been wondering why I had all these <p>, </p> and <br /> tags in there. Turns out I had neglected to set the convert_breaks=”0″ attribute when I redid my page. So it stored all the break information within the comment. This really didn’t affect the presentation at all, as it just came right back out in that format. But it meant that my comments had all this markup in them for the last couple weeks.

The first thing I did was add the attribute back so that I wouldn’t get any more. Then I set about trying to figure out how in the world I was going to easily update the comments that had already been hosed like this. If it was one or two, I would have done it manually. But it was around 140!

Naturally I recalled the Search & Replace function built in to Movable Type, but unfortunately it was built only around entries. Doesn’t do a thing for comments. But it was oh-so-close to what I needed. So I figured what the heck – I’ll hack at it. But rather than go in and hack the module itself, I figured I’d write a little plugin instead.

I did have to update the template, because I needed the comment options on the screen. And the plugin, for the most part, takes the Six Apart code and just tweaks it a bit so that it works for comments. Because of this, it may not be completely kosher for me to release it this way. If they say something, I’ll have to take it down and present the information in a different way.

In any case, it did the job just as I wanted, so I figured I would make it available for download. Just unpack the archive, put the plugin (comment-search-and-replace.pl) in your plugins folder and the template (search_replace.tmpl) in the tmpl/cms folder. Both are found under the main MT directory. This adds some comment-specific options to the screen and allows you to search (and replace) your comments, as well as your entries. Enjoy.

Update: Everything else on the search portion of the screen looks the same. However, when you get your results, you’ll notice that the entry title includes the name of the commenter, and the Authored On date is when the comment was added.

This isn’t perfect, of course, but it should help you to nail down a particular comment, rather than searching through the entire page. At the same time, it’s done in a way that doesn’t modify too much of the code, but instead uses existing functionality, just slightly modified.


Posted

in