Skip to content

Instantly share code, notes, and snippets.

@ryanmeisters
Last active December 1, 2016 07:15
Show Gist options
  • Save ryanmeisters/8b9bba42dda43ea9c8543c15479cd1d6 to your computer and use it in GitHub Desktop.
Save ryanmeisters/8b9bba42dda43ea9c8543c15479cd1d6 to your computer and use it in GitHub Desktop.
<?php
/**
* Rewrite Shop Categories
*
* @wordpress-plugin
* Plugin Name: Rewrite Shop Categories
* Description: Allows categories and products both to have 'shop' base
* Author: Plant Ninja
*/
if ( ! defined( 'WPINC' ) ) { die; }
// Flush rewrite rules on activation and deactivation.
register_deactivation_hook( __FILE__, 'flush_rewrite_rules' );
register_activation_hook( __FILE__, 'flush_rewrite_rules' );
// Priority 20 so filter runs _after_ woocommerce filter.
add_filter( 'rewrite_rules_array', 'rsc_rewrite_shop_categories' , 20 );
/**
* Simple function to filter wordpress rewrite rules array.
*
* @param array $rules Rules array passed by wordpress.
*/
function rsc_rewrite_shop_categories( $rules ) {
return rsc_get_product_category_rewrite_rules() + $rules;
}
function rsc_get_product_category_rewrite_rules() : array {
/*
Rewrite rules to match Plant Ninja categories and sub-categories.
The Plant Ninja product url structure is as follows:
- We want Urls to look like this:
shop/<base-category>/<optional-sub-category>/<the-product>
- Sharing the 'shop' base between categories and products is not supported
by WP/woocommerce out of the box (presumably because wordpress would have
no way to distinguish between categories and products.
- We can get around this by defining a way to distinguish between the two.
For Plant Ninja, the deepest category for a product must start with
"vegan-". E.g. "vegan-protein", "vegan-creatine".
- We will support one or two levels of categories for now.
*/
$new_rules = [
/*
* Match arbitrarily named base categories. Products in these categories
* must have an additional sub-category with the 'vegan-' prefix, and the
* base category must not be prefixed with 'vegan-'.
*
* Matches:
* shop/some-arbitrary-base-category
* shop/some-arbitrary-base-category/
*
* Should NOT match:
* shop/vegan-<anything>
* shop/vegan-<anything>/
* shop/base-category/<anything...>
*/
'shop/(?!vegan-)([^/]+?)/?$' => 'index.php?product_cat=$matches[1]',
/*
* Matches:
* shop/some-base-category/vegan-sub-category
* shop/some-base-category/vegan-<sub-category>/
*
* Should NOT match:
* shop/arbitrary-base-category
* shop/arbitrary-base-category/
* which should be covered by the previous rule.
*/
'shop(?:/.*)*?/(vegan-[^/]+)/?$' => 'index.php?product_cat=$matches[1]',
];
return $new_rules;
}
@alpeshp
Copy link

alpeshp commented Dec 1, 2016

Hello Ryan

i have used your plugin but unable to activate it

my problem is i have set Product category base = shop and Shop base with category = /shop/%product_cat%/

but when i visit to my product or category it show me 404 error

please can you help me to solve this problem

settings

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment