User:Ilmari Karonen/canonical

From NetHackWiki
< User:Ilmari Karonen
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)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

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;
}