Disable WordPress Built-in Canonical URL link tag in HTML head

Posted on In QA

The built-in canonical URL function since WordPress 2.9 is great. But under some situation, it is not needed. For example, all the Mingle Forum threads’ canonical URL is set to the URL of the page that contains the [mingleforum] shortcode, which, of course, is wrong.

How to diabled it?

We can disabled WordPress’s Built-in Canonical URL by the following script:

# Remove WordPress' canonical links
remove_action('wp_head', 'rel_canonical');

Specifically for Mingle forum, we only need to disable the canonical links for pages. Here are the changes:

diff --git a/b/wp-content/plugins/mingle-forum/wpf.class.php b/b/wp-content/plug
index 4e00451..96ca02c 100644
--- a/b/wp-content/plugins/mingle-forum/wpf.class.php
+++ b/b/wp-content/plugins/mingle-forum/wpf.class.php
@@ -20,6 +20,8 @@ class mingleforum{
                add_action("admin_menu", array(&$this,"add_admin_pages"));
                add_action("admin_head", array(&$this, "admin_header"));
                add_action("wp_head", array(&$this, "setup_header"));
+        # Remove WordPress' canonical links
+        remove_action('wp_head', 'rel_canonical');
                add_action("plugins_loaded", array(&$this, "wpf_load_widget"));
                add_action("wp_footer", array(&$this, "wpf_footer"));
                if($this->options['wp_posts_to_forum'])
@@ -34,7 +36,8 @@ class mingleforum{
                add_filter('mf_ad_above_info_center', array(&$this, 'mf_ad_above
                add_filter('mf_ad_above_quick_reply', array(&$this, 'mf_ad_above
                add_filter('mf_ad_above_breadcrumbs', array(&$this, 'mf_ad_above
-               add_filter('mf_ad_below_first_post', array(&$this, 'mf_ad_below_
+        add_filter('mf_ad_below_first_post', array(&$this, 'mf_ad_below_first_p
+
                $this->init();
        }

@@ -1260,7 +1263,10 @@ class mingleforum{
                        }
                        </script>
                <?php
-               }
+        }
+        else {
+            rel_canonical();
+        }
        }

More readings about canonical URL in WordPress:

http://stackoverflow.com/questions/2430623/wordpress-auto-generated-canonical-links-how-to-add-a-custom-url-parameter

http://pixelpunk.co.uk/2010/01/disable-wordpress-built-in-canonical-url/


Hi ,

Thank you for your recommendation for removing the canonical link for the forum page only. However, I am am not able to find the following code in the wpf.class.php and was hoping you let us know where we can find this class so that we can modify it accordingly. (Does -1260 refer to the line number in the wpf.class.php?

@@ -1260,7 +1263,10 @@ class mingleforum{
}

<?php
– }
+ }
+ else {
+ rel_canonical();
+ }
}





Google is not indexing our forum and we do not want to remove the canonical links globally cause we are not sure if it is going to cause other issues …


Hi trueactivist,

The canonical URL is really a problem and possibly prevent Google from indexing your forum since the canonical tag keeps telling Google that all forum posts are the “same page”.

For you reference, I post more code related to the canonical URL here:

(The line numbers may not be the same as yours since I customized Mingle forum a lot).

wpf.class.php

   1 <?php
   2 include("wpf_define.php");
   3 include_once( 'bbcode.php' );
   4 //@ob_start();
   5 
   6 if(!class_exists('mingleforum')){
   7 class mingleforum{
   8 
   9     var $db_version = 1; //MANAGES DB VERSION
  10 
  11     function mingleforum()
  12     {
  13         $this->get_forum_admin_ops();
  14         $this->get_set_ads_options();
  15         if($this->options['forum_use_seo_friendly_urls'])
  16         {
  17             add_filter("init", array(&$this, "flush_wp_rewrite_rules"));
  18             add_filter("rewrite_rules_array", array(&$this, "set_seo_friendly_rules"));
  19         }
  20         add_action("admin_menu", array(&$this,"add_admin_pages"));
  21         add_action("admin_head", array(&$this, "admin_header"));
  22         add_action("wp_head", array(&$this, "setup_header"));
  23         # Remove WordPress' canonical links
  24         remove_action('wp_head', 'rel_canonical');
  25         add_action("plugins_loaded", array(&$this, "wpf_load_widget"));
  26         add_action("wp_footer", array(&$this, "wpf_footer"));

Here, line 23, 24 is newly added to remove canonical tag.

wpf.class.php

1292     function setup_header(){
1293         $this->setup_links();
1294         global $user_ID;
1295         if(is_page($this->get_pageid()))
1296         {
1297             if($this->options['forum_use_rss']) { ?>
1298                 <link rel='alternate' type='application/rss+xml' title="<?php echo __("Forums RSS", "mingleforum"); ?>" href="<?php echo $this->global_feed_url;?>" /> <?php
1299             }
1300             if($this->ads_options['mf_ad_custom_css'] != "") {
1301             ?>
1302             <style type="text/css"><?php echo stripslashes($this->ads_options['mf_ad_custom_css']); ?></style>
1303             <?php } //ENDIF FOR CUSTOM ADS CSS ?>
1304             <link rel='stylesheet' type='text/css' href="<?php echo "$this->skin_url/style.css";?>"  />
1305             <script language="JavaScript" type="text/javascript" src="<?php echo WPFURL."js/script.js"?>"></script>
1306             <script language="JavaScript" type="text/javascript">
1307             function wpf_confirm(){
1308                 var answer = confirm ('<?php echo __("Are you sure you want to remove this?", "mingleforum");?>');
1309                 if (!answer)
1310                     return false;
1311                 else
1312                     return true;
1313             }
1314             </script>
1315 
1316             <?php $this->generate_canonical_tag(); // add cononical tag ?>
1317 
1318         <?php
1319         }
1320         else {
1321             rel_canonical();
1322         }
1323     }

This is the changed setup_header() function. The idea is to enable canonical tag again for non-page in WordPress, and add canonical tag for the page for Mingle forum. For other pages, just ignore the canonical tag—this should be find if your site uses WordPress posts with not that many pages.

The generate_canonical_tag() function is as follows (if it is not provided by Mingle forum. I can not remember it clearly).

1262     function generate_canonical_tag()
1263     {
1264         $url = '';
1265         if ($this->options['forum_use_seo_friendly_urls'])
1266         {
1267             $uri = $this->get_seo_friendly_query();
1268             if (!empty($uri) && $uri['action'] && $uri['id'])
1269             {
1270                 switch($uri['action'])
1271                 {
1272                 case 'group':
1273                     $url = $this->get_grouplink($uri['id']);
1274                     break;
1275                 case 'forum':
1276                     $url = $this->get_forumlink($uri['id']);
1277                     break;
1278                 case 'thread':
1279                     $url = $this->get_threadlink($uri['id']);
1280                     break;
1281                 }
1282             }
1283         }
1284         if (!empty($url)) {
1285             echo "<link rel='canonical' href='" . $url . "' />";
1286         }
1287         // echo " " . $uri['id'] . " ";
1288         return true;
1289     }

One comment

Leave a Reply

Your email address will not be published. Required fields are marked *