hooks – Use ID as slug for product category by default in WooCommerce?


We have a non-English store with WooCommerce. Product categories are created with the same slug as the title of non-English characters making the URL not so pretty nor shareable.

How can I make WordPress use only the ID of the as the slug whenever we create a product category? Did this for the WooCommerce products: https://stackoverflow.com/a/70457452/49318, and it works great. But after some research, I’m still not sure how to do this with the product categories?

Which of the hooks below should I use with add_action()?


And how can I use the ID as the slug for product category in the action?

I came up with this:

add_action( 'edited_product_cat', 'wpse373625_on_editing', 10, 3 );
function wpse373625_on_editing ( $id ){
    //This temporarily removes action to prevent infinite loops
    remove_action( 'edited_product_cat', 'wpse373625_on_editing' );
    wp_update_term( array(
        'term_id'        => $id,
        'slug' =>  $id , //Wordpress would generate the slug based on the post name
    //redo action
    add_action( 'edited_product_cat', 'wpse373625_on_editing', 10, 3 );

But it doesn’t work. Any help would be appreciated!

datasn.io 2 years 2021-12-25T05:41:49-05:00 0 Answers 0 views 0

Leave an answer