Search code examples
wordpressmod-rewritecategoriesslugpermalinks

Use the category name instead of category slug in permalinks


How to create a permalink structure which finish by:

/category-name/post-name/

Instead of:

/category-slug/post-name/

By default, Wordpress is offering %category% tag strucure as "A sanitized version of the category name (category slug field on New/Edit Category panel".

How can I use the category name instead of category slug in permalinks?


Solution

  • add_filter('rewrite_rules_array', 'category_name_rewrite_rule');
    function category_name_rewrite_rule($rules) {
        $new_rules = array();
        $categories = get_categories();
        foreach ($categories as $category) {
            $cat_name = preg_replace('#\s+#', '-', $category->name);
            $new_rules['/'.$category->slug}.'/'] = '/'.url_encode($cat_name).'/';
        }
        return $new_rules + $rules;
    }