<?php
function remove_taxonomy_base($permalink, $term, $taxonomy) {
$public_taxonomies = get_taxonomies(array('public' => true), 'names');
if (in_array($taxonomy, $public_taxonomies)) {
if ($taxonomy === 'category') {
$category_base = get_option('category_base');
$category_base = $category_base ? $category_base : 'category';
$permalink = str_replace('/' . $category_base . '/', '/', $permalink);
} else {
$permalink = home_url('/' . $term->slug . '/');
}
}
return $permalink;
}
add_filter('term_link', 'remove_taxonomy_base', 10, 3);
function advanced_taxonomy_rewrite_rules() {
$public_taxonomies = get_taxonomies(array('public' => true), 'names');
foreach ($public_taxonomies as $taxonomy) {
$terms = get_terms(array(
'taxonomy' => $taxonomy,
'hide_empty' => false,
));
if (!empty($terms) && !is_wp_error($terms)) {
foreach ($terms as $term) {
if (is_reserved_term_slug($term->slug)) {
continue;
}
if ($taxonomy === 'category') {
add_rewrite_rule(
'^' . $term->slug . '/?$',
'index.php?category_name=' . $term->slug,
'top'
);
add_rewrite_rule(
'^' . $term->slug . '/page/([0-9]{1,})/?$',
'index.php?category_name=' . $term->slug . '&paged=$matches[1]',
'top'
);
if ($term->parent > 0) {
$ancestors = get_term_parents_list($term->term_id, 'category', array(
'format' => 'slug',
'separator' => '/',
'link' => false,
'inclusive' => false
));
if (!empty($ancestors)) {
add_rewrite_rule(
'^' . trim($ancestors, '/') . '/' . $term->slug . '/?$',
'index.php?category_name=' . $term->slug,
'top'
);
add_rewrite_rule(
'^' . trim($ancestors, '/') . '/' . $term->slug . '/page/([0-9]{1,})/?$',
'index.php?category_name=' . $term->slug . '&paged=$matches[1]',
'top'
);
}
}
} else {
add_rewrite_rule(
'^' . $term->slug . '/?$',
'index.php?' . $taxonomy . '=' . $term->slug,
'top'
);
add_rewrite_rule(
'^' . $term->slug . '/page/([0-9]{1,})/?$',
'index.php?' . $taxonomy . '=' . $term->slug . '&paged=$matches[1]',
'top'
);
if (is_taxonomy_hierarchical($taxonomy) && $term->parent > 0) {
$ancestors = get_term_parents_list($term->term_id, $taxonomy, array(
'format' => 'slug',
'separator' => '/',
'link' => false,
'inclusive' => false
));
if (!empty($ancestors)) {
add_rewrite_rule(
'^' . trim($ancestors, '/') . '/' . $term->slug . '/?$',
'index.php?' . $taxonomy . '=' . $term->slug,
'top'
);
add_rewrite_rule(
'^' . trim($ancestors, '/') . '/' . $term->slug . '/page/([0-9]{1,})/?$',
'index.php?' . $taxonomy . '=' . $term->slug . '&paged=$matches[1]',
'top'
);
}
}
}
}
}
}
}
add_action('init', 'advanced_taxonomy_rewrite_rules', 10);
function is_reserved_term_slug($slug) {
$reserved_slugs = array(
'attachment', 'attachment_id', 'author', 'author_name', 'calendar',
'cat', 'category', 'category_name', 'cpage', 'day', 'debug', 'error',
'exact', 'feed', 'hour', 'link_category', 'm', 'minute', 'monthnum',
'more', 'name', 'nav_menu_item', 'nopaging', 'offset', 'order', 'orderby',
'p', 'page', 'page_id', 'paged', 'pagename', 'pb', 'post', 'post_type',
'posts', 'preview', 'robots', 's', 'search', 'second', 'sentence',
'tag', 'tag_id', 'taxonomy', 'tb', 'term', 'terms', 'theme', 'title',
'type', 'w', 'year', 'comments_popup', 'admin', 'login', 'wp-admin',
'wp-content', 'wp-includes', 'wp-json', 'comments', 'shop'
);
$post_types = get_post_types(array('public' => true), 'names');
$all_reserved = array_merge($reserved_slugs, $post_types);
return in_array($slug, $all_reserved);
}
function flush_taxonomy_rules() {
advanced_taxonomy_rewrite_rules();
flush_rewrite_rules();
}
function maybe_flush_taxonomy_rules() {
if (get_option('taxonomy_base_removal_flush_needed', 'yes') === 'yes') {
flush_taxonomy_rules();
update_option('taxonomy_base_removal_flush_needed', 'no');
}
}
add_action('admin_init', 'maybe_flush_taxonomy_rules');
function set_taxonomy_flush_needed() {
update_option('taxonomy_base_removal_flush_needed', 'yes');
}
add_action('created_term', 'set_taxonomy_flush_needed');
add_action('edited_term', 'set_taxonomy_flush_needed');
add_action('delete_term', 'set_taxonomy_flush_needed');
add_action('permalink_structure_changed', 'set_taxonomy_flush_needed');