How to Add Canonical Tags in WordPress to boost your SEO by Michael Scaramozzino

How to Add Canonical Tags in WordPress to Boost Your SEO

How to Add Canonical Tags in WordPress with or Without Plugins to Boost Your SEO Rankings

Today I’ll show you how to add canonical tags in WordPress to boost your SEO rankings. First we’ll define what canonical tags are. Next we’ll look at why canonical tags are important for SEO. Then we’ll see how to add canonical tags in WordPress using WordPress plugins. Finally we’ll show how to add canonical tags in WordPress without plugins.


What are Canonical Tags?

Canon: An authoritative principal or rule.

Canonical Name (CNAME): A domain name system (DNS) record associating an alias to the primary host name.

Canonical Tag: A meta tag indicating the URL to the original source web page of content that is duplicated.

A canonical tag is a meta tag that specifies the primary URL that points to the original web page containing content that may be duplicated on other webpages elsewhere. So a duplicated web page or post may contain a canonical tag that points to the URL of the original webpage that contains that same content.

A self-referential canonical tag indicates the primary URL to use for a web page that is accessible via various alternate URLs. For instance posts in WordPress may be accessed via the post’s primary permalink or via alternate URLs such as a category, archive or tag URL.

Here is a simple example of a canonical tag that might appear in the header of a webpage for this post.

<link rel=”canonical” href=”https://dreamlight.com/how-to-add-canonical-tags-in-wordpress/” />

Why are Canonical Tags Important for SEO?

If the same content appears on multiple webpages, or if it is accessible via multiple URLs, then those multiple hits may compete with each other and dilute the search engine rankings of that content. Not only does duplicate content dilute your SEO rankings but search engines may also indirectly “penalize” duplicate content by listing duplicate hits only under the “show similar” links and by lowering your rank in other ways. You can boost the search engine rankings of the original source webpage by using canonical tags on the duplicated content. Just point the canonical tag to the URL of the original source webpage. This way the search engines know which page to index and which to skip.

Also, using self-referential canonical tags on any webpages that may be accessible via alternate URLs will avoid having those alternate URLs dilute your main URLs search engine rankings. As a result, self-referential canonical tags can strengthen the search engine rankings of your webpages.


How to Add Canonical Tags in WordPress with Plugins

There are various WordPress plugins that can automatically add canonical tags to your webpages. One popular SEO plugin that we often use is Yoast SEO. The Yoast SEO plugin automatically adds self-referential canonical tags to WordPress pages and posts. You may also override the default self-referential permalink canonical tags in Yoast SEO.

How to add canonical tags in wordpress with Yeost SEO plugin

 


How to Add Canonical Tags in WordPress without Plugins

Below is a simple example of how to add self-referential canonical tags in WordPress multi-site without using plugins. Simply add this php code to the header.php template file. The particular WordPress multi-site used in this example has a sub-directory domain structure.

<!-- Self referential canonical tag on all pages -->
<?php
   if ( is_front_page() ) {
      $canonical_url = get_home_url();
      if ( ! is_main_site() ) { // add trailing slash for subsite home directories
         $canonical_url = $canonical_url . '/';
      }
   } else {
      $canonical_url = get_permalink();
   }
?>
<link rel="canonical" href="<?php echo $canonical_url ?>" />

On a non-multi-site WordPress site you may skip the test for ( ! is_main_site() ) and the php code would look like the simplified sample below.

<!-- Self referential canonical tag on all pages -->
<?php
   if ( is_front_page() ) {
      $canonical_url = get_home_url();
   } else {
      $canonical_url = get_permalink();
   }
?>
<link rel="canonical" href="<?php echo $canonical_url ?>" />

DreamLight Can Design an Award-winning Web Site for You

New WordPress Website SampleDreamLight can develop an award-winning WordPress website for you. We can work closely with your marketing team to update your existing Web site that may be outdated or to design a brand new custom WordPress website. In addition to leveraging SEO as discussed above, we design all our custom WordPress websites to be fully responsive. As a result the overall design layout automatically re-flows on-the-fly to take advantage of modern mobile devices. As multimedia experts we can also create a wide range of integrated branded marketing content for your website to help it really stand out, including: 2D/3D digital design, illustration, animation, interactive multimedia or even web application development.

Contact us for more information about designing a custom WordPress website or to explore your specific needs.


Request a Free Quote Today!

WordPress Web Design + Digital Marketing Multimedia


If you enjoyed this tutorial please consider making a donation to fund more!


DreamLight wrote the book: Creating a 3D Animated CGI Short. See what we can do for you.

5 Responses

  1. Gwyneth Llewelyn
    | Reply

    Hi again!

    I found a slight issue with your code, which is not immediately obvious. Things called on functions.php are called before the Loop is called, which, in turn, means before $wp_query has been set. This means that things like is_front_page(), is_main_site(), etc. will theoretically not work, since they rely on the actual existence of a valid (set) $wp_query.

    Nevertheless, your code still does a great job — mostly because this ‘rule’ is very likely ‘bent’ by many theme developers. For example, in my test case (my own blog!), sometimes I would get the correct rel=canonical, sometimes it would be empty, and sometimes I would get repeated rel=canonical links (!), not necessarily the correct ones. This is not a real surprise: in some cases, functions.php will be called several times during the process of rendering the page; thus the repeated inserts.

    One of the safest ways to avoid this happening is by using a hook. In this case, it makes sense to add it to wp_head, like in the following gist:

    https://gist.github.com/GwynethLlewelyn/82ace1c8bf0e8669b540c6274138f653

    This code, however, will only work on the so-called singulars — that is, pages, single posts, attachment pages, and the like. It will not work on category/tag pages, nor on search results. Why? Well, the explanation is not immediately obvious, but from the perspective of WordPress, singulars are treated very differently by the code — even if both can trace their ‘birth’ to a $wp_query, the result is quite different — namely, the kind of object returned by a singular does have a unique ID that can be easily turned into a canonical URL (or permalink, or anything unique such as a UUID…), a query for all posts in a category/tag (filtered or not) will return a different kind of object — one that has no concept of a unique ID, since it returns a collection of posts (and of their IDs).

    Thus, in both your example and my own attempt, we can get canonical URLs for everything that just returns one element (be it a post, page, attachment, whatever… home pages, or author pages, may just be special cases of ‘pages’ which may require slightly different parsing, but that’s it).

    Of course, you can handle all those cases separately, one by one; say, start checking if you’re seeing the result of a category query; if yes, get the corresponding slug and construct the canonical URL from it. If it’s not a category, it might be a tag instead, so use the function to extract the corresponding tag slug… and so forth, for all possible cases (which are many — more than I thought, actually).

    It’s certainly possible to write all the code to handle all possible cases. However, I’d guess that this would come very close to the complexity of what the SEO plugins do. Granted, most of them have an additional check to make in all instances: handling manually-set canonical URLs, or such URLs as the plugin itself generates for each HTML page displayed, and which have to be stored ‘somewhere’. This is easy to do if we’re just talking about singulars (it could just be a meta key); but not so easy to deal with for all the other cases, especially those that involve complex filtering rules. That’s the reason what SEO plugins might be heavy-duty beasts requiring a lot of overhead to do all these things…

    Anyway, thanks to you, I’ve explored a wholly unknown universe!

    • Hi Gwyneth,
      Yes, I was only using it on single pages/posts and it worked OK on the themes I used it on. You are correct that for more robust wide-scale purposes a dedicated SEO plugin would be the way to go!
      Thanks,
      -MikeS

  2. Gwyneth Llewelyn
    | Reply

    Well done! Most of the other resources I read on this subject simply directed readers to one of the popular SEO plugins; you, at least, included some code, which could easily be placed anywhere.

    Personally, I’m a bit baffled why WordPress doesn’t do that by default; after all, it’s not that much of a hassle, since WP already has the notion of ‘permalink’ anyway…

    • Thanks Gwyneth,

      I’m glad it helped you out. It took me a bit to figure it out after doing some research, so I figured I’d post it to help others as well.

      Thanks,
      -MikeS

  3. James
    | Reply

    Great article.Its a pity though i can’t add a canonical tag on my site because its on free wordpress.com

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.