Fun Filters


Updated Oct 4. Fixed code to not convert sequences inside the regex example into smilies. Updated funfilters.zip file with a properly formatted README.

I finally got around to improving and cleaning up my blog filters. If any other other WordPress users would like to play with my hacks, I’m making the code available: funfilters.zip

Instructions

Inside the zip, you will find three text files:

The first step is to insert the contents of fun_filters.php.txt at the end of your b2-includes/b2functions.php file (but before the final ‘?>‘ line)

Next, open up your b2-includes/b2vars.php file and scroll down to the bottom. Look for this line:

    add_filter('all', 'wptexturize');

Just before that line, insert the contents of the activate_filters.php.txt file. With this code in place, your blog will automatically switch on the pirate filter on Talk Like a Pirate Day (September 19). Also, any of the other filters can be activated by adding ‘?filter=filtername‘ to the end of your URL (where filtername is one of ‘pirate‘, ‘chef‘, ‘fudd‘, ‘jive‘, or ‘kraut‘).

This started when I added a pirate filter to my blog for Talk Like a Pirate Day. My first version was easier than I expected it to be (though it had flaws), which inspired me to locate and convert some of Kalsey’s MovableJive filters.

The biggest flaw in the earliest version was that it would filter text inside of HTML tags, causing it to mangle links and such. I fixed this by borrowing an idea from Simon Willison. Simon’s use of a callback function to only match text that was not part of a tag was good, but it included the ‘>’ and ‘<’ brackets from surrounding tags in the matched text being substituted, requiring you to hack them back in at the end of your content filter.

After an afternoon studying the pcre pattern syntax and wrestling regexes with the help of the Regex Coach I came up with an improved pattern, which doesn’t require us to tack the ‘>’ and ‘<’ back on manually. Cool, huh? Here’s the regex pattern I came up with: (?(?<=>)|\A)([^<>]+)(?(?=<)|\Z)

And yes, I know, it’s not perfect. But it should work okay for HTML that’s moderately clean. You’d probably have to try hard to bust it. It doesn’t even seem to get confused if you have angle brackets inside of an attribute in a tag, even though I thought it would.

Other Posts of Interest

3 Comments

  1. Posted 4/5/2004 at 1:21 pm | Permalink

    Awesome stuff!

  2. Posted 4/12/2004 at 10:51 am | Permalink

    Dude, I am so glad I found your site… really cool stuff.:mrgreen:

  3. Posted 8/4/2004 at 5:11 am | Permalink

    Dougal,

    Any thoughts on updating this for WP 1.2+ ? I’d take a stab at it, but, uh, well…you know.

2 Trackbacks

  1. [...] ike a Pirate Day is only three weeks away, I spent some time this weekend revamping my old Fun Filters hack. The result is a new plugin for WordPress 1.2 or higher, which I call the [...]

  2. By Jean-Philippe Leboeuf Notebook on 1/2/2005 at 1:56 pm

    Stereotyped or humorous dialect filters

    While preparing my entry about styling languages, I re-discovered many dialect translators. For example, if you want plugins for your webblog, you can easily get: For MovableType: MovableJive. For WordPress: the Text Filter Suite Plugin for WordPress (…

Post a Comment

Your email is never published nor shared. Comments or website URLs deemed to be off-topic or inappropriate may be deleted at the site owner's discretion.