OhhMuaOhhMua
  • Home
  • Hosting
    • Best Web Hosting
    • Free Web Hosting
    • VPS Hosting
  • WordPress
    • Solutions & Troubleshooting
    • Installation & Setup
    • Themes & Plugins
    • Security
  • Computer Tips
    • Gaming Errors & Solutions
    • PC Troubleshooting
    • Computer Hardware
  • Coupons & Deals
  • Contact Us
  • veo3 prompt generator
Reading: Step-by-Step Tutorial: Customizing Canonical Tags in WordPress
Share
Notification Show More
Font ResizerAa
OhhMuaOhhMua
Font ResizerAa
  • Computer Tips
  • Hosting
  • WordPress
Search
  • Home
  • WordPress
    • Installation & Setup
    • Security
    • Solutions & Troubleshooting
    • Themes & Plugins
    • Troubleshooting
  • Hosting
    • Free Web Hosting
    • VPS Hosting
    • Best Web Hosting
  • Computer Tips
    • PC Troubleshooting
    • Gaming Errors & Solutions
    • Computer Hardware
  • Coupons & Deals
  • veo3 prompt generator
Have an existing account? Sign In
Follow US
Copyright © 2024 ohhmua. All rights reserved.
OhhMua > Blog > WordPress > Solutions & Troubleshooting > Step-by-Step Tutorial: Customizing Canonical Tags in WordPress
Solutions & Troubleshooting

Step-by-Step Tutorial: Customizing Canonical Tags in WordPress

ohhmua
Last updated: September 15, 2024 3:25 pm
ohhmua
Share
4 Min Read
Customizing Canonical Tags in WordPress
Customizing Canonical Tags in WordPress
SHARE
Contents
Step-by-Step CustomizationExample Code for WooCommerce Canonical Customization:Explanation:How to Add This Code:Benefits:

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.

Customizing Canonical Tags in WordPress
Customizing Canonical Tags in WordPress

Here’s an example of how to customize the canonical tag for WordPress and WooCommerce:

Step-by-Step Customization

  1. Use Filters to Override Canonical URLs: Both Yoast SEO and Rank Math plugins allow you to modify canonical URLs using filters.

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

  1. Go to your WordPress dashboard.
  2. Navigate to Appearance > Theme Editor (or use a custom plugin).
  3. Open the functions.php file.
  4. 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.

You Might Also Like

SEO in the Age of AI: How to Make Google Understand and Value Your Content

Cannot Fetch Sitemap in Google Search Console

Discovered – Currently Not Indexed in Google Search Console: What It Means & How to Fix It

Why 2 Backlinks per Article Might Be Killing Your SEO

What is Obsidian? Why Developers Love This Note-Taking App

TAGGED:CanonicalCanonical Tags
Share This Article
Facebook Twitter Email Print
Leave a comment Leave a comment

Leave a Reply Cancel reply

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

Trending

How to Change WordPress Login URL Without Plugins
Solutions & Troubleshooting

How to Change WordPress Login URL Without Plugins (100% Success)

September 9, 2024
My Google Discover traffic skyrocketed after I did these 10 things
Solutions & Troubleshooting

My Google Discover traffic skyrocketed after I did these 10 things

May 14, 2025
Optimize WordPress
Solutions & Troubleshooting

Want Faster Load Times? Optimize WordPress This Way

May 16, 2025
How to Choose the Right Motherboard for Intel 14th Gen Processors
Computer Hardware

How to Choose the Right Motherboard for Intel 14th Gen Processors

April 22, 2025
Why 2 Backlinks per Article Might Be Killing Your SEO
Solutions & Troubleshooting

Why 2 Backlinks per Article Might Be Killing Your SEO

May 23, 2025
What is Obsidian
Solutions & Troubleshooting

What is Obsidian? Why Developers Love This Note-Taking App

May 20, 2025
Previous Next

You Might Also Like

How to Add a Read More to Product Descriptions in WooCommerce
Solutions & Troubleshooting

How to Add a “Read More” and “Show Less” Button to Product Descriptions in WooCommerce

May 5, 2025
WordPress Covers Every Website Type
Solutions & Troubleshooting

Blog, E-Commerce, or Forum? WordPress Covers Every Website Type!

April 29, 2025
Optimized Pagination thumbnail
Solutions & Troubleshooting

How I Optimized Pagination and Skyrocketed Traffic

April 28, 2025
How to Pick the Ideal Blogging Platform for Your Needs
Solutions & Troubleshooting

How to Pick the Ideal Blogging Platform for Your Needs

April 1, 2025
Previous Next
newsletter featured

Always Stay Up to Date

Subscribe to our newsletter to get our newest articles instantly!

Follow US on Social Media

Facebook Youtube Steam Twitch Unity

Copyright © 2024 ohhmua. All rights reserved.

OhhMua

Information

  • About
  • Terms & Conditions
  • Privacy Policy
Welcome Back!

Sign in to your account

Lost your password?