Skip to content

Instantly share code, notes, and snippets.

@prionkor
Created June 17, 2015 10:41
Show Gist options
  • Save prionkor/ef2e6b24d8d5561b4ad7 to your computer and use it in GitHub Desktop.
Save prionkor/ef2e6b24d8d5561b4ad7 to your computer and use it in GitHub Desktop.
Rewrites.php
// rewrites
add_action( 'wp_loaded', 'kcs_internal_rewrites' );
function kcs_internal_rewrites(){
$tokens = kcs_get_tokens(); // array('foo', 'bar') etc
foreach($tokens as $token){
add_rewrite_rule("$token", "index.php?$token=1", 'top');
add_rewrite_rule("([^/]*)/$token", 'index.php?pagename=$matches[1]&'. $token .'=1', 'top');
add_rewrite_rule("([^/]*)/([^/]*)/$token", 'index.php?pagename=$matches[1]/$matches[2]&'. $token .'=1', 'top');
add_rewrite_rule("([^/]*)/([^/]*)/([^/]*)/$token", 'index.php?pagename=$matches[1]/$matches[2]/$matches[3]&'. $token .'=1', 'top');
}
}
add_filter( 'query_vars', 'kcs_internal_query_vars' );
function kcs_internal_query_vars( $query_vars ){
$tokens = kcs_get_tokens();
$query_vars = array_merge($query_vars, $tokens);
return $query_vars;
}
add_action( 'parse_request', 'kcs_internal_rewrites_parse_request' );
function kcs_internal_rewrites_parse_request( &$wp ){
$tokens = kcs_get_tokens();
foreach($tokens as $token){
if (!array_key_exists( $token, $wp->query_vars ) ) {
continue;
}
// process
return;
}
// other codes removed
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment