How to Add rel=canonical to WordPress Pages for Better SEO

How to add rel canonical in wordpress; Add canonical url to wordpress page; Wordpress rel canonical tag setup; Set canonical link in wordpress; Wordpress add canonical to homepage; Use yoast seo for canonical urls; Automatic canonical url wordpress; Wordpress canonical url plugin; Fix duplicate canonical tags wordpress; Add rel canonical to wordpress posts;

Explanation

Adding a rel=canonical tag to your WordPress pages and posts helps search engines understand which URL is the preferred version when you have similar or duplicate content. This can improve your site's SEO by preventing duplicate content issues.

Here's a simple way to add a canonical tag:

  • The function checks if you're viewing a single post or page using is_singular().
  • It then grabs the URL of the current post or page with get_permalink().
  • Finally, it outputs the canonical link tag in the <head> section of your site.

If you're using Yoast SEO, it might already add its own canonical tags. To avoid duplicates, the code checks if Yoast is active and removes its canonical tag using remove_action().

This setup ensures you have a single, clean canonical tag on each page, which is great for SEO and keeps search engines happy!

Code

<?php
// Add rel=canonical to WordPress pages and posts

function wp_dudecom_add_canonical_tag() {
    if (is_singular()) {
        global $post;
        
        // Get the canonical URL
        $canonical_url = get_permalink($post->ID);

        // Output the canonical link tag
        echo '<link rel="canonical" href="' . esc_url($canonical_url) . '" />' . "\n";
    }
}
add_action('wp_head', 'wp_dudecom_add_canonical_tag');

// Remove duplicate canonical tags if Yoast SEO is active
function wp_dudecom_remove_yoast_canonical() {
    if (defined('WPSEO_VERSION')) {
        remove_action('wpseo_head', array(WPSEO_Frontend::get_instance(), 'canonical'), 10);
    }
}
add_action('wp', 'wp_dudecom_remove_yoast_canonical');
?>

Instructions

File Location: Add the following code to your theme's functions.php file or a custom plugin file.

Prerequisites:

  • Access to your WordPress theme files or the ability to create/edit a plugin.
  • Basic understanding of how to edit PHP files.
  • Optional: Yoast SEO plugin installed (if applicable).

Implementation Steps:

  1. Access your WordPress Dashboard: Log in to your WordPress admin panel.
  2. Navigate to Appearance > Theme Editor: If editing functions.php, or Plugins > Add New > Create a new plugin if you prefer using a plugin file.
  3. Locate the functions.php file: In the Theme Editor, find the functions.php file in the right-hand sidebar under "Theme Files".
  4. Copy and Paste the Code: Insert the provided code snippet at the end of the functions.php file or in your custom plugin file.
  5. Save Changes: Click the "Update File" button to save your changes.
  6. Verify Implementation: Visit a single post or page on your site and view the page source to ensure the rel=canonical tag is present in the <head> section.
  7. Check for Yoast SEO: If you have Yoast SEO installed, ensure that the duplicate canonical tag is removed by checking the page source for only one rel=canonical tag.

If you need help with implementation or require more advanced functionality, consider using the services of wp-dude.com.