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.

Continues after this ▼

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() )
 return;

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

 // 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 http://core.trac.wordpress.org/attachment/ticket/18660/canonical.6.patch
 $link = user_trailingslashit( trailingslashit( $canonical_url ) );
 }
 else
 {
 $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

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!

JoomlaCandy

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

5 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 🙂

Leave a Reply

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