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
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
    • Computer Hardware
  • Coupons & Deals
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

My Google Discover traffic skyrocketed after I did these 10 things

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

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

How I Optimized Pagination and Skyrocketed Traffic

How to Pick the Ideal Blogging Platform for Your Needs

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 Stories

Internet Connected But Can't Access Some Websites
Computer Tips

Internet Connected But Can’t Access Some Websites: Causes and Fixes

September 25, 2020
what is computer hardware
Computer Hardware

What is Computer Hardware? Everything You Need to Know

August 23, 2020
How to Install WordPress on cPanel
Installation & Setup

How to Install WordPress on cPanel: A Beginner-Friendly Guide

April 24, 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
WordPress Covers Every Website Type
Solutions & Troubleshooting

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

April 29, 2025
5 Signs It's Time to Upgrade Your Computer 5
Computer Tips

5 Signs It’s Time to Upgrade Your Computer

July 24, 2023

You Might Also Like

Protecting Your WordPress Site from File Upload Vulnerabilities
Solutions & Troubleshooting

Protecting Your WordPress Site from File Upload Vulnerabilities

September 26, 2024
Title and Meta Description Mismatch on Google
Solutions & Troubleshooting

Title and Meta Description Mismatch on Google[How to fix]

September 25, 2024
Contact button
Solutions & Troubleshooting

Contact button in the footer with added call button shake effect

September 21, 2024
Guide to Creating a Beautiful Promotion Notification Box for Your Website
Solutions & Troubleshooting

How to Create a Stunning Notification Box for Your Website

September 20, 2024
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?