Controlling rel canonical tag in WordPress

I used a forum plugin in wordpress. This plugin provides a shortcode which is placed on a page. Once place this single page is used to shows forum home page, forum category pages and topics pages as well. For the sake of SEO i didn’t want the canonical tag e.g. <link rel=”canonical” href=”http://my-website.com/section” /> to appear of forum home page and subsequent pages.

I have been using the WordPress SEO Plugin by Yoast. This plugin creates canonical tag by removing the default rel_canonical action from wp_head filter. As a good plugin it provides a filter to alter the use of canonical tag in it’s own function. The name of the filter is “wpseo_canonical”.

I placed the following piece of code in my theme/functions.php file. The name of the page on which i wanted this tag removed was “rejseforum”. I obviously wanted this action be performed on front-end thus if(!is_admin()) check. Rest of the code is very much understandable for someone good in php.

if(!is_admin()) {
add_action('wpseo_canonical', 'rejseforum_skip_canonical');
function rejseforum_skip_canonical($canonical) {
global $post;
if($post->post_name=='rejseforum') {
return false;
}
return $canonical;
}
}

Even if i didn’t use the WordPress SEO Plugin by Yoast and wanted to remove the canonical tag i would have used the following line of code somewhere in my functions.php file:

remove_action( 'wp_head', 'rel_canonical' );

Leave a Reply