Dougal Campbell's geek ramblings

WordPress, web development, and world domination.

Fun Filters

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:

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.

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. Pingback: Jean-Philippe Leboeuf Notebook

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

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

Leave a Reply