Canonical tags are essential for SEO. Whether you use a plugin or add them manually, implementing them correctly improves search engine rankings and prevents duplicate content issues.

We will provide several examples of how to add canonical tags to WordPress without a plugin. Each page should have a canonical tag in the ‘head’, with some exceptions such as 404 pages, etc.

In fact, most WordPress themes generate a canonical tag automatically, especially for single posts and the front page. However, WordPress themes differ in how they generate meta tags. Additionally, most do not generate canonical tags for different archives, such as: categories, tags, etc.

In this guide, we will share our knowledge with you on how to generate canonical tags, including for paginated content.

What is Canonical Tag?

A canonical tag is an HTML element used to indicate the preferred version of a webpage when there are multiple pages with similar or duplicate content. It helps search engines understand which page to index and rank, avoiding issues like duplicate content penalties. The canonical tag is placed in the

<head>
section of a webpage and points to the original or most authoritative version of the content.

How to generate canonical tags, including for paginated content?

First, check which pages have a canonical tag generated by the CMS, theme, or plugin.

Make sure not to generate a canonical tag where one already exists, as this will cause duplicates.

If you’re on the front page, the canonical tag will look similar to this example:

<link rel="canonical" href="https://example.com/">

If you don’t have a canonical tag on any type of page, here’s the code that generates a canonical tag for all types of pages:

<link rel="canonical" href="<?php
$canonical_url = '';
if (is_front_page() || is_singular()) {
$canonical_url = get_permalink(); // URL for frontpage and singular
} elseif (is_home() || is_archive()) {
if (is_paged()) {
$canonical_url = get_pagenum_link(get_query_var('paged')); // Paginated page URL (e.g. /page/2/)
} else {
$canonical_url = get_pagenum_link(); // Main blog/archives URL
}
} elseif (is_search()) {
$canonical_url = home_url('/?s=' . urlencode(get_search_query())); // Search results URL
}
// Clean query strings and normalize slashes
$canonical_url = strtok($canonical_url, '?');
$canonical_url = preg_replace('#(?<!https:|http:)/+#', '/', $canonical_url);
echo esc_url($canonical_url);
?>">

Avoid duplicates!

If, for example, you have previously had Canonical FrontPage and single post/page, this code can add 2nd tag and cause duplicates.

To cancel the generation of Canonical in FrontPage and single post/page from our code, replace this part at the beginning of the code.

Replace this:

<link rel="canonical" href="<?php
$canonical_url = '';
if (is_front_page() || is_singular()) {

With this:

if (!is_front_page() && !is_singular()) { ?>
<link rel="canonical" href="<?php
$canonical_url = '';

But if you want the Canonical tag to be generated by our code and cancel the other one generated by the theme or Yoast WP – SEO, enter the code in

functions.php

 

Cancel Canonical tag generated from the theme:

remove_action('wp_head', 'rel_canonical');

 

Cancel Canonical tag generated by Yoast WP – SEO:

add_filter('wpseo_canonical', '__return_false');

 

We shared how to generate a Canonical tag without a plugin, but we do not encourage you not to use an SEO plugin.

You can use one such as: Yoast SEO, Rank Math, All in One SEO (AIOSEO) or any other SEO plugin.