Support

Search results for ""

Sorry, no results found. Perhaps you would like to search the documentation?
All Topics
Simon Gevcen

Updating a product title should automatically update the product slug

Is this possible that whenever we update a product title the slug gets automatically adjusted? Thanks

10 months ago
Simon Gevcen

Hi Stefan,

I tried as below but then when I want to Add New Line, it causes some conflicts in your plugin.
Any help? Thanks

function update_woocommerce_product_slug( $post_id ) {
// Check if this is a WooCommerce product and not an autosave
if ( get_post_type( $post_id ) === ‘product’ && ! wp_is_post_autosave( $post_id ) && ! wp_is_post_revision( $post_id ) ) {

// Get the product title
$product_title = get_the_title( $post_id );

// Generate a new slug from the product title
$new_slug = sanitize_title( $product_title );

// Update the post slug if it is different
wp_update_post( array(
‘ID’ => $post_id,
‘post_name’ => $new_slug
) );
}
}
add_action( ‘save_post’, ‘update_woocommerce_product_slug’ );

10 months ago
Stefan van den Dungen Gronovius
Developer

The issue is that the code is creating a loop (not related to our plugin ;) )
calling wp_update_post triggers the ‘save_post’ hook again.
You could check if the name is different and only then update the post.

10 months ago
Simon Gevcen

You can closr this ticket thanks

10 months ago

You must be logged in to reply to this topic.