Add a canonical tag to WordPress header.php

Adding the canonical tag to your WordPress blog might be essential if you do not want search engines to see your content twice (= duplicate content) and thus giving you a penalty as such.

To add this tag, simply go to your theme’s folder and find header.php. Now, add the following somewhere after <head> but before </head>

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

Clear your cache and see if this meta tag was added.

Since WordPress is constantly innovating and adding features to its core, this CMS now natively supports the rel="canonical" link-tag. To request WordPress to add this tag to your website’s pages, you have to paste this code into functions.php (found in your themes directory, such as wp-content/themes/YourTheme):

add_action( 'wp_head', 'rel_canonical' );

In case you want to specifically remove the canonical rel-tag in the <head> of your WordPress blogposts and pages, you may choose to specifically disable the build-in rel_canonical() function that WordPress offers:


remove_action( 'wp_head', 'rel_canonical' );


Author Bio

Thank you for your interest in my blog! On this miniblog, I write mostly short (technical) blog posts that might interest other people. Read more about me or feel free to contact me.


4 thoughts on “Add a canonical tag to WordPress header.php

Leave a Reply to syed Murtaza Ali Cancel reply

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