How to change a WordPress term’s slug before saving

You add the following snippet to your theme’s functions.php file;

The “wp_insert_term_data” filter allows you to modify a term’s name, slug or term_group before it is added to the database. This works for terms added via WP All Import and also manually through admin as well.

The filter passes all taxonomy terms, not just woocommerce attributes so you would need to add the taxonomy’s slug (attribute)

add_filter('wp_insert_term_data', function ($data, $taxonomy, $args) {

        //replace my-attribute with attribute slug
        if ($taxonomy == 'pa_my-attribute') {

            //Replace all < with lt-
            if (false !== strpos($data['name'], '&lt;')) {
                $data['slug'] = 'lt-' . $data['slug'];
            }

            //Replace all > with gt-
            if (false !== strpos($data['name'], '&gt;')) {
                $data['slug'] = 'gt-' . $data['slug'];
            }
        }

        return $data;

}, 10, 3);

To get the attribute slug go to Woocommerce > Attributes and copy the slug from there. Be sure to include pa_ before i.e my-attribute becomes pa_my-attribute.

enter image description here

As you can see it works with all < and > terms

enter image description here