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="<?php echo $_SERVER['REQUEST_URI'];?>">

Obviously you will need to replace 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>

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

  1. Dheeraj sharma

    Nice Post Simon

  2. This didn’t work for me

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

      • 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.

        • 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/

          • 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.