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

What’s the Best Way to Start Learning Crypto Coding?

Hey everyone! I'm totally new to coding and I'm really eager to learn. I recently watched a video that mentioned picking a specific area...

Looking for Guidance to Transition from Front-End to Full-Stack Development

Hey everyone! I've been working as a front-end developer for over five years, primarily with React. Lately, I've been feeling a bit stuck, especially...

Help! My Old Employer Changed My Apple ID After I Quit

I made a mistake by using my personal Apple ID on my work Mac, and now I'm in a bit of a jam. When...

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

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...

Random Number Generator – Create Multiple Random Numbers Instantly

Welcome to our free, browser-based Random Number Generator – a fast and flexible tool designed to help you generate as many random numbers as...

Instant Online Dice Roller

Need a quick, trustworthy way to roll dice without digging through a board‑game box or cluttering your desk? Our free online dice roller is...

Docx To PDF

Need to turn a Word document into a fully‑formatted PDF—without installing software or handing over your email address? Our free DOCX‑to‑PDF converter does exactly...

xAI Grok Token Calculator

This tool is a simple xAI Grok token calculator that helps you estimate the number of tokens your input text might consume when working...

DeepSeek Token Calculator

This tool is a straightforward DeepSeek token calculator, created to help you estimate how many tokens your text may use when working with DeepSeek...

Latest Posts

Latest Questions