Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove Category Base from WordPress Category URL

I fished around the internet for a solution to this, tried a plugin or two to remove the /category/ from wordpress url's.

While some of these plugins are good, the category link still display's /category/.

Also I've tried putting in ./ in the category base options in permalinks settings.

Does anyone know how I could do like a php search and replace or something like that?

like image 880
daryl Avatar asked Apr 13 '11 22:04

daryl


People also ask

How do I remove a category from WordPress?

It's easy to do that. Simply open the functions. php file in your theme and add the following code at the end of the file: function prefix_category_title( $title ) { if ( is_category() ) { $title = single_cat_title( '', false ); } return $title; } add_filter( 'get_the_archive_title', 'prefix_category_title' );


2 Answers

A cleaner solution:

add_filter('user_trailingslashit', 'remcat_function');
function remcat_function($link) {
    return str_replace("/category/", "/", $link);
}
add_action('init', 'remcat_flush_rules');
function remcat_flush_rules() {
    global $wp_rewrite;
    $wp_rewrite->flush_rules();
}
add_filter('generate_rewrite_rules', 'remcat_rewrite');
function remcat_rewrite($wp_rewrite) {
    $new_rules = array('(.+)/page/(.+)/?' => 'index.php?category_name='.$wp_rewrite->preg_index(1).'&paged='.$wp_rewrite->preg_index(2));
    $wp_rewrite->rules = $new_rules + $wp_rewrite->rules;
}
like image 131
Geek Avatar answered Sep 18 '22 05:09

Geek


Using WordPress 3.9.1 (latest version as of this post) I simply added a single line to my theme's functions.php...

$wp_rewrite->add_permastruct('category_base', '%category%');

Then I opened Settings > Permalinks and hit Save. This appears to flush the permalink cache and makes it work.

like image 42
Simon East Avatar answered Sep 22 '22 05:09

Simon East