Wikipedia links in the last post

In an effort to help explain the technical post son this site with as little difficulty as possible, I’ve started linking terms to Wikipedia. To do this I added a little WordPress plugin to the old my-hacks.php file.

This adds support so that typing any topic between <wiki> tags will automatically create a link to the desired Wikipedia article.

The code follows below:

function wiki($text) {
  $offset = 0;
  do
  {
    $start = @strpos($text, '<wiki>', $offset);
    if ($start !== false)
    {
      $end = strpos($text, '</wiki>', $start);
      $topic =  trim(substr($text, $start + 6, $end - $start - 6));
      $text = substr($text, 0, $start) .
              "<a href=\"http://en.wikipedia.org/wiki/" .
              str_replace(" ", "_", $topic) .
              "\" title=\"Wikipedia entry on $topic\">$topic</a>" .
              substr($text, $end + 7);
      $offset = $end;
    }
  } while ($start !== false);
  return $text;
}

About this entry