Skip to content

Instantly share code, notes, and snippets.

@dartiss
Last active January 23, 2018 15:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dartiss/d0dae65e70378ec73d4e1eb49c6309b5 to your computer and use it in GitHub Desktop.
Save dartiss/d0dae65e70378ec73d4e1eb49c6309b5 to your computer and use it in GitHub Desktop.
WordPress shortcode to add a Wikipedia link to post text
function wikilinker_shortcode( $paras = '', $content = '' ) {
// Extract the shortcode parameters
extract( shortcode_atts( array( 'alt' => '', 'rel' => '', 'lang' => 'en', 'target' => '' ), $paras ) );
// If an alternative link is specified use that rather than the linked text
if ( '' != $alt ) { $lookup = $alt; } else { $lookup = $content; }
// Now ensure that all spaces are replaced with underscores. It's what Wikipedia would want
$lookup = str_replace( ' ', '_', $lookup );
// Build the title plus any additional, optional parameters
$title = sprintf( __( '%s on Wikipedia', 'wikilinker' ), $content );
$extras = '';
if ( '' != $rel ) { $extras .= ' rel="' . $rel . '"'; }
if ( '' != $target ) { $extras .= ' target="' . $target . '"'; }
// Generate the HTML code
$output = '<a href="https://' . $lang . '.wikipedia.org/wiki/' . $lookup . '" title="' . $title . '"' . $extras . '>' . $content . '</a>';
return $output;
}
add_shortcode( 'wikilink', 'wikilinker_shortcode' );
@dartiss
Copy link
Author

dartiss commented Jan 23, 2018

This is a super simple plugin that allows you to quickly and easily add Wikipedia links to your post or page content.

The quickest way to add a link is simply to wrap the [wikilink] shortcode around a word or series of words. It will then link to those words. For example...

Chicago was also the codename for [wikilink]Windows 95[/wikilink].

This will then add a link to the words "Windows 95", creating a quick and easy way to link to useful information for readers. Unfortunately, not all Wikipedia links are that simple. If the text doesn't link to the correct entry you can use the alt parameter to specify your own. For example...

If you've seen the film, the [wikilink alt="Deadpool (Original Motion Picture Soundtrack)"]soundtrack to Deadpool[/wikilink] is worth considering.

By default this plugin uses the English language version of Wikipedia but you can override this using the lang parameter. Other available parameters include rel and target and operate exactly the same as their HTML equivalents.

Here are all the parameters in use...

[wikilink lang="fr" alt="Pop rock" rel="nofollow" target="_blank"]musique pop et rock[/wikilink]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment