Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save azizultex/2f8340c2c2c7ea18b6be57aadfc3ab76 to your computer and use it in GitHub Desktop.
Save azizultex/2f8340c2c2c7ea18b6be57aadfc3ab76 to your computer and use it in GitHub Desktop.
Change slug ( add_rewrite_rule ) of already registered taxonomy or custom post type
/***************************************************
USING add_rewrite_rule
****************************************************/
/* azizultex
1. https://www.ait-themes.club/knowledge-base/how-can-i-change-items-slug-and-slug-of-other-custom-post-types/
2. http://wordpress.stackexchange.com/questions/41988/redeclare-change-slug-of-a-plugins-custom-post-type
3. http://wordpress.stackexchange.com/questions/134322/rewrite-rule-for-custom-taxonomy
*/
/***************************************************
TAXONOMY
****************************************************/
function change_stores_taxonomies_slug($args, $taxonomy) {
if ( 'stores' === $taxonomy ) {
$args['rewrite']['slug'] = 'colleges';
}
return $args;
}
add_filter( 'register_taxonomy_args', 'change_stores_taxonomies_slug', 10, 2 );
add_action( 'admin_init', 'stores_url_rewrite' );
function stores_url_rewrite(){
add_rewrite_rule("^locations/([^/]*)$",'index.php?stores=$matches[1]','top');
flush_rewrite_rules();
}
/*
re-register the taxonomy overriding args
*/
function modify_stores_taxonomy() {
$stores_tax_args = get_taxonomy( 'stores' );
$stores_tax_args->show_admin_column = true;
$stores_tax_args->rewrite['slug'] = 'locations';
$stores_tax_args->rewrite['with_front'] = false;
register_taxonomy( 'stores', 'store_page', (array) $stores_tax_args );
}
add_action( 'init', 'modify_stores_taxonomy', 11 );
/***************************************************
CPT
CHANGE SLUGS OF TEAM POST TYPES
****************************************************/
function change_team_post_types_slug( $args, $post_type ) {
/* item post type slug */
if ( 'dt_team' === $post_type ) {
$args['rewrite']['slug'] = 'dt-team';
}
return $args;
}
add_filter( 'register_post_type_args', 'change_team_post_types_slug', 10, 2 );
@azizultex
Copy link
Author

azizultex commented Sep 25, 2016

When using Store Locator Plus plugin for a school website, I had to change their taxonomy slug 'stores' to 'locations'.

Changing URL Slug can be done in 2 ways based on situation:
Situation 1: Rewrite rule isn't defined when registering custom post type or taxonomy. For this situation we need to add our rewrite rule using register_taxonomy_args filter for taxonomy as I did above, as the plugin used below code to register taxonomy.

register_taxonomy( SLPLus::locationTaxonomy, SLPLus::locationPostType, array( 'hierarchical' => true, 'labels' => array( 'menu_name' => __( 'Categories', 'store-locator-le' ), 'name' => __( 'Store Categories', 'store-locator-le' ), ), 'capabilities' => array( 'manage_terms' => 'manage_slp_admin', 'edit_terms' => 'manage_slp_admin', 'delete_terms' => 'manage_slp_admin', 'assign_terms' => 'manage_slp_admin', ) ) );

after filtering rewrite rule, we simply need to add_rewrite_rule and flush rewrite rules as above.

Situation 2: Rewrite rule is already defined when registering custom post type or taxonomy. At this situation, we can modify the options table by providing your own custom rules based on the existing post type. Details here: http://wordpress.stackexchange.com/questions/41988/redeclare-change-slug-of-a-plugins-custom-post-type

BEST SOLUTION:
I think, the best solution to override / add new args to a registered taxonomy is to use register_taxonomy using existing args. This way we aren't going to use add_rewrite_rule so we are skipping the fear to affect other pages URL when changing / adding slug rewrite rules.
More: http://wordpress.stackexchange.com/questions/161788/how-to-modify-a-taxonomy-thats-already-registered

Hope, this will help someone.

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