Dougal Campbell's geek ramblings

WordPress, web development, and world domination.

Fun Filters

UPDATE 2008-09-22: This code were bein' superceded by me Text Filter Suite plugin. Aarrr, pass the grog! You can download th' current version o' th' plugin from th' WordPress Plugin Directory.

Updated Oct 4. Fixed code t' not convert sequences inside th' regex example into smilies. Updated file with a properly formatted README.

I finally got aroun' t' improvin' and cleanin' up me blog filters. Fire the cannons! If any other other WordPress users would like t' play with me hacks, I’m makin' th' code available:


Inside th' zip, ye will find three text files:

The first step is t' insert th' contents o' fun_filters.php.txt at th' end o' yer b2-includes/b2functions.php file (but before th' final ‘?>‘ line)

Next, open up yer b2-includes/b2vars.php file and scroll down t' th' bottom. Look fer this line:

    add_filter('all', 'wptexturize');

Just before that line, insert th' contents o' th' activate_filters.php.txt file, I'll warrant ye. With this code in place, yer blog will automatically switch on th' pirate filter on Talk Like a Pirate Day (September 19). Also, any o' th' other filters can be activated by addin' ‘?filter=filtername‘ t' th' end o' yer URL (where filtername is one o' ‘pirate‘, ‘chef‘, ‘fudd‘, ‘jive‘, or ‘kraut‘).

This started when I added a pirate filter t' me blog fer Talk Like a Pirate Day. My first version were bein' easier than I expected it t' be (though it had flaws), which inspired me t' locate and convert some o' Kalsey’s MovableJive filters.

The biggest flaw in th' earliest version were bein' that it would filter text inside o' HTML tags, causin' it t' mangle links and such. I fixed this by borrowin' an idea from Simon Willison, avast. Simon’s use o' a callback function t' only match text that were bein' not part o' a tag were bein' good, but it included th' ‘>’ and ‘<‘ brackets from surroundin' tags in th' matched text bein' substituted, requirin' ye t' hack them back in at th' end o' yer content filter.

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

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

About Dougal Campbell

Dougal is a web developer, and a “Developer Emeritus” for the WordPress platform. When he’s not coding PHP, Perl, CSS, JavaScript, or whatnot, he spends time with his wife, three children, a dog, and a cat in their Atlanta area home.

This entry was posted in Plugins, WordPress and tagged , , , , , , , . Bookmark the permalink.

8 Responses to Fun Filters

  1. Pingback: geek ramblings » Text Filter Suite Plugin for WordPress

  2. logtar says:

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

  3. Craig says:


    Any thoughts on updatin' this fer WP 1.2+ ? I’d take a stab at it, but, uh, well…ye know.

  4. Pingback: Jean-Philippe Leboeuf Notebook

  5. Frank says:

    Will this work fer WP 2.6? Aarrr! The download link is dead.

  6. Pingback: Mind Blowing WordPress Plugins « Lorelle on WordPress

  7. Pingback: Odds and Ends WordPress Plugins You Must Know About « Lorelle on WordPress

Leave a Reply

%d bloggers like this: