Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Garconis/3c59307a6d8d7f71fe9fdd433c6298f8 to your computer and use it in GitHub Desktop.
Save Garconis/3c59307a6d8d7f71fe9fdd433c6298f8 to your computer and use it in GitHub Desktop.
WordPress | Rewrite taxonomy term link URL to remove taxonomy slug
<?php
// https://wordpress.stackexchange.com/questions/21076/remove-taxonomy-base-or-term-from-url
// Requires going to Settings > Permalink Settings, and resaving any time you make a change to this Snippet.
// Within the CPT UI settings for the Taxonomy, be sure Rewrite is set to True, but that there is no Custom Rewrite Slug used.
// These functions help forcibly remove the taxonomy's slug from the term's URL.
// You will need to keep an eye out for slug clashes (particularly with Pages or Posts), since this doesn't prevent those.
/**
* Help WordPress to force rewrites so it can know what the URL is that we want for each term
*/
function custom_term_rewrite_basic() {
// each Product Type taxonomy term
add_rewrite_rule('cut-stack/?$', 'index.php?product_types=cut-stack', 'top');
add_rewrite_rule('digital-printing/?$', 'index.php?product_types=digital-printing', 'top');
add_rewrite_rule('flexible-packaging/?$', 'index.php?product_types=flexible-packaging', 'top');
add_rewrite_rule('in-mold/?$', 'index.php?product_types=in-mold', 'top');
add_rewrite_rule('prepress/?$', 'index.php?product_types=prepress', 'top');
add_rewrite_rule('pressure-sensitive/?$', 'index.php?product_types=pressure-sensitive', 'top');
add_rewrite_rule('promotional-printing/?$', 'index.php?product_types=promotional-printing', 'top');
add_rewrite_rule('roll-fed/?$', 'index.php?product_types=roll-fed', 'top');
add_rewrite_rule('shrink-sleeve/?$', 'index.php?product_types=shrink-sleeve', 'top');
add_rewrite_rule('stretch-sleeve/?$', 'index.php?product_types=stretch-sleeve', 'top');
// each Market Type taxonomy term
add_rewrite_rule('beer/?$', 'index.php?market_types=beer', 'top');
add_rewrite_rule('beverages/?$', 'index.php?market_types=beverages', 'top');
add_rewrite_rule('food/?$', 'index.php?market_types=food', 'top');
add_rewrite_rule('household-products/?$', 'index.php?market_types=household-products', 'top');
add_rewrite_rule('nutraceutical/?$', 'index.php?market_types=nutraceutical', 'top');
add_rewrite_rule('paint-coatings/?$', 'index.php?market_types=paint-coatings', 'top');
add_rewrite_rule('personal-care/?$', 'index.php?market_types=personal-care', 'top');
add_rewrite_rule('private-label-retail/?$', 'index.php?market_types=private-label-retail', 'top');
add_rewrite_rule('spirits/?$', 'index.php?market_types=spirits', 'top');
}
add_action('init', 'custom_term_rewrite_basic');
/**
* Remove the slug from the Product Type taxonomy term permalinks.
*/
function fs_custom_product_types_link( $link, $term, $taxonomy ) {
// if it's not this Taxonomy, then return the regular link
if ( $taxonomy !== 'product_types' ) {
return $link;
}
// otherwise, remove the taxonomy slug from the term URL
return str_replace( 'product_types/', '', $link );
}
add_filter( 'term_link', 'fs_custom_product_types_link', 10, 3 );
/**
* Remove the slug from the Market Type taxonomy term permalinks.
*/
function fs_custom_market_types_link( $link, $term, $taxonomy ) {
// if it's not this Taxonomy, then return the regular link
if ( $taxonomy !== 'market_types' ) {
return $link;
}
// otherwise, remove the taxonomy slug from the term URL
return str_replace( 'market_types/', '', $link );
}
add_filter( 'term_link', 'fs_custom_market_types_link', 10, 3 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment