How to Remove Product Category from URL in WooCommerce: A Comprehensive Guide
Introduction:
Having a clean and SEO-friendly URL structure is crucial for any online store. In WooCommerce, the default URL structure often includes the product category, resulting in URLs like `yourdomain.com/product-category/category-name/product-name/`. While this structure provides context, it can also make URLs unnecessarily long and less appealing to both users and search engines. This article will guide you through how to remove the `product-category` base from your WooCommerce URLs, improving your site’s SEO and user experience.
Main Part:
There are several methods to remove the `product-category` base from your WooCommerce URLs. We’ll cover the most common and effective ones:
1. Using the WooCommerce Settings
This is the simplest and often the recommended method for most users.
- Navigate to Permalinks: Go to your WordPress dashboard, then navigate to Settings > Permalinks.
- Product Category Base: Look for the “Product category base” setting. By default, it’s likely set to `product-category`.
- Empty the Field: Completely delete the text in this field, leaving it blank.
- Save Changes: Click the “Save Changes” button at the bottom of the page.
- After saving, you may need to flush your permalinks. Go back to Settings > Permalinks and simply click “Save Changes” again without making any alterations. This forces WordPress to regenerate your rewrite rules.
- This method might not work perfectly with all themes and plugins. If you encounter issues, consider the alternative methods below.
- Access your `functions.php` file: You can access this file through your theme editor (Appearance > Theme Editor) or via FTP. Always back up your theme before making changes.
- Add the following code:
Important Considerations:
2. Using Custom Code (Function)
For more control and compatibility, you can use a custom function in your theme’s `functions.php` file or a custom plugin.
add_filter( 'woocommerce_get_permalink', 'remove_product_category_slug', 10, 3 );
function remove_product_category_slug( $permalink, $post, $leavename ) {
if ( ‘product’ !== $post->post_type ) {
return $permalink;
}
// If permalink contains the product category, remove it
if ( strpos( $permalink, ‘/product-category/’ ) !== false ) {
$permalink = str_replace( ‘/product-category/’, ‘/’, $permalink );
}
return $permalink;
}
- Save the `functions.php` file.
- Flush your permalinks as mentioned in the previous method.
Explanation of the Code:
- `add_filter(‘woocommerce_get_permalink’, ‘remove_product_category_slug’, 10, 3);`: This line hooks into the WooCommerce permalink generation process.
- `remove_product_category_slug(…)`: This function checks if the post type is ‘product’. If it is, and the permalink contains `/product-category/`, it removes that part of the URL.
3. Using a Plugin
If you’re not comfortable editing code, several plugins can help you remove the product category base.
- Search for a suitable plugin: In your WordPress dashboard, go to Plugins > Add New and search for plugins like “Remove Category Slug” or “WooCommerce Permalink Manager”.
- Install and activate the plugin: Choose a plugin with good reviews and active support.
- Configure the plugin: Follow the plugin’s instructions to remove the `product-category` base from your URLs. Most plugins offer a simple interface for achieving this.
Pros and Cons of Each Method:
| Method | Pros | Cons |
| ——————————-
| WooCommerce Settings | Simplest and quickest. No coding required. | May not be compatible with all themes/plugins. Can sometimes lead to 404 errors if not flushed correctly. |
| Custom Code (functions.php) | More control and generally more reliable. Avoids relying on third-party plugins. | Requires basic PHP knowledge. Improper implementation can break your site. Requires careful backup and testing. |
| Using a Plugin | Easy to use and configure without coding. | Adds another plugin to your site, potentially impacting performance. Relies on the plugin developer for updates and compatibility. Potential security risks. |
Potential Issues and Solutions
- 404 Errors: After removing the `product-category` base, you might encounter 404 errors. This usually happens because the permalinks haven’t been properly flushed. Go to Settings > Permalinks and click “Save Changes” again.
- Conflicting Rewrite Rules: Other plugins or custom code might be interfering with your permalink structure. Deactivate plugins one by one to identify the Read more about How To Add Additional Information In Woocommerce Products culprit and adjust the settings or code accordingly.
- Category Archives: Removing the `product-category` base might affect your category archive pages. Ensure that your theme correctly handles category archive URLs.
Conclusion:
Removing the `product-category` base from your WooCommerce URLs is a worthwhile optimization that can improve your site’s SEO and user experience. Choose the method that best suits your technical skills and comfort level. Remember to always back up your website before making any changes and thoroughly test your site after implementing any of these solutions. By following these steps, you can achieve cleaner, more SEO-friendly URLs for your WooCommerce products.