Subscribe via

Automatically Deal With Duplicate Content in WordPress

Thaya Kareeson

The Official Google Webmaster Central Blog recently announced that they now support the ability for you to specify the preferred version of a URL. This means that following these guidelines will help you eradicate your site’s duplicate contents that Googlebot sees.

Why would you do such a thing? Simply because many black-hat SEO folks abuse duplicate content to gain search ranking and Google tries really hard to detect and penalize them. The last thing you want to do is present yourself as an SEO spammer to Google.

In the post, Google is saying that you can easily tell the Googlebot the preferred version of a page by specifying the following tag within your <head></head> tag:

<link rel="canonical" src="http://example.com/your/preferred/version/of/this/page"/>

SEO No Duplicate WordPress Plugin

I have made a plugin that does this for you. Visit the plugin page!

No Plugin? No Problem

If you don’t want to download another plugin, you can just place the following code into your functions.php file within your theme folder.

function set_canonical() {
  if ( is_single() or is_page() ) {
    global $wp_query;
    echo '<link rel="canonical" href="'.get_permalink($wp_query->post->ID).'"/>';
  }
}
add_action('wp_head', 'set_canonical');

Update
It turns out that the code above does not handle all types of WordPress pages. I highly recommend downloading the SEO No Duplicate WordPress plugin to do this for you instead!

That’s it! Now if you go to any version of your post page, you will see the little link on the top of the page publicly telling bots that the preferred version of the page is the permalink version. Take that duplicate contents!

Save and Share
StumbleUpon
Reddit

21 Responses to “Automatically Deal With Duplicate Content in WordPress”

[go to last comment]
  1. redwall_hp

    I read about this on Yoast the other day. It's interesting.

    But what if a scraper puts this on their page, and you don't have it? Would that make theirs the preferred URL, and kill your page? o_0

  2. Artem Russakovskii

    redwall_hp, I don't think you quite understood the point of these canonical urls – they're for your own pages, not other sites.

    To address your concern, see this post from their blog:
    Can this link tag be used to suggest a canonical URL on a completely different domain?
    No. To migrate to a completely different domain, permanent (301) redirects are more appropriate. Google currently will take canonicalization suggestions into account across subdomains (or within a domain), but not across domains. So site owners can suggest www.example.com vs. example.com vs. help.example.com, but not example.com vs. example-widgets.com.

  3. Thaya Kareeson

    @Artem Russakovskii
    I wrote a full reply to @redwall_hp before I read this comment, but now I realized that you said what I was going to say before I said it :). Thanks for helping moderate comments!

  4. Thaya Kareeson

    @redwall_hp
    Looks like @Artem Russkakovskii got the answer below.

  5. redwall_hp

    Okay, I missed that when I read it the first time around. Good to know. :)

  6. Thanks for the function code. I appreciate the option!

  7. brad hart

    I agree thanks for the copy paste code. I do this with a lot of my plugins so it is nice thing to see readily available.

  8. Thaya Kareeson

    You're very welcome!

  9. Joost de Valk

    As said in the post about your plugin, this only works for single posts, not for pages (though that should be easy to add), not for pages posts, not for tags, not for categories, not for date based or author archives, etc. :) If you want something that does support that, try my Canonical plugin: http://yoast.com/canonical-url-links/

    Other than that: great work Thaya, for sharing this with the community!

  10. john

    I have installed the plugin and it works, but it does not work for cached posts made with the global translator plugin. Is there any way to include the code for these posts?

  11. Yep! the above code is only for single post, and cann't handle other WordPress pages.

  12. Thaya Kareeson

    @Joost de Valk
    Thank you for visiting! It's an honor to have you here. This is quite weird because I just happened to skip reading my RSS feeds for one day (when you released that plugin) and it seems I have released a duplicate plugin (that doesn't work as well as yours). Awesome job on the Canonical URL links plugin. I'm not sure where I would want to go with SEO No Duplicate yet, but please don't hold it against me if I try to enhance it to work as well as yours.

  13. Thaya Kareeson

    @John
    Have you tried clearing the global translator cache? Maybe it just hasn't been regenerated.

  14. Thaya Kareeson

    @DGs
    Yup! That's true. I would recommend using Joost's plugin for now until I update the plugin.

  15. Joost de Valk

    Hey Thaya, of course I don't mind!! That's what open source is all about :)

  16. Thaya Kareeson

    Released 0.2
    - Added support for all types of WordPress pages
    - Added canonical user overrides via custom fields

  17. Thaya Kareeson

    Released SEO No Duplicate 0.2
    - Added support for all types of WordPress pages
    - Added canonical user overrides via custom fields
    I highly recommend using the plugin to do this for you instead of using the code above!

  18. nop

    I agree no plugins.

  19. Ashley Trends

    Is the plugin (SEO no Duplicate) can delete duplicate posts also?

  20. Thaya Kareeson

    That's not the point of SEO No Duplicate. Google is supposed to recognize your canonical URL settings and get rid of the duplicate post entry on their side.

  21. traffic

    so it sound s like no plugs is best here ….. exactly where in the string on that page does this piec of code go ?? function set_canonical() {
    if ( is_single() or is_page() ) {
    global $wp_query;
    echo '<link rel="canonical" href="'.get_permalink($wp_query->post->ID).'"/>';
    }
    }
    add_action('wp_head', 'set_canonical');

    where on that page them function codes correct>.

[go to first comment]

Leave a Reply