Created
March 26, 2012 04:29
-
-
Save mikeschinkel/2202932 to your computer and use it in GitHub Desktop.
Root-based Taxonomy URLs Plugin for WordPress
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
* Plugin Name: Root-based Taxonomy URLs for WordPress | |
* Description: Enables root-based Taxonomy URLs, i.e. Makes /taxonomy/my-taxonomy/ URLs route as /my-taxonomy/ | |
* Author: Mike Schinkel | |
* Author URI: http://about.me/mikeschinkel | |
* Plugin URI: https://gist.github.com/1421235 | |
* Version: 0.1 | |
* License: GPL 2+ | |
* Notes: Must use register_root_based_taxonomy_url( $taxonomy[, $priority] ) in an 'init' hook to specify root_based taxonomy. | |
*/ | |
class Rootbased_Taxonomy_URLs { | |
static $root_based_taxonomies = array(); | |
static function register( $taxonomy, $priority = 10 ) { | |
self::$root_based_taxonomies[$taxonomy] = $priority; | |
} | |
static function on_load() { | |
add_filter( 'request', array( __CLASS__, 'request' ) ); | |
add_filter( 'term_link', array( __CLASS__, 'term_link' ), 10, 3 ); | |
} | |
static function request( $query_vars ) { | |
global $wp; | |
if ( isset( $query_vars['error'] ) && '404' == $query_vars['error'] ) { | |
$taxonomy = self::get_term_taxonomy( $wp->request ); | |
if ( ! is_null( $taxonomy ) ) { | |
$query_vars = array( | |
$taxonomy => $wp->request, | |
'taxonomy' => $taxonomy, | |
); | |
} | |
} else if ( 1 == count( $query_vars ) ) { | |
$taxonomy = self::get_term_taxonomy( reset( $query_vars ) ); | |
if ( $taxonomy ) { | |
wp_safe_redirect( self::term_link( $wp->request, null, $taxonomy ), 301 ); | |
exit; | |
} | |
} | |
return $query_vars; | |
} | |
static function term_link( $term_link, $term, $taxonomy ) { | |
$taxonomy_object = get_taxonomy( $taxonomy ); | |
$taxonomy_slug = isset( $taxonomy_object->rewrite['slug'] ) ? $taxonomy_object->rewrite['slug'] : $taxonomy; | |
$term_link = preg_replace( "#^(http(s?)://[^/]+/)?{$taxonomy_slug}/#", '$1', $term_link ); | |
return preg_match( '#^http(s?)://#', $term_link ) ? $term_link : "/{$term_link}"; | |
} | |
static function get_term_taxonomy( $term ) { | |
global $wpdb; | |
asort( self::$root_based_taxonomies ); | |
$root_based_taxonomies = array_keys( self::$root_based_taxonomies ); | |
$where_taxonomies = implode( "','", $root_based_taxonomies ); | |
$orderby_taxonomies = implode( ',', $root_based_taxonomies ); | |
$sql =<<<SQL | |
SELECT | |
tt.taxonomy | |
FROM | |
{$wpdb->term_taxonomy} tt | |
INNER JOIN {$wpdb->terms} t ON tt.term_id=t.term_id | |
WHERE 1=1 | |
AND tt.taxonomy IN ('{$where_taxonomies}') | |
AND t.slug = '%s' | |
ORDER BY | |
FIND_IN_SET( tt.taxonomy, '{$orderby_taxonomies}' ) | |
SQL; | |
$taxonomy = $wpdb->get_var( $wpdb->prepare( $sql, $term ) ); | |
return $taxonomy; | |
} | |
} | |
Rootbased_Taxonomy_URLs::on_load(); | |
function register_root_based_taxonomy_url( $taxonomy, $priority = 10 ) { | |
Rootbased_Taxonomy_URLs::register( $taxonomy ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment