Last active
January 23, 2018 15:29
-
-
Save dartiss/d0dae65e70378ec73d4e1eb49c6309b5 to your computer and use it in GitHub Desktop.
WordPress shortcode to add a Wikipedia link to post text
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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]