Canonical tag by SEO

How to change canonical tags in WordPress ?

By Atul Jain | WordPress Tutorials | 22 Oct 2021


What is canonical links?

canonical link search

If you have a single page that's accessible by multiple URLs, or different pages with similar content (for example, a page with both a mobile and a desktop version), Google sees these as duplicate versions of the same page. Google will choose one URL as the canonical version and crawl that, and all other URLs will be considered duplicate URLs and crawled less often.

If you don't explicitly tell Google which URL is canonical, Google will make the choice for you, or might consider them both of equal weight, which might lead to unwanted behavior.

Now lets see and example:

 

https://www.xyz.com/blog/page/3/

 

https://www.xyz.com/blog/page/3/

 

https://www.xyz.com/blog/

 

Here we are given with page URL, current canonical URL, and recommended canonical URL. We need to change current canonical URL to recommended canonical URL.

First lets open the URL and open Dev tools, inside header tag you will see a link tag with rel canonical.

Image to show current canonical link

As we do not want google to view all pages URL as separate websites and want to consider it as same origin. So now we will proceed to make changes resulting our canonical links to be “https://www.xyz.com/blog/“.

Please follow the below mentioned steps:

  1. First check your SEO tool being used in WordPress. In our case we are using Rank Math SEO.

  2. Rank Math SEO Filters and Hooks for Developers - Rank Math Visit Rank Math developer API page and search for filter for the desired use-case. Filters are functions in WordPress which perform certain task.

    rankmath canonical api

  3. In the above image you can see filter to adjust canonical links. param $canonical passed is containing current canonical link. And inside fucntion you can make changes and return new canonical link.
  4. Here we will de-structure canonical string and search for desired sub-string. In our case $canonical = https://www.xyz.com/blog/page3 and our sub-string will be “blog/page“.

  5. We will use strpos function. strpos — Find the position of the first occurrence of a sub-string in a string.

  6. add_filter( 'rank_math/frontend/canonical', function( $canonical ) {
      if (strpos($canonical, 'blog/page')){
        return 'https://www.xyz.com/blog/';
      } elseif (strpos($canonical, 'blog/uncategorized/page')) {
        return 'https://www.xyz.com/blog/uncategorized';
      }
    return $canonical
     
  7. Here in the above code, we are checking $canonical contains “blog/page“ or “blog/uncategorized/page“. If yes then return new canonical link.

  8. Paste above code to Dashboard > Appearance > Theme Editor > functions.php.
    functions.php file wordpress

  9. Now Click on Update File, and open the same link again.
  10. And you have successfully changed canonical tag for selected pages to avoid link duplication in Google SEO.
    canonical links changed

 

Thank You :)

How do you rate this article?

0


Atul Jain
Atul Jain

I am software developer by profession. Love to learn new stuffs and making blog and articles on them is my fav thing to do.


WordPress Tutorials
WordPress Tutorials

In this blog I'll walk you through step by step guide to change canonical tag in WordPress.

Send a $0.01 microtip in crypto to the author, and earn yourself as you read!

20% to author / 80% to me.
We pay the tips from our rewards pool.