Fix Double Rewrite Tag in Extra Permatructs
Fix permastructure when you already use rewrite tag in rewrite slug.
by KLicheR · github.com/klicher/wp-fix-double-rewrite-tag-in-extra-permatructs · website
Install
No release zip yet. The repository archive installs, but the folder name will carry the branch suffix and updates will not flow:
wp plugin install https://github.com/klicher/wp-fix-double-rewrite-tag-in-extra-permatructs/archive/refs/heads/master.zipFix double rewrite tag in extra Permastructs
This plugin fix a really specific case when you use the rewrite tag of a custom taxonomy in the rewrite slug of this same taxonomy. WordPress will always add the rewrite tag at the end of the rewrite slug so this plugin is useful to detect that you already use the rewrite tag in the rewrite slug and reconstruct extra permastructs without the adding the rewrite tag at the end.
Usage
Add a filter to specified the taxonomies you want to fix.
<?php
add_filter('taxonomies_extra_structure_to_fix', function($taxonomies_to_fix) {
return array('color');
});
?>
Example
Custom taxonomy with the taxo rewrite tag in the rewrite slug
<?php
register_taxonomy('color',
array('product'),
array(
'label' => 'Colors',
'labels' => array(
'name' => 'Colors',
'singular_name' => 'Color',
'menu_name' => 'Colors',
),
'public' => true,
'rewrite' => array(
'slug' => 'my-product-color/%color%/products',
),
)
);
?>
This will create URLs like:
/my-product-color/blue/products/blue/my-product-color/red/products/red
The permastructs behind it will be: /my-product-color/%color%/products/%color%. Which is noooot cool.
With my plugin, you'll obtain these URLs:
/my-product-color/blue/products/my-product-color/red/products
The permastructs behind it will be: /my-product-color/%color%/products.
WordPress behaviour
If you're interested in seeing how WordPress do this, you can check the line with the method add_permastruct in the function register_taxonomy of the file /wp-includes/taxonomy.php.