User:Sysy~metawiki/Joshua Oreman's icon extension

From Meta, a Wikimedia project coordination wiki
A proposal to move this page to was rejected.

See also Bug 539: Allow images that link somewhere other than the image page

From: Joshua Oreman <oremanj at gmail dot com>	Mailed-By:
To: MediaWiki announcements and site admin list <mediawiki-l at>
Date: Aug 30, 2005 2:04 PM

Untested, but should work.



$wgExtensionFunctions[] = "wfImageLink";

function wfImageLink()
   global $wgParser;
   $wgParser->setHook ('imglink', 'renderImageLink');

function renderImageLink ($text, $attr)
   global $wgParser, $wgUser, $wgTitle;
   $parserOptions = ParserOptions::newFromUser( $wgUser );
   $parser =& new Parser();
   $output =& $parser->parse("[[".$input."|IMAGE]]", $wgTitle, $parserOptions);
   $ret = $output->getText();
   $itag = "<img border='0' ";
   foreach ($attr as $k => $v) {
       if ($k == "src") {
           $itag .= "src='" . Image::imageUrl($attr['src']) . "' ";
       } else {
           $itag .= "$k='$v' ";
    $itag .= "/>";
   return str_replace ($ret, "IMAGE", $itag);

and add


to LocalSettings.php.

To use, do

<imglink src='MyImage'>Destination page</imglink>

instead of the doesn't-work

[[Destination page|[[Image:MyImage]]]]

You can also use height and width attributes on the <imglink> tag.


  • Rowan Collins added on the mailing list: it would be "nicer" to use specific link-related functions rather than parsing as a text link and replacing. Also, can't you just use $wgParser rather than $parser=new Parser?