Created
May 18, 2012 16:06
-
-
Save bradt/2726069 to your computer and use it in GitHub Desktop.
WordPress Routes
This file contains hidden or 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 | |
function bt_register_routes( $routes ) { | |
foreach ( $routes as $route_name => $args ) { | |
bt_register_route( $route_name, $args ); | |
} | |
} | |
function bt_register_route( $route_name, $args = array() ) { | |
$args = wp_parse_args( $args, array( | |
'query_var' => 'route_' . $route_name, | |
'rewrite_rules' => array(), | |
'template_path' => str_replace( '_', '-', $route_name ) . '.php', | |
'title' => ucwords( str_replace( '_', ' ', $route_name ) ), | |
'body_class' => '', | |
'parse_query_func' => false, | |
) ); | |
BT_Routes::$routes[$route_name] = $args; | |
} | |
class BT_Routes { | |
public static $routes = array(); | |
function init() { | |
add_filter( 'generate_rewrite_rules', array( __CLASS__, 'add_rewrite_rules' ) ); | |
add_filter( 'query_vars', array( __CLASS__, 'query_vars' ) ); | |
add_filter( 'parse_query', array( __CLASS__, 'parse_query' ), 10, 2); | |
add_filter( 'template_include', array( __CLASS__, 'template_include' ) ); | |
add_filter( 'body_class', array( __CLASS__, 'body_classes' ) ); | |
add_filter( 'wp_title', array( __CLASS__, 'wp_title' ), null, 3 ); | |
add_action( 'posts_selection', array( __CLASS__, 'context_fixer' ) ); | |
} | |
function current() { | |
global $wp_query; | |
foreach ( self::$routes as $args ) { | |
if ( $wp_query->get( $args['query_var'] ) ) return $args; | |
} | |
return false; | |
} | |
function parse_query( $query ) { | |
if ( !$current = self::current() ) return $query; | |
if ( is_callable( $current['parse_query_func'] ) ) { | |
call_user_func( $current['parse_query_func'] ); | |
} | |
} | |
function wp_title( $title, $sep, $seplocation ) { | |
if ( !$current = self::current() ) return $title; | |
return $current['title'] . ' ' . $title; | |
} | |
function context_fixer() { | |
if ( !$current = self::current() ) return; | |
global $wp_query; | |
$wp_query->is_home = false; | |
} | |
function template_include( $template ) { | |
if ( !$current = self::current() ) return $template; | |
return locate_template( array( | |
$current['template_path'] . '.php', | |
'index.php' | |
)); | |
} | |
function body_classes( $c ) { | |
if ( !$current = self::current() || !$current['body_class'] ) return $c; | |
$c[] = $current['body_class']; | |
return $c; | |
} | |
function query_vars( $query_vars ) { | |
foreach ( self::$routes as $args ) { | |
$query_vars[] = $args['query_var']; | |
} | |
return $query_vars; | |
} | |
function add_rewrite_rules( $wp_rewrite ) { | |
$new_rules = array(); | |
foreach ( self::$routes as $args ) { | |
foreach ( $args['rewrite_rules'] as $pattern => $match_vars ) { | |
if ( is_int( $pattern ) ) { | |
$pattern = $match_vars; | |
$match_vars = ''; | |
} | |
if ( $match_vars ) $match_vars = '&' . $match_vars; | |
$new_rules[$pattern . '/?$'] = 'index.php?' . $args['query_var'] . '=1' . $match_vars; | |
} | |
} | |
//print_r($new_rules); | |
$wp_rewrite->rules = array_merge( $new_rules, $wp_rewrite->rules ); | |
//print_r($wp_rewrite->rules); | |
return $wp_rewrite; | |
} | |
} | |
BT_Routes::init(); | |
// Example - http://somedomain.com/edit-profile/ | |
bt_register_route( 'edit_profile', array( | |
'rewrite_rules' => array( 'edit-profile' ), | |
'template_path' => 'edit-profile' | |
) ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment