Bobcares

How to add Canonical link WordPress Programmatically

by | Nov 27, 2022

Wondering how to add Canonical link WordPress programmatically? Our in-house experts are here to help you out with this article. Our WordPress Support team is here to offer a lending hand with your queries and issues.

How to add Canonical link WordPress Programmatically

add Canonical link WordPress ProgrammaticallyA canonical URL defines a particular website address as the default option. This comes in handy when a web page has several URLs. Furthermore, defining a canonical URL prevents search engines like Google from deciding which URL to display.

Another advantage of adding a canonical URL is that it prevents getting a penalty from Search engines due to content showing more than once in the search results.

Today, our in-house experts are going to demonstrate how to add a canonical link in WordPress.

First, head to the theme’s folder and locate the header.php file.m Then add the following line after <head> and before the /<head> tag.

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

Additionally, WordPress now supports the following link tag as well:

rel=”canonical”

However, in order to add this tag, we have to add the following code to the functions.php file in the themes folder:

add_action( 'wp_head', 'rel_canonical' );

We can remove the canonical rel-tag by disabling the build-in function as seen below:

remove_action( 'wp_head', 'rel_canonical' );

Besides keeping search engine penalties at bay, it also stops Google bots from spending a lot of time crawling through multiple pages for the same content. Furthermore, it speeds up the time taken by Google to find new and updated content.

 

We can easily check if we have defined the canonical domain successfully by viewing the page source of the site’s homepage.

[Need assistance with a different issue? Our team is available 24/7.]

Conclusion

In conclusion, our Support Engineers introduced us to canonical links and how to add Canonical link WordPress programmatically in a jiffy. Drop us a line in the comments if you face any trouble.

PREVENT YOUR SERVER FROM CRASHING!

Never again lose customers to poor server speed! Let us help you.

Our server experts will monitor & maintain your server 24/7 so that it remains lightning fast and secure.

GET STARTED

0 Comments

Submit a Comment

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

Never again lose customers to poor
server speed! Let us help you.