Removing the canonical URLs in WordPress

The canonical tag specifies a URL to indicate the original page URL if there are multiple pages (or URLs) of the same content. Its purpose is to help the search engines to index only the original pages. Some SEO plugins like Yoast, it generates a self-referencing URL in a canonical tag on your all pages.

Issues of putting a canonical tag across all pages

Most of the time, putting a canonical tag in all pages does not cause problems because search engines like Google can handle that fine. But that is not a good idea for Bing.

Bing may refuse to index a page that has a canonical URL pointing to itself because it considers the page as a duplicated one as below. Even Bing used to index that page before.

URL cannot be indexed by Bing

The URL is not indexable as the page is an alternate version
of the similar page which you have specified as canonical version
using a  ``tag. Bing does not index the
alternate version.

Note

If your site is removed from Bing index, get more ideas about how to fix it at Bing Webmaster Guidelines (Updated in 2020).

1. Remove the canonical URLs added by Yoast

The canonical URLs can be generated by the WordPress or/and an SEO tool like Yoast. Newer version of Yoast (like v19.0) generates a canonical tag with a class property which looks like:

<link rel="canonical" href="http://example.com/a/" 
  class="yoast-seo-meta-tag">

Yoast allows you to edit the canonical URL on a page. But if you do not want Yoast put a canonical tag on all pages, you need to remove all of them programmatically.

Yoast SEO provides the wpseo_canonical filter (old version like v13.4 use ) for you to custom the function which returns the canonical URL of a page (see Yoast SEO Canonical URLs API). To remove the canonical URLs, you can add your own callback that returns NULL to the filter like:

function no_canonical_url () {
    return NULL;
}

add_filter( 'wpseo_canonical', 'no_canonical_url' );

Or

add_filter( 'wpseo_canonical', '__return_false' );

__return_false is a WordPress API which just returns false, note there are double _ at the beginning.

Note

Old version of Yoast does not provide such a filter for you to custom. Like Yoast v13.4.1 directly generates a canonical tag with below code:

add_action( 'wpseo_head', [ $this, 'canonical' ], 20 );

In such case, you can use remove_action() to remove the callback for wpseo_head action like:

remove_action( 'wpseo_head', 
    [ WPSEO_Frontend::get_instance(), 'canonical' ], 20 );

However, directly make changes in the the Yoast’s folder is not a good idea. If Yoast gets updated, the changes will be lost. For the moment, the best way to custom a plugin is to write a new plugin in which you can make customization to the original plugin. See more at Ways to custom WordPress .

Here is the plugin file looks like:

remove-canonical-urls.php

<?php
/*
 * Plugin Name: Remove Canonical URLs
 * Description: A plugin to remove the canonical URLs genereated by Yoast. SEO
 * Author: Gloomic
 * Version: 0.1
 */

if ( ! defined( 'WPINC' ) ) {
    die( 'No direct access.' );
}

// Custom Yoast's function which generates a canonical URL tag
// to make Yoast not genretate canonical URLs.
add_filter( 'wpseo_canonical', '__return_false' );

Create a folder with the same name remove-canonical-urls under your site’s plugins folder then active the plugin.

Or you can either put it the functions.php of a child theme if you are using one.

2. Remove the canonical URLs added through get_canonical_url hook

WordPress’s get_canonical_url filter hook filters the canonical URL for a post.

It is much similar to that described in the previous section.

add_filter( 'get_canonical_url', '__return_false' );

Similarly, put your code in a child theme or your own plugin.

Resource

  • Yoast SEO Canonical URLs – API documentation

  • Filter get_canonical_url

    Filters the canonical URL for a post.

  • Early in 2011, Duane Forrester from Bing has said:

    > “Something else you need to keep in mind when using the rel=canonical is that it was never intended to appear across large numbers of pages. We’re already seeing a lot of implementations where the command is being used incorrectly.”