WooCommerce: How to Shorten Product Category Permalinks

Learn how to shorten your WooCommerce store’s product category permalinks! Perfect for enhancing readability and SEO.

The Problem

Recently, a client reached out to me with a request to simplify the product category URLs of his online store.

By default, WooCommerce generates product category permalinks that include the entire hierarchy of parent categories. This can lead to long and unwieldy URLs such as the following:

https://example.com/product-category/grand-parent-category/parent-category/child-category/

The client expressed worries about the readability of his online store’s URLs, and was keen to adopt a shorter and simpler permalink structure instead:

https://example.com/c/child-category/

The Solution

Alas, WooCommerce doesn’t offer a built-in way to shorten product category URLs by removing parent category slugs. But luckily, there’s a simple way to achieve this with a bit of custom coding:

add_action( 'init', function() {
    $product_cat_taxonomy = get_taxonomy( 'product_cat' );
    $product_cat_taxonomy->rewrite = false;
    register_taxonomy('product_cat', 'product', $product_cat_taxonomy);
});

Adding the above code snippet to your theme’s functions.php file will effectively disable the default rewriting of product category URLs to include the entire hierarchy of parent categories.

How it works

The above snippet uses two built-in WordPress functions to modify the behavior of WooCommerce product category permalinks:

  1. First, the get_taxonomy function retrieves the taxonomy object for WooCommerce’s product categories.
  2. Then, the taxonomy’s rewrite property is set to false, effectively telling WooCommerce not to use its default URL rewriting mechanism for product categories.
  3. Last but not least, the snippet calls the register_taxonomy function with the updated taxonomy object. This applies the change by overwriting the original object.(( register_taxonomy() – Function | Developer.WordPress.org ))

From now on, product category URLs will no longer include the parent category slugs, leading to shorter and more straightforward permalinks for your clients and/or users!

Don’t forget to redirect!

After changing your site’s URL structure, it’s essential to implement 301 redirects.(( 301 Moved Permanently – HTTP | MDN )) Editing your site’s .htaccess file or installing a WordPress plugin are common methods for managing WooCommerce redirects.

A 301 redirect passes the link equity (ranking power) from the original URL to the new URL, helping to maintain your site’s search performance.(( Redirects: How To Use, SEO Impact & Types (301 vs 302) – Moz )) In addition to redirects, you might also want to update any internal links within your site’s content or navigation menus to reflect the new permalink structure.

Sources

Published

Leave a comment

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