How to add rel=canonical tag to WordPress posts via Custom Fields

no duplicates - how to add canonical tag

I see two main reasons for using the rel=canonical tag:

Reason 1: You’re using a CMS or an eCommerce application that produces different url:s for the same content. That is actually quite common. The best solution for SEO would be to canonize the different urls into one, but that is not always possible.

Reason 2: You are deliberately creating two pages/posts/products with almost the same content for some reason. For example, you have made small differences like different titles or images, to fit for different audiences or to be shared on different social media platforms, or you’re simply testing different landing pages for your campaign.

In either case, the canonical tag will help you keep Google from penalizing the similar pages for being duplicate content. You just add the canonical tag to the pages that you don’t want Google to prioritize, and you let the tag show Google what page you want to rank in the SERP.

How to add the canonical tag to WordPress posts or pages

The only free plugin that I’ve found doing this in a satisfactory way is Yoast SEO. But if you favor another SEO plugin, you might want to use the following mod to your WordPress theme, which will allow you to add rel=canonical via Custom Fields.

Just add this code to your theme’s functions.php:

/* Begin Rel-canonical by custom fields */
// A copy of rel_canonical but to allow an override on a custom tag
function rel_canonical_with_custom_tag_override()
 if( !is_singular() )

 global $wp_the_query;
 if( !$id = $wp_the_query->get_queried_object_id() )

 // check whether the current post has content in the "canonical_url" custom field
 $canonical_url = get_post_meta( $id, 'canonical_url', true );
 if( '' != $canonical_url )
 // trailing slash functions copied from
 $link = user_trailingslashit( trailingslashit( $canonical_url ) );
 $link = get_permalink( $id );
 echo "<link rel='canonical' href='" . esc_url( $link ) . "' />\n";

// remove the default WordPress canonical URL function
if( function_exists( 'rel_canonical' ) )
 remove_action( 'wp_head', 'rel_canonical' );
// replace the default WordPress canonical URL function with your own
add_action( 'wp_head', 'rel_canonical_with_custom_tag_override' );
/* End Rel-canonical by custom fields */

When you have added this code to your theme’s functions.php, you just go ahead to the post where you want to add the rel=canonical tag, edit it and find the Custom Fields area below your content editor (if you don’t find it there, go to the top right corner, click Screen Options and select the checkbox Custom Fields).

The first time you do this, you have to click Enter new (see picture).

Add new custom field

Then you type canonical_url in the name field (left red arrow below), and the canonical url in the Value field (right red arrow below).

Add canonical tag

The next time you do it, you just use the drop-down selector to select canonical_url.

Check this article for reference how NOT to use canonical url: 5 common mistakes with rel=canonical

Continues after this ▼

I have used this code for WordPress 4.3.1 with positive results. If you have issues or suggestions, please let me know in a comment!


Worked professionally with Joomla! CMS since its birth in 2005. Today, I work more with WordPress, even though I'm following what happens in the Joomla world with much interest and still consider it a great CMS. Would love if you connect with me!

You may also like...

7 Responses

  1. Very useful post. Even if you are not a skilled coder, you can find here all the necessary information to achieve this functionality without using plugins.

    In may case, I’m using Yoast, so I’m covered, but if you don’t want to get all the whistles and bells from Yoast, this post provides a great way to add “canonical” tag.

    Thank you for this useful post.

  2. JoomlaCandy says:

    Thanks Vladimir, I’m glad you appreciated it. Love your blog!

  3. Thanks for sharing a great techniques for using canonical tag in wordpress

  4. JoomlaCandy says:

    Hello Narinder, and welcome! Glad you appreciated it my guide 🙂

  5. Lindsay says:

    So, the Yoast plugin feature works for if: a) I am dealing with duplicate content within my own site or b) I scrape someone else’s content (with permission!) and want to point back to their original post. Go it.

    But, what do I do when I want to do the inverse of b)? ie. Another blog wants to share my work but I want to point to my article as the Canonical piece. I can’t do that with Yoast; only they can. Do I then follow the Custom Fields method you outlined above and set the URL for the canonical/original/most important version of the work on the original post, itself?

    Thank you!

  6. Your Article And Content Is Great !! Really Helped !
    Your Post Thumbnail is mind blowing.. LOL.. and also very appropriate to the post.

Leave a Reply

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