Mediawiki Google PlusOne
Revision as of 09:15, 1 September 2011 by <bdi>PhilippeTeuwen</bdi> (talk | contribs) (Created page with "This is just a little hack to add the [http://www.google.com/+1/button/ Google +1 button] to a mediawiki installation. See technical notes on [https://www.google.com/intl/en/webm…")
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 purge the server cached pages. 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;