Customize Yoast Canonical URL Programmatically

When you install the Yoast plugin for WordPress, you will find that it will automatically generate the canonical tag for you on all web pages. For the majority of installs, you will never need to worry about this. The canonical URL is pretty much always going to be the same URL as the page. This exception will often occur with AMP HTML pages, but any good plugin should handle this for you automatically.

Recently I found an issue where a site was including some unique, custom content and proper listings on the archive pages on WordPress. The default behaviour of Yoast was to set the canonical URL of all archive pages for categories, post types and tags to the base page. This means that any page after page 1 will not get indexed by Google and you will find you get an error in Google Search Console to tell you the canonical URL does not match a URL that you have provided in the sitemap. Strange behaviour considering Yoast also generated the sitemap for this website. None the less, I needed a solution.

Using PHP Code to Change Canonical Tag

The code snippet below can be added to a custom plugin or into a theme. It will use a hook built into the Yoast plugin that will check to see if the “pg” query value exists and if it does, it will make sure that it adds it to the canonical URL before the tag is written to the page. The call to htmlspecialchars is just for safety to make sure that anyone adding dangerous values in the URL for PG will not be able to hijack your site and do some bad stuff.

add_filter('wpseo_canonical' , 'custom_change_canonical', 10,1);
function custom_change_canonical($url)
{
	global $post;
	if(isset($_GET['pg']))
	{
		return $url . "?pg=" . htmlspecialchars($_GET['pg']);
	}	
   return $url;
}

Related Articles

Related Questions

Help! My Lenovo Laptop Keeps Blue Screening – What Should I Do?

I've been having a tough time with my Lenovo Ideapad Gaming 3 15ACH6. It's been randomly blue screening, displaying errors like 'Critical Process Died'...

Upgrading My Rig: Is the 14600KF and 5060 Ti a Good Combo with Arctic Freezer 36?

Hey folks! I'm looking to upgrade my old setup which currently has an i5-3570K. My plan is to start by getting an RTX 5060...

How can I properly containerize an application that installs across the entire Linux filesystem?

I'm trying to containerize the Acronis Backup agent, but it's challenging because the installation spreads files all over different directories in the Linux filesystem....

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Latest Tools

Online Hash Generator – String to Hash Converter

Need to quickly generate a hash from a string? Whether you're verifying file integrity, securing data, or just experimenting with cryptographic tools, this simple...

Convert CSV To HTML Table

Need to quickly turn CSV data into an HTML table? Whether you're copying data from Excel, Google Sheets, or another spreadsheet, this tool makes...

Student Group Randomizer

Creating fair and balanced groups in the classroom can be time-consuming — especially when you're trying to avoid repetition, manage different skill levels, or...

Random Group Generator

Need to split a list of people, items, or ideas into random groups? Our free Random Group Generator makes it quick and easy. Whether...

Flip Text Upside Down – Free Online Tool

Ever wanted to flip your text upside down just for fun or to grab someone’s attention in a creative way? This free online Upside...

Raffle Ticket Generator

If you're running a fundraiser, charity draw, or local event and need raffle tickets fast, this free online tool lets you generate and print...

Latest Posts

Latest Questions