I'm trying to adjust the URL structure of my WooCommerce product slugs by replacing specific characters programmatically. Here's my current situation:
Product Title: Desk Flag 15x22,5 Cm
Current slug example:
/product/dek-flag-15x225-cm-masa
Desired slug example:
/product/dek-flag-15x22-5-cm-masa
As you can see, I want to replace 22,5 in the slug with 22-5.
I tried several approaches, including using the wp_insert_post_data filter to modify the slug during product creation or updates, but I couldn't get it to work as expected.
Here’s the code I attempted:
function custom_product_slug_replace_comma($slug, $post_ID, $post_status, $post_type) {
// Sadece WooCommerce ürünleri için çalıştır
if ('product' === $post_type) {
// Virgülleri (-) ile değiştir
$slug = str_replace(',', '-', $slug);
}
return $slug;
}
add_filter('wp_unique_post_slug', 'custom_product_slug_replace_comma', 10, 4);
However, the slug remains unchanged after saving the product. I also want to ensure existing product slugs are updated to match the desired format.
Could someone guide me on how to achieve this properly? I'm open to either a dynamic solution that works for all future products or a one-time script that updates existing slugs.
Thanks in advance for your help