Skip to content

Instantly share code, notes, and snippets.

@micahwave
Created October 21, 2012 21:30
Show Gist options
  • Save micahwave/3928578 to your computer and use it in GitHub Desktop.
Save micahwave/3928578 to your computer and use it in GitHub Desktop.
rewrite rules
/**
* Custom endpoints, rewrite rules for contributors page tabs
*/
function time_rewrite_rules() {
global $wp_rewrite;
add_rewrite_tag( '%photo%', '([^&]+)' );
add_rewrite_tag( '%slide%', '([^&]+)' );
add_rewrite_tag( '%tab%', '([^&]+)' );
add_rewrite_tag( '%print%', '([^&]+)' );
// endpoints for galleries and packages
add_rewrite_endpoint( 'photo', EP_PERMALINK );
add_rewrite_endpoint( 'slide', EP_PERMALINK );
// contributors
add_rewrite_rule( '^contributors/?$', 'index.php?post_type=time_guest', 'top' );
add_rewrite_rule( '^contributors/(.+?)/page/([0-9]{1,})/?$', 'index.php?post_type=time_guest&tab=$matches[1]&page=$matches[2]', 'top' );
add_rewrite_rule( '^contributors/([^/]*)/?', 'index.php?post_type=time_guest&tab=$matches[1]', 'top' );
// components
add_rewrite_rule( '^component/header', 'index.php?component_type=header', 'top' );
add_rewrite_rule( '^component/footer', 'index.php?component_type=footer', 'top' );
// newsletters
add_rewrite_rule( 'newsletter/?$', 'index.php?newsletter=newsletter', 'top' );
add_rewrite_rule( 'newsletter-text/?$', 'index.php?newsletter=newsletter-text', 'top' );
add_rewrite_rule( 'newsletter/cartoons/?$', 'index.php?newsletter=cartoons', 'top' );
add_rewrite_rule( 'newsletter/cartoons-text/?$', 'index.php?newsletter=cartoons-text', 'top' );
$format_rewrite_map = array(
'photos' => 'gallery',
'recaps' => 'recap',
'lists' => 'special',
'quotes' => 'quote',
'videos' => 'video',
'reviews' => 'review'
);
foreach( $format_rewrite_map as $path => $format ) {
add_rewrite_rule( '^'.$path.'/page/?([0-9]{1,})/?$', 'index.php?taxonomy=time_post_format&term=time-post-format-'.$format.'&paged=$matches[1]', 'top' );
add_rewrite_rule( '^'.$path.'/?$', 'index.php?taxonomy=time_post_format&term=time-post-format-'.$format, 'top' );
}
// print
add_rewrite_rule( '([0-9]{4})/([0-9]{1,2})/([0-9]{1,2})/([^/]+)/print(/(.*))?/?$', 'index.php?year=$matches[1]&monthnum=$matches[2]&day=$matches[3]&name=$matches[4]&print=1', 'top' );
add_rewrite_rule( '([0-9]{4})/([0-9]{1,2})/([0-9]{1,2})/([^/]+)/slide/([^/]+)/print/?$', 'index.php?year=$matches[1]&monthnum=$matches[2]&day=$matches[3]&name=$matches[4]&slide=$matches[5]&print=1', 'top' );
add_rewrite_rule( '([0-9]{4})/([0-9]{1,2})/([0-9]{1,2})/([^/]+)/photo/([^/]+)/print/?$', 'index.php?year=$matches[1]&monthnum=$matches[2]&day=$matches[3]&name=$matches[4]&photo=$matches[5]&print=1', 'top' );
// dont flush the rules in production
if( defined( 'TIME_DEV' ) ) {
$wp_rewrite->flush_rules();
}
}
add_action( 'init', 'time_rewrite_rules', 999 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment