Remove the product and product-category word in WooCommerce

Simply remove the product and product-category word in WooCommerce in the most simple way.

By default, in the permalink of WooCommerce, there will be the words 'product' and 'product-category'. This helps distinguish the post's path or its categories. WooCommerce discourages removing these words to avoid duplicate errors or conflicting links between regular posts and the product. However, many people still want to remove them so that the path of the product is shorter, more search engine friendly.

Remove the product and product-category word in WooCommerce

There are many different methods to do this, both plugin and code snippets. I see some sites sharing code snippets that help customize WooCommerce links without using plugins. Specifically here is the code:

However, the fact that this code is not optimal should seriously affect the performance of the website. Specifically, they can overload the host CPU and RAM, resulting in a very slow website load, even a 503 (overload) error. I have noted this situation on the website of some customers. You absolutely should not use them.

There are many different methods to do this, both plugin and code snippets. I see some sites sharing code snippets that help customize WooCommerce links without using plugins. Specifically here is the code:

[php]
function brt_remove_slug( $post_link, $post ) {
if ( !in_array( get_post_type($post), array( 'product' ) ) || 'publish' != $post->post_status ) {
return $post_link;
}
if('product' == $post->post_type){
$post_link = str_replace( '/product/', '/', $post_link ); //replace the 'product' word with your current slug
}else{
$post_link = str_replace( '/' . $post->post_type . '/', '/', $post_link );
}
return $post_link;
}
add_filter( 'post_type_link', 'brt_remove_slug', 10, 2 );

/* Fix 404 error when removed product slug */
function brt_woo_product_rewrite_rules($flash = false) {
global $wp_post_types, $wpdb;
$siteLink = esc_url(home_url('/'));
foreach ($wp_post_types as $type=>$custom_post) {
if($type == 'product'){
if ($custom_post->_builtin == false) {
$querystr = "SELECT {$wpdb->posts}.post_name, {$wpdb->posts}.ID
FROM {$wpdb->posts}
WHERE {$wpdb->posts}.post_status = 'publish'
AND {$wpdb->posts}.post_type = '{$type}'";
$posts = $wpdb->get_results($querystr, OBJECT);
foreach ($posts as $post) {
$current_slug = get_permalink($post->ID);
$base_product = str_replace($siteLink,'',$current_slug);
add_rewrite_rule($base_product.'?$', "index.php?{$custom_post->query_var}={$post->post_name}", 'top');
add_rewrite_rule($base_product.'comment-page-([0-9]{1,})/?$', 'index.php?'.$custom_post->query_var.'='.$post->post_name.'&cpage=$matches[1]', 'top');
add_rewrite_rule($base_product.'(?:feed/)?(feed|rdf|rss|rss2|atom)/?$', 'index.php?'.$custom_post->query_var.'='.$post->post_name.'&feed=$matches[1]','top');
}
}
}
}
if ($flash == true)
flush_rewrite_rules(false);
}
add_action('init', 'brt_woo_product_rewrite_rules');

/* Fix 404 error when create a new product */
function brt_woo_new_product_post_save($post_id){
global $wp_post_types;
$post_type = get_post_type($post_id);
foreach ($wp_post_types as $type=>$custom_post) {
if ($custom_post->_builtin == false && $type == $post_type) {
brt_woo_product_rewrite_rules(true);
}
}
}
add_action('wp_insert_post', 'brt_woo_new_product_post_save');

// Remove Parent Category from Child Category URL
add_filter('term_link', 'brt_no_category_parents', 1000, 3);
function brt_no_category_parents($url, $term, $taxonomy) {
if($taxonomy == 'category'){
$term_nicename = $term->slug;
$url = trailingslashit(get_option( 'home' )) . user_trailingslashit( $term_nicename, 'category' );
}
return $url;
}

// Rewrite new url
function brt_no_category_parents_rewrite_rules($flash = false) {
$terms = get_terms( array(
'taxonomy' => 'category',
'post_type' => 'post',
'hide_empty' => false,
));
if($terms && !is_wp_error($terms)){
foreach ($terms as $term){
$term_slug = $term->slug;
add_rewrite_rule($term_slug.'/?$', 'index.php?category_name='.$term_slug,'top');
add_rewrite_rule($term_slug.'/page/([0-9]{1,})/?$', 'index.php?category_name='.$term_slug.'&paged=$matches[1]','top');
add_rewrite_rule($term_slug.'/(?:feed/)?(feed|rdf|rss|rss2|atom)/?$', 'index.php?category_name='.$term_slug.'&feed=$matches[1]','top');
}
}
if ($flash == true)
flush_rewrite_rules(false);
}
add_action('init', 'brt_no_category_parents_rewrite_rules');

/* Fix 404 error when create a new category */
function brt_new_category_edit_success() {
brt_no_category_parents_rewrite_rules(true);
}
add_action('created_category','brt_new_category_edit_success');
add_action('edited_category','brt_new_category_edit_success');
add_action('delete_category','brt_new_category_edit_success');
[/php]

However, the fact that this code is not optimal should seriously affect the performance of the website. Specifically, they can overload the host CPU and RAM, resulting in a very slow website load, even a 503 (overload) error. I have recorded this situation on the website of some customers. You absolutely should not use them.

In this article, I will introduce to you the simplest but most effective way, with little effect on the performance of the website.

1. First, you need to install and activate a plugin called Premmerce WooCommerce Permalink Manager Pro.

Remove the product and product-category word in WooCommerce remove the product and product-category word in woocommerce Remove the product and product-category word in WooCommerce install active premmerce woocommerce permalink manager pro

2. Next, click the Allow & Continue button.

Remove the product and product-category word in WooCommerce remove the product and product-category word in woocommerce Remove the product and product-category word in WooCommerce click on allow and continue button

3. The plugin setup interface will look like this. Choose the type of path structure that suits your needs.

Remove the product and product-category word in WooCommerce remove the product and product-category word in woocommerce Remove the product and product-category word in WooCommerce woocommerce permalink settings

Note:

  • Use WooCommerce settings: use WooCommerce permalink settings. This setting you can customize at Settings > Permalinks. It is located right below the permalink setting of WordPress.
  • Category slug: use the path of product catalog type domain.com/category.
  • Product slug: use the product path type domain.com/product.
  • Full category path: use the path to the product catalog type domain.com/parent-category/sub-category.
  • Product slug with primary category: use the product path type domain.com/category/product.
  • Full product path: Use the product path type domain.com/parent-category/sub-category/product.

Check the Use primary category and Add canonicals sections to increase the compatibility of the plugin. Finally, click the Save Changes button to save.

4. Clear the website cache (if you use a cache creation plugin) and check the results. It's simple, right? Good luck!

You know another simple and more effective method to remove the word product, product-category from the path of WooCommerce? Do not forget to share it with us in the comment box below.

- Remove the product and product-category word in WooCommerce - For more article, please visit our blog -

Remove the product and product-category word in WooCommerce -