From time to time I share WooCommerce tips like for example How to do facebook login on WooCommerce post published a while ago. Today I’m sharing a tiny code I’m using for a SEO experiment I’m conducting that involves removing the product category slug from the url.
Basically I needed in order to rank for Sillas de oficina ( Office chairs ) to convert this:
Of course the first thing I did as always that I need something is to google for it. I tried a couple of snippets but none of them worked and also found a paid plugin claiming to be the only solution that was costing more than 100USD which I thought it was a bit crazy for a single couple of words removed from the url.
So I decided to build something myself and after a couple of tests I got something working that I would like to share with you
How to remove product-category slug in WooCommerce?
I’m still not sure if this is the right or perfect solution as I’m still testing it. It works for me and it could work for you, but if you find any flaw , please let me know in the comments.
In order to make this work you need to follow some steps and rules. The code is experimental and is better to avoid using it on production sites. That being said this is what you need to remove the slug from category pages.
- Enter a “.” in the product category base field of the permalinks page. This will help to generate the urls around the site like we want
- Be sure that you don’t have any page, post or attachment with the same name (slug) as the category page or they will collide and the code won’t work.
- Install and activate the plugin below:
How this works?
Basically we filter every request and check if some common vars that are set on a regular category url are filled. If they are, we perform a single sql query to see if we have any product category with that name and if we do have we simply set the vars of the request as if we were looking to a category page.
Everything happens before the main sql queries are executed so this shouldn’t be much resource waster. It works with page cache plugins and I guess that if you use persistent cache the code could be modified to benefit of it.
Feel free to test the code and let me know if it’s not working on any particular case. I only tried with two levels of category and pagination, nothing else.