rel=”canonical”-URL for WordPress Theme

Duplicate content is always an issue in search engines, especially in the context of content management systems and blogging applications. Therefore, they decided together (Google, Yahoo and Microsoft) for a notice using the link tag.

In essence, provides the tag with the rel attribute canonical only a reference to the preferred URL. Once again a new discussion started in the WP Hacker List about this new possibility, which I can not understand. WordPress offers plenty of opportunities so that this form is not necessary to be included in the core, there is sufficiently different and more important topics and the content of a theme should be broadly still lie with the developer. I will show you in this post a simple and quick way to integrate the attribute.

Without getting in details and to enter the SEO world, I will pass on some links, which sufficiently explained the issue with enough background on this topic. Useful and concise is the article „Specify your canonical“ and Yoast provides information and a Plugin on this topic.

As always, you can use various Plugins or expand your theme for indication for the search engines. The second possibility is shown below in my favorite code.

<?php if ( is_singular() ) echo '<link rel="canonical" href="' . get_permalink() . '" />'; ?>

The above code has to be in the head of the theme, usually in the header.php of the theme and you're done. From now, every single page and post has the notice on the preferred URL. For the query of page or post takes care the Conditional Tag is_singular() since WordPress version 1.5 in the core.

Comments are closed.

13 comments

  1. Matt Wiebe

    Good tip on the canonical urls. I'll have to use that in the future.

    However, although is_singular() might work, the proper conditional tag is is_single(): http://codex.wordpress.org/Conditional_Tags#A_Single_Post_Page

  2. Michael

    Matt, is_singular() applies the rel="canonical"-link to single post view AND pages.

  3. ...paul

    Thanks for that; it looks like a worthwhile addition to the header file. Much appreciated. 🙂

  4. Mário Andrade

    It's not that Simple

    If my URL is already canonical i'm going to say this page's canonical url is this page and that's a bad use of it.

    If the site's url is p=387 and you want to say the canonical url is /post-about-something/ the code you shared isn't enought.

  5. Leeiio

    I agree with @Mário Andrade ,but it can be use between page/ and page/1/ if you don't want to show the url page/1/ in google or any other Search Engine.

  6. Binny V A

    I wrote a post about this topic a while ago - Avoid Duplicate Content - Use Canonical URL in WordPress

  7. 5ubliminal

    Check out Canonicals WordPress Plugin.

  8. Atw

    Very nice

  9. iceflatline

    Nice indeed. One problem for me though, it does not seem to place the rel="canonical" link to the home page. Any work-around or is this not a concern?

  10. Corporate Graphic Design

    Nice - very very useful for SEO purposes - would recomment people follow this =)

  11. Elyot

    I am using this wp plug in www. yoast.com/ wordpress/ canonical/ for canonical urls

  12. Stjepan Tafra

    i've read that wordPress 2.9 adds a new tag to the page header which specifies the canonical path to the actual content. do you know how i can enable this feature?

  13. Michael

    Stjepan, here is a article http://wpengineer.com/support-in-wordpress-for-relcanonical/