Canonical URLs without using a WordPress Plugin

and published
add canonicals in wordpress without plugin

Everyone that knows me will know that I hate using WordPress plugins, not that I’m a website designer that believes that every other designer and coder should write their own code, far from it. I’m a web designer that believes you should only use trusted plugins when you absolutely need to. Using a plugin that offers you 100 features when you only need one, makes no sense to me! Using such a plugin is only going to bloat your code, slow down your SQL and increase the chances of exploitable vulnerabilities.

Whenever possible you should avoid using WP plugins unless you absolutely need to, if you’re looking to use just canonical URLS then you don’t need a fancy all dancing all singing SEO plugin, you can add canonical URLS to WordPress without a plugin, in fact in this short guide I’ll provide you the code that with work with any content management system that uses PHP.

WordPress SEO Plugins

I do however recommend Yoast SEO or any other reputable SEO plugin if you want better SEO management, such as changing titles and meta descriptions but only if you need this… a 5 page business website doesn’t really require an SEO plugin because everything you can do with the plugin you can do without, of course it might take you 20mins more! But the end results will be a site that is easier to upgrade without fear of something going wrong and a cleaner SQL database.

Universal PHP Code for Canonical URLS

Below is the code that you need to add within the head of your theme template:

<link rel="canonical" href="https://www.bybe.net<?php echo $_SERVER['REQUEST_URI'];?>">

Obviously you will need to replace bybe.net with your own domain.

Adding canonical Links in WordPress

Below are a few easy steps that will have you up and running with WordPress canonical URLS in no time at all, and without a plugin.

  1. Login to WordPress
  2. Hover over Appearance
  3. Click Editor
  4. Find Theme Header (header.php)
  5. Add the Universal PHP Code for Canonical URLS found above within the HEAD section of your theme header file.

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

17 Responses to “Canonical URLs without using a WordPress Plugin”

  1. Dheeraj sharma

    Nice Post Simon

    Reply
  2. This didn’t work for me

    Reply
    • Actually it does – needed the full url!! 😀

      Reply
      • Also, it now says that certain pages are blocked from being crawled – the ones that had the duplicate issues – so I guess that’s a good thing? As I don’t want them being indexed.

        Reply
        • Ah wait… they all show up with a / at the end – is there anyway to remove this /?
          As just want the page .html not .html/

          Reply
          • Sorry but its not compatible with extensions. In this day and age there is never any need to use .php, .html extensions. URLS are better short in terms of user experience and SEO.

  3. Anthony Tornambe

    Nice, It works fine.

    Reply
  4. Creativelopers

    If this is done, will it affect posts that dates as far back as 2012?

    Reply
  5. Great tutorial, I added the canonical tag and it improves my website appearance.

    Reply
  6. Sorry, I forgot to include the code tags.

    Reply
  7. Okay, apparently you can’t actually include code in the comments. TLDR; Your tag is missing a forward slash before the closing bracket.

    Reply
  8. Very cool 🙂 Damn awesome to do this without a plugin. Question though….sometimes one actually needs to fire the rel-cannon at a target rather than at itself. How do you implement that with your excellent solution?

    Reply
    • Hi Dove, the code is very simple and ideal for sites that only require internal canonical links. You could however adapt the code and have it use WordPress Conditional Tags but for a large volume of pages this would not be ideal.

      Another option would be to have a custom field but you would need to edit your LOOP to include it. If you want to keep things simple I recommend you use Yoast SEO for external URLS, this will set canonicals locally by default and should you want to use an external source then simply click advanced before you publish the page and enter the external URL.

      Picture of Yoast external canonical option:

      Yoast WordPress Canonical URLS

      Reply
  9. Does this work for sites that recently switched from http to https? I added the code but I am still getting canonical errors

    Reply
    • Yes, this works for both HTTPS and HTTP websites, see below:

      • Websites without SSL use:
        <link rel="canonical" href="http://www.example.co.uk<?php echo $_SERVER['REQUEST_URI'];?>">
      • Websites with SSL use:
        <link rel="canonical" href="https://www.example.co.uk<?php echo $_SERVER['REQUEST_URI'];?>">

      Note: replace example.co.uk with your own domain name

      Reply