Rewrite rules for URL prefixes

I am rewriting some URL prefixes (i.e. category prefix, tag prefix or custom taxonomy prefix) in my WordPress plugin. I am using WordPress’ rewrite API:

add_filter('rewrite_rules_array','wp_insertMyRewriteRules');
add_filter('init','flushRules');  

// Remember to flush_rules() when adding rules
function flushRules(){
    global $wp_rewrite;
    $wp_rewrite->flush_rules();
}

// Adding a new rule
function wp_insertMyRewriteRules($rules)
{
    $newrules = array();
    $newrules['kategorie/(.+?)/?$'] = 'index.php?category_name=$matches[1]';
    return $newrules + $rules;
}

It is working, but the system URLs on site have not changed, and WP is using the old prefix ‘category’ from the database. How do I rewrite those URLs as well?

Related posts

Leave a Reply

1 comment

  1. If you want to change the permalink structure for all categories, then you can configure that at Settings -> Permalinks -> Category base.

    If you want to change the link for only one or a few categories, you can use the WordPress category_link filter:

    function update_permalink( $permalink, $cat_id ) {
        // If $cat_id is category we want to change, modify $permalink
        return $permalink;
    }
    add_filter( 'category_link', 'update_permalink', 11, 2 );