As your website grows, chances are you have several pages or posts where the content is nearly the same. This can cause duplicate content issues where a search engine like Google doesn’t know which of the URLs to include in the search results. 

A canonical tag (aka “rel canonical”) is a way of telling search engines that a specific URL represents the master copy of a page. Using the canonical tag prevents problems caused by identical or “duplicate” content appearing on multiple URLs. Practically speaking, the canonical tag tells search engines which version of a URL you want to appear in search results.

What is Canonical URLs

canonical URL WordPress

source: moz.com

Setting Canonical URLs in Yoast SEO

Step 1.Log in to your WordPress website.

You are now in your ‘Dashboard’.

Step 2.Go to the post, page, or taxonomy for which you want to change the canonical URL.

On the left-hand side, you will see a menu that allows you to navigate to your posts, pages, and other content. Find the content you want to edit, and click on it to access the editing screen.

menu wp content

Step 3.Once you are in the editing screen, go to the ‘Advanced’ section in the Yoast SEO sidebar or meta box.

Note: the Advanced section in the meta box is part of the SEO tab.

advanced tab yoast seo_wp

Step 4.Enter the full canonical URL, including http/s and www or non-www, in the ‘Canonical URL’ field.

canonical_url_yoast_wp

Step 5.Update the post, page, or taxonomy.

source: yoast.com

Setting Canonical URLs in Rank Math

Step 1.Edit the Post/Page

At first, you need to open the post/page that contains the duplicate content by clicking on the Edit as shown below:

open post scaled

Step 2.Navigate to the Advanced Tab of Rank Math

Once you’ve opened the post/page, navigate to the Advanced tab of the Rank Math in your editor. If you’re unable to find the Advanced tab, please enable the advanced mode from WordPress Dashboard > Rank Math > Dashboard.

rank math advanced tab

Step 3.Change the Canonical URL

Under the Advanced tab, you can change the Canonical URL field to point to the main source of your content. The Canonical URL informs the search crawlers of the main page if you have pages or posts with similar content.

The below screenshot is for reference only.

rank math canonical url

Step 4.Save the Post

Once you’re done setting your Canonical URL – simply update the page as you normally would after making a change or click Publish if this is a newly created page.

source: rankmath.com

Add canonical tag in WordPress without plugins

Step 1.Add The Following Code to the functions.php File

Inside the WordPress dashboard Theme Editor or on the web server, find and open the functions.php file. Go to Appearance -> Theme Editor.

You must create a child theme before making any changes to functions.php file. Otherwise, the applied changes will be lost after each update.
Create child theme in WordPress step by step [without plugin]

As an alternative method, you can use the Code Snippets plugin to insert your codes into WordPress.

Copy the PHP code from the below box and paste it at the end of the functions.php file.

function canonical_r(){
if( !is_admin() ) {
$canonical_raw = (get_post_meta(get_the_ID(), 'canonical', true));
if ($canonical_raw)	 {
$canonical_url = preg_replace('/https*:\/\/|\s/', '', $canonical_raw);
?>
<link rel="canonical" href="<?php echo 'https://'.$canonical_url; ?>" />
<?php }} }
add_action('wp_head', 'canonical_r');

Step 2.Open an existing Post/Page or category and turn On the Custom Fields Option

Go to WordPress dashboard, then open an existing post to which you want to add meta keywords tag.

At the top right of your WordPress dashboard, you should see three dots. Click on those dots to open the WordPress options menu.
In the pop-up screen for Preferences, click on the “Panel” tab located on the left-hand side.
Under the “Additional” section, click on the “Custom Fields” toggle bar to toggle it to the on position. A page reload is required for this change. Make sure your content is saved before reloading. Then, click on the “Enable and Reload” button that appears.
After the page reloads, a “Custom Fields” section will appear below your WordPress text editor.

Step 3.Create A Custom Field for Canonical URL

Scroll down past the bottom of the WordPress text editor to the new “Custom Fields” section that was enabled in the previous step.
You should now see an “Add New Custom Field” area.

Here’s how you create the custom field for meta keywords:

  • Under the “Name” column, click on “Enter New” which is located below the “- Select -” dropdown menu.
  • In the text field that now appears under the “Name” column, type in “canonical”. This will assign a custom field for the meta keywords.
  • In the text field under the “Value” column, type in the URL.
  • Click on the “Add Custom Field” button to save this new custom field for the meta keywords.
Canonical URL in WordPress Custom Field

Step 4.Update the post and check the result

Go to the post and open the source code by pressing Ctrl+U keys and check the correctness of the Canonical URL.

Canonical rel in WordPress source

If this article is difficult for you to read in text, you can watch the video version below.

That’s it, If you have a question, be sure to comment for us to answer.

Share this post
Maya
Maya

Hi, my name is Maya and I’m a WordPress plugin developer. I created this website to share some of the helpful codes that I’ve used in my own projects.
If you’re looking for a custom plugin for your website, you can contact me by clicking on Hire a developer in the menu. I’d love to hear from you.

Articles: 56

4 Comments

  1. Hi,
    thank you very much for your reply. 🙂
    You mean I should add the canoncial url in the main url? So that the product or blog entry, for example, has an entry with itself? Or where is to placde the canoncial url exactly?
    Many of the urls that the GSC mentions are amp-urls and feed-urls, category urls and also some normal urls that exist but has not been crawled.
    How should the code look like to add it to the header?
    The rank math plugin may be a solution. What addon is managing this in rank math?

  2. Thank you for this great article. So I have still two questions:
    When I have more than 800 urls with a mark in the GSC that the canonial url is missing, how can I then edit them all at once?
    And 2nd question is: Which post has to have the canonial url entry, the original post or the similar post?

    • Hi Jonathan
      If you have more than one post on a specific topic, these posts will compete with each other on the SERP page, and you can use the canonical URL(with the main post URL) in older posts to transfer SEO points to the main post. (The answer to your second question)
      Regarding the first question, can I ask what pages these urls are related to? post, page, or something else? Normally, you will not need the canonical URL and you can ignore the mark in GSC, but if you still insist on adding the canonical tag to all URLs, I think it is possible to add this tag to the head of all pages automatically with a custom PHP code.
      I also think that the RankMath plugin will automatically add the canonical tag to all pages.

Leave a Reply

Your email address will not be published. Required fields are marked *