To customize the canonical tag in WordPress and WooCommerce, especially when using SEO plugins like Yoast SEO or Rank Math, you can modify the canonical URL based on certain conditions, such as for the shop page, product categories, or home page.
Here’s an example of how to customize the canonical tag for WordPress and WooCommerce:
Step-by-Step Customization
-
Use Filters to Override Canonical URLs: Both Yoast SEO and Rank Math plugins allow you to modify canonical URLs using filters.
-
Add Custom Code: The following code checks if the current page is the shop page, a product taxonomy page, or the home page, and changes the canonical URL accordingly:
To point the canonical URL to a main link, simply paste the following code into the functions.php
file of the active theme. The path will be:
wp-content/themes/{your theme folder}/functions.php
add_filter( 'wpseo_canonical', 'ohhmua_rank_math_canonical_url', 99 );
add_filter('rank_math/frontend/canonical', 'ohhmua_rank_math_canonical_url', 99);
function ohhmua_rank_math_canonical_url($canonical_url){
if(is_shop()){
$canonical_url = get_permalink( wc_get_page_id( 'shop' ) );
}elseif (is_product_taxonomy() || is_category() || is_tag()){
$canonical_url = get_term_link(get_queried_object_id());
}elseif (is_home()){
$canonical_url = get_permalink(get_option( 'page_for_posts' ));
}
return $canonical_url;
}
Example Code for WooCommerce Canonical Customization:
// For Yoast SEO
add_filter( 'wpseo_canonical', 'ohhmua_custom_canonical_url', 99 );
// For Rank Math SEO
add_filter('rank_math/frontend/canonical', 'ohhmua_custom_canonical_url', 99);
function ohhmua_custom_canonical_url($canonical_url) {
// If on WooCommerce shop page
if (is_shop()) {
$canonical_url = get_permalink( wc_get_page_id( 'shop' ) );
// If on product taxonomy (category, tag)
} elseif (is_product_taxonomy() || is_category() || is_tag()) {
$canonical_url = get_term_link( get_queried_object_id() );
// If on the blog home page
} elseif (is_home()) {
$canonical_url = get_permalink( get_option( 'page_for_posts' ) );
}
// Return modified canonical URL
return $canonical_url;
}
Explanation:
- Filters for SEO Plugins: The code adds filters for both Yoast SEO (
wpseo_canonical
) and Rank Math (rank_math/frontend/canonical
), so it works with either plugin. - Conditional Checks: The canonical URL is modified based on specific page types:
is_shop()
: Checks if the current page is the WooCommerce shop page.is_product_taxonomy() || is_category() || is_tag()
: Handles product categories, tags, or other taxonomy pages.is_home()
: Checks if it’s the blog home page.
How to Add This Code:
- Go to your WordPress dashboard.
- Navigate to Appearance > Theme Editor (or use a custom plugin).
- Open the
functions.php
file. - Paste the code at the bottom of the file and save.
Benefits:
- Custom canonical URLs help ensure that search engines are indexing the correct version of a page.
- Prevents potential SEO issues caused by duplicate content or incorrect URLs.
This customization can greatly improve your site’s SEO, especially for WooCommerce stores where canonical URLs might need more specific handling.
Hi, I’m Nghia Vo: a computer hardware graduate, passionate PC hardware blogger, and entrepreneur with extensive hands-on experience building and upgrading computers for gaming, productivity, and business operations.
As the founder of Vonebuy.com, a verified ecommerce store under Vietnam’s Ministry of Industry and Trade, I combine my technical knowledge with real-world business applications to help users make confident decisions.
I specialize in no-nonsense guides on RAM overclocking, motherboard compatibility, SSD upgrades, and honest product reviews sharing everything I’ve tested and implemented for my customers and readers.