Wikilinkifier
As you may have noticed, I link to Wikipedia a lot, and having to type the entire address out bothers me, so I wrote a WordPress plugin to do it for me.
What this plugin does it take your post and, before it’s displayed, replaces [[foo]] and [[bar|baz]] with links to Wikipedia. In the first case, foo, in the second bar.
Like my Cthulheriser plugin, it doesn’t actually change anything in the database, so if it acts up, you can just turn it off with no damage to your data.
It doesn’t convert spaces to underscores, or change the first letter to a capital letter, both because Wikipedia does that for you anyway, and because PHP is awkward about regular expressions.
I know how to do it in Perl, dammit. ;.;
The code is below, or here. Copy that code to a textfile (or just download that textfile), change the extension to .php, upload it to your /wp-content/plugins/ folder, and activate it from you Plugins page.
(Licensed under the GPL, of course.)
<?php
/*
Plugin Name: Wikilinkifier
Plugin URI: http://cairnarvon.rotahall.org/?p=586
Description: Replaces Wikitext link syntax with links to Wikipedia.
Version: 1.0
Author: Koen Crolla
Author URI: http://cairnarvon.rotahall.org/
*/
function wikilink($content) {
$pattern = array('/\[\[([^\]|]+)([^\]|]+)?\]\]/',
'/\[\[([^\]|]+)\|([^\]]+)?\]\]/');
$replace = array("<a href=\"http://en.wikipedia.org/wiki/$1\">$1</a>",
"<a href=\"http://en.wikipedia.org/wiki/$2\">$1</a>");
$content = preg_replace($pattern, $replace, $content);
return $content;
}
add_filter('the_content', 'wikilink');
?>
Coren said,
February 17th, 2007 at 4:15 pm
This is pretty handy. I approve.
I also learned where the name of my favorite audio player, [[Foobar2000]], is actually slightly more clever than I thought.
Coren said,
February 17th, 2007 at 4:15 pm
Heeh, it doesn’t work in comments~
Cairnarvon said,
February 17th, 2007 at 4:23 pm
If you want it to work in comments, add
add_filter('comment_text', 'wikilink');to the end.(Before the ?>, though, obviously.)
Terras said,
February 17th, 2007 at 4:34 pm
Woo~ Useful~
Quhan said,
February 18th, 2007 at 8:12 pm
Neat neat.
Cairnarvon said,
February 18th, 2007 at 8:14 pm
I want to do a post about regular expressions, but there really isn’t much to say about them without turning it into a how-to type of thing.