Rosio Pavoris

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.

Wikipedia desu~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');

?>

6 Comments

  1. Coren said,

    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.

  2. Coren said,

    Heeh, it doesn’t work in comments~

  3. Cairnarvon said,

    If you want it to work in comments, add add_filter('comment_text', 'wikilink'); to the end.

    (Before the ?>, though, obviously.)

  4. Terras said,

    Woo~ Useful~

  5. Quhan said,

    Neat neat.

  6. Cairnarvon said,

    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.

Post a Comment

RSS feed for comments on this post · TrackBack URL