Mediawiki Google PlusOne
Jump to navigation
Jump to search
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.
This is just a little hack to add the Google +1 button to a mediawiki installation. See technical notes on integrating Google +1 button for webmasters
Simply add the following snipped to your LocalSettings.php.
Remarks:
- It will add a button between title and article content.
- I chose to move it to the right for aesthetic reasons, cf "div align=right", feel free to modify
- I enforced the html attribute on g:plusone to avoid some special URLs to be taken into account instead of the canonical one, e.g. a button on http://wiki.yobi.be/index.php?title=Main_Page&action=print will actually link to http://wiki.yobi.be/wiki/Main_Page
- Because I used hook ArticleViewHeader, it will only display the button on article pages, and not on edition, history, etc.
# Google Plus
$wgHooks['BeforePageDisplay'][] = 'fnPlusOneJavaScript';
function fnPlusOneJavaScript( $out )
{
$script = '<script type="text/javascript" language="javascript" src="https://apis.google.com/js/plusone.js">'
. '</script>';
$out->addScript( $script);
return true;
}
$wgHooks['ArticleViewHeader'][] = 'fnPlusOneAnchor';
function fnPlusOneAnchor( &$article, &$outputDone, &$pcache )
{
global $wgOut;
global $wgTitle;
$wgOut->addHTML('<div align=right><g:plusone size="medium" href="' . $wgTitle->escapeLocalUrl() . '"></g:plusone></div>');
return true;
}
And make sure you invalidate the server cache. One easy way is to add the following to your LocalSettings.php to clean the cache whenever LocalSettings.php is edited:
$wgCacheEpoch = max( $wgCacheEpoch, gmdate( 'YmdHis', @filemtime( __FILE__ ) ) );
From v1.17 of mediawiki you can use this instead:
$wgInvalidateCacheOnLocalSettingsChange = true;