UPDATE 2008-09-22: This code was superceded by my Text Filter Suite plugin. You can download the current version of the plugin from the WordPress Plugin Directory.
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:
- README.funfilters.txt (what you’re reading right now)
- fun_filters.php.txt
- activate_filters.php.txt
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.
Fun FiltersRelated posts:
- Talk Like a Pirate (and TFS updates)
" Talk Like a Pirate Day is next Monday (September 19), less than a week away. Dust off your eyepatch, dig up that gold hoop..." - Text Filter Suite Plugin for WordPress
"Since Talk Like a Pirate Day is only three weeks away, I spent some time this weekend revamping my old Fun Filters hack. The result..." - Yarrr! It’s Talk Like a Pirate Day!
"Once again, we have arrived on Talk Like a Pirate Day. I had been hoping that I would be able to find time to upgrade..." - Happy Talk Like a Pirate Day!
" Ahoy! Prepare to be boarded! It’s time fer Talk Like a Pirate Day again! Don’t forget t' install me Text Filter Suite Plugin if..." - Pirate filter fix
"Michael Heilemann wrote: > Hey Dougal. > > I get the following error when I try to comment on my site with the > pirate..."














4 Comments
Awesome stuff!
Dude, I am so glad I found your site… really cool stuff.:mrgreen:
Dougal,
Any thoughts on updating this for WP 1.2+ ? I’d take a stab at it, but, uh, well…you know.
Will this work for WP 2.6? The download link is dead.
2 Trackbacks
[...] 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 [...]
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 (…