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:
https://sillasde.com/product-category/oficina/
into:
https://sillasde.com/oficina/
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:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
Plugin Name: Remove product-category slug | |
Plugin URI: https://timersys.com/ | |
Description: Check if url slug matches a woocommerce product category and use it instead | |
Version: 0.1 | |
Author: Timersys | |
License: GPLv2 or later | |
*/ | |
add_filter('request', function( $vars ) { | |
global $wpdb; | |
if( ! empty( $vars['pagename'] ) || ! empty( $vars['category_name'] ) || ! empty( $vars['name'] ) || ! empty( $vars['attachment'] ) ) { | |
$slug = ! empty( $vars['pagename'] ) ? $vars['pagename'] : ( ! empty( $vars['name'] ) ? $vars['name'] : ( !empty( $vars['category_name'] ) ? $vars['category_name'] : $vars['attachment'] ) ); | |
$exists = $wpdb->get_var( $wpdb->prepare( "SELECT t.term_id FROM $wpdb->terms t LEFT JOIN $wpdb->term_taxonomy tt ON tt.term_id = t.term_id WHERE tt.taxonomy = 'product_cat' AND t.slug = %s" ,array( $slug ))); | |
if( $exists ){ | |
$old_vars = $vars; | |
$vars = array('product_cat' => $slug ); | |
if ( !empty( $old_vars['paged'] ) || !empty( $old_vars['page'] ) ) | |
$vars['paged'] = ! empty( $old_vars['paged'] ) ? $old_vars['paged'] : $old_vars['page']; | |
if ( !empty( $old_vars['orderby'] ) ) | |
$vars['orderby'] = $old_vars['orderby']; | |
if ( !empty( $old_vars['order'] ) ) | |
$vars['order'] = $old_vars['order']; | |
} | |
} | |
return $vars; | |
}); |
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.
UPDATE:
I will share some snippets that users send me by email regarding this post. I haven’t tested them so use carefully.
add_filter('term_link', 'term_link_filter', 10, 3); function term_link_filter( $url, $term, $taxonomy ) { $url=str_replace("/./","/",$url); return $url; }
venera says
hi , I cant install this plugin
wpadmin says
Be sure to have an updated version of PHP in your site.
venera says
php 5.3 and How will I install this file? zip?
Peter says
Hello,
What’s happen with old URL product? Because, google index all my product, and after this change, show 404 error. How to redirect old URL with new? 🙂
wpadmin says
The code is more for a new shop starting. You will have to create redirects fromd old to new urls