Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Add category slug to woocommerce single product url WordPress


I'm trying to edit the single product page url so that it shows the full hierarchy i.e. includes the parent category and all sub categories in the url. I've managed to do this using this combination: (you can view here

To remove /product-category/ I use this plugin

Then in Wordpress -> Settings -> Permalinks -> Product permalink base

set to: p/%product_cat%

<strong>Is there a way here to output the category slug instead of category? The issue is that it outputs the title in the url, which doesn't look great. e.g for this products it outputs this:</strong> Supplies/330mm-16-metre-eaves-protection-5u/

instead of this

So instead of the actual category title Roofing Supplies, it should be the slug roofing-supplies.

A way I've been getting around this is by installing Yoast then activating yoast breadcrumbs and the using this to change the titles:

wordpress -> products -> categories ->
then change the name of the category to the same as the slug, e.g. I've done this for Hand Tools and changed title to hand-tools, then at the bottom of the same category you will now have some yoast options just change the Breadcrumbs Title to what you want I changed to Hand Tools.

Now the products output with the correct url i.e. like here

The title on the product page is incorrect, it's showing as hand-tools, instead of Hand Tools

<strong>I think the easiest way to resolve is to use the category-slug in the Wordpress permalinks settings, so that I don't have to change the category title and use yoast to change the breadcrumb title - question is how do I output the category slug in wordpress permalinks settings?

Answers (3)


Sabby Sam answers:

I guess you need to change your permalink setting in admin panel. In wordpress yoast setting there is an option hide category slug check that one. If you want to change the product slug then try to edit in plugin.

willcm comments:

I wish it was that simple!

What I don't understand is why there is not a category slug i can insert on Wordpress permalink settings,

here suggests that %category% will work - but it doesn't?

Is there a way I can manually code a new structure tag that uses the category slug?


Giri answers:

Hey I think your codecanyon plugin does not sanitize category title. Can you message me your codecanyon plugin? I'll try to fix it..

willcm comments:

I've disabled the codecanyon plugin and still have the same issue, will email you the plugin though


MDan answers:


See this post:
I think you could do this within your .htaccess file.

See also this 3 threads:

Hope this helps.

willcm comments:

Sorted myself using this code here:

thanks to this chap -

remove_filter( 'post_type_link', 'woocommerce_product_cat_filter_post_link', 10, 2 ); // for woocommerce < 2
remove_filter( 'post_type_link', 'woocommerce_product_post_type_link', 10, 2 ); // for woocommerce >= 2
add_filter( 'post_type_link', 'woocommerce_subcategory_permalink', 10, 2 );
function woocommerce_subcategory_permalink( $permalink, $post ) {

// Abort if post is not a product
if ( $post->post_type !== 'product' )
return $permalink;

// Abort early if the placeholder rewrite tag isn't in the generated URL
if ( false === strpos( $permalink, '%product_cat%' ) )
return $permalink;

// Get the custom taxonomy terms in use by this post
$terms = get_the_terms( $post->ID, 'product_cat' );

if ( empty( $terms ) ) {
// If no terms are assigned to this post, use a string instead (can't leave the placeholder there)
$permalink = str_replace( '%product_cat%', _x('product', 'slug', 'woocommerce'), $permalink );
} else {
// Get the first term
$first_term = array_shift( $terms );

// Get the hierachical product_category
$parents = woo_get_term_parents( $first_term->term_id, 'product_cat' );

// Replace the placeholder rewrite tag hierachical product_category
$permalink = str_replace( '%product_cat%/', $parents, $permalink );

return $permalink;

/* Credits: */
if ( ! function_exists( 'woo_get_term_parents' ) ) {
function woo_get_term_parents( $id, $taxonomy ) {
$chain = '';
$parent = &get_term( $id, $taxonomy );
if ( is_wp_error( $parent ) )
return $parent;

$name = $parent->slug;

if ( $parent->parent && ( $parent->parent != $parent->term_id ) ) {
$chain .= woo_get_term_parents( $parent->parent, $taxonomy);

$chain .= $name."/";
return $chain;
} // End woo_get_term_parents()