When writing posts, I often use various technical terms. Explaining them one by one basically means just copying the content from the Wikipedia entries — tedious and thankless work. It's better to just drop in a link. But adding links every time is a nightmare; anyone doing that kind of data entry would go crazy.
A very simple solution is to add a plugin that automatically attaches Wikipedia external links to marked-up terms.
Implementation
It's basically done using the preg_replace function in php.
<?php
/**
*Plugin Name: Wikipedia_Entry_Inserter
*Version: 0.1
*Plugin URI: http://argcv.com/articles/1675.c
*Description: insert wikipedia entry with <wiki> or <wiki:zh>
*Author: Yu Jing
*Author URI: http://argcv.com
*/
function Wikipedia_Entry_Inserter($outer){
if(!is_singular()) return $outer;
$outer = preg_replace("/<wiki>(.+?)<\/wiki>/",
'<a href="//en.wikipedia.org/wiki/$1" title="$1" target="_blank">$1</a>',
$outer);
$outer = preg_replace("/<wiki:zh>(.+?)<\/wiki:zh>/",
'<a href="//zh.wikipedia.org/wiki/$1" title="$1" target="_blank">$1</a>',
$outer);
return $outer;
}
add_filter('the_content', 'Wikipedia_Entry_Inserter');
?>Note: Due to a plugin issue, the spaces between characters in the first parameter of preg_replace need to be removed.
I only know it's caused by the wiki:zhLatex</wiki:zh> plugin; I didn't quite figure out the specifics.
Reference: https://goo.gl/MPa3ob
Usage
Save the code as a PHP file, package it as a zip, and upload it as a plugin.
When using it, wrap the term with <wiki> (English Wikipedia) or <wiki:zh> (Chinese Wikipedia).
The result looks like this:
PS:
Turning every little trick into a plugin — I must be out of my mind 囧
Another Approach
The method above uses direct string matching, replacing specific strings with other content via regex. But it has a drawback: the rules are hard to manage, and complex logic requires even more convoluted code. On the WordPress platform, add_shortcode is another, better approach.
The code is as follows:
<?php
/**
*Plugin Name: Wikipedia Entry Inserter
*Version: 0.2
*Plugin URI: http://argcv.com/articles/1675.c
*Description: insert wikipedia entry with [raw][wiki] and [/wiki][/raw]
*Author: Yu Jing
*Author URI: http://argcv.com
*/
function wiki_entries_widget($attr, $content = ""){
extract(shortcode_atts(array(
"lang" => 'en',
"entry" => '',
'class' => 'wiki_widget'), $attr));
if($content == ''){
return '';
}
$link = '//'. ($lang == 'zh' ? 'zh' : 'en').'.wikipedia.org/wiki/'. ($entry ? $entry : $content);
return '<a class="' . $class . '" href="'.$link.'" >'.$content.'</a>';
}
add_shortcode("wiki", "wiki_entries_widget");This way we only need a single "wiki" shortcode, and we can easily add various options. For example:
[raw]
[wiki lang='zh' entry='东风破']周杰伦的歌[/wiki]
[/raw]We can set the language to zh, display it as "Jay Chou's songs," while the link actually points to the "Dong Feng Po" entry. This implementation is somewhat simpler than direct string matching. The output code is as follows:
[wiki lang='zh' entry='东风破']周杰伦的歌[/wiki]The result looks like this:
Comments · 25