User:Ilmari Karonen/canonical
< User:Ilmari Karonen
Jump to navigation
Jump to search
Revision as of 20:03, 22 November 2010 by Ilmari Karonen (talk | contribs) (Quick and dirty MediaWiki hook to add rel=canonical links to all normal page view)
Quick and dirty MediaWiki hook to add rel=canonical links to all normal page views:
$wgHooks['ArticleViewHeader'][] = 'addCanonicalLinks';
function addCanonicalLinks ( &$article ) {
global $wgRequest, $wgOut;
if ( $article->getOldID() || $wgRequest->getVal( 'redirect' ) == 'no' ) return true;
$tags = ( $wgOut->mLinktags ? $wgOut->mLinktags : array() );
foreach ( $tags as $tag ) {
if ( $tag['rel'] === 'canonical' ) return true;
}
$wgOut->addLink( array( 'rel' => 'canonical',
'href' => $article->getTitle()->getFullURL() ) );
return true;
}