Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Enabled Root-based Category URLs in WordPress (Any bugs? Post comments below.)
<?php
/*
* Plugin Name: Root-based Category URLs
* Description: Enables root-based Category URLs, i.e. Makes /category/my-category/ URLs route as /my-category/
* Author: Mike Schinkel
* Author URI: http://about.me/mikeschinkel
* Plugin URI: https://gist.github.com/1421235
* Version: 0.1.1
* License: GPL 2+
*/
class Rootbased_Category_URLs {
static function on_load() {
add_filter( 'request', array( __CLASS__, 'request' ) );
add_filter( 'category_link', array( __CLASS__, 'category_link' ) );
}
function request( $query_vars ) {
global $wp;
if ( preg_match( self::get_category_base() . '(.*)', $wp->request, $match ) ) {
wp_safe_redirect( self::category_link( $wp->request ), 301 );
exit;
}
$category_hash = array_flip( self::get_category_slugs() );
if ( isset( $category_hash[$wp->request] ) ) {
$query_vars = array( 'category_name' => $wp->request );
}
return $query_vars;
}
function get_category_base() {
$category_base = get_option('category_base');
if ( empty( $category_base ) )
$category_base = 'category';
return "/{$category_base}/";
}
function category_link( $category_link ) {
return str_replace( self::get_category_base(), '/', $category_link );
}
function get_category_slugs() {
$categories = get_categories();
foreach( $categories as $index => $category ) {
$categories[$index] = $category->slug;
}
return $categories;
}
}
Rootbased_Category_URLs::on_load();
@Ciantic

This comment has been minimized.

Show comment Hide comment
@Ciantic

Ciantic Dec 12, 2011

Great stuff, hope it's public domain. Going to create more generalized version for custom taxonomy with add_filter('term_link', ...), get_terms()

Ciantic commented Dec 12, 2011

Great stuff, hope it's public domain. Going to create more generalized version for custom taxonomy with add_filter('term_link', ...), get_terms()

@mikeschinkel

This comment has been minimized.

Show comment Hide comment
@mikeschinkel

mikeschinkel Dec 12, 2011

I just added "License: GPL 2+".

If you update it and want to share back with me I'll review it and update this?

Owner

mikeschinkel commented Dec 12, 2011

I just added "License: GPL 2+".

If you update it and want to share back with me I'll review it and update this?

@AverageJoe

This comment has been minimized.

Show comment Hide comment
@AverageJoe

AverageJoe May 17, 2012

Mike, when I used your code I get this error.

Warning: preg_match() [function.preg-match]: Unknown modifier '(' in ... \wp-content\plugins\RemoveCategory\RemoveCategory.php on line 19

Mike, when I used your code I get this error.

Warning: preg_match() [function.preg-match]: Unknown modifier '(' in ... \wp-content\plugins\RemoveCategory\RemoveCategory.php on line 19

@mikeschinkel

This comment has been minimized.

Show comment Hide comment
@mikeschinkel

mikeschinkel May 18, 2012

@AverageJoe: What do you have your category base set to?

Screenshot showing Category Base option

Owner

mikeschinkel commented May 18, 2012

@AverageJoe: What do you have your category base set to?

Screenshot showing Category Base option

@mikeschinkel

This comment has been minimized.

Show comment Hide comment
@mikeschinkel

mikeschinkel May 18, 2012

@AverageJoe: Alternately, do you have any other plugins that modify your URLs?

Owner

mikeschinkel commented May 18, 2012

@AverageJoe: Alternately, do you have any other plugins that modify your URLs?

@Ciantic

This comment has been minimized.

Show comment Hide comment
@Ciantic

Ciantic May 18, 2012

@AverageJoe: Try setting rewriting on. By clicking on that image "Month and name" for example. Basically the "default" does not allow anykind of rewriting, you must set anything else than that.

Ciantic commented May 18, 2012

@AverageJoe: Try setting rewriting on. By clicking on that image "Month and name" for example. Basically the "default" does not allow anykind of rewriting, you must set anything else than that.

@jerzyk

This comment has been minimized.

Show comment Hide comment
@jerzyk

jerzyk May 9, 2015

to avoid warnings such as:

preg_match(): Unknown modifier '(' 

change line 19 (add regexp delimiters, to avoid parsing "/" from get_category slugs as delimiters), to:

if ( preg_match( '#' . self::get_category_base() . '(.*)#', $wp->request, $match ) ) {

jerzyk commented May 9, 2015

to avoid warnings such as:

preg_match(): Unknown modifier '(' 

change line 19 (add regexp delimiters, to avoid parsing "/" from get_category slugs as delimiters), to:

if ( preg_match( '#' . self::get_category_base() . '(.*)#', $wp->request, $match ) ) {
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment