Skip to content

Instantly share code, notes, and snippets.

@wturnerharris
Created November 11, 2013 13:52
Show Gist options
  • Save wturnerharris/7413478 to your computer and use it in GitHub Desktop.
Save wturnerharris/7413478 to your computer and use it in GitHub Desktop.
This class is an example of how to add rewrite rules and the actions to take should the rewrite be matched.
<?php
/**
* WP_Theme_Rewrites Class for WordPress.
*
*/
if(realpath(__FILE__) === realpath($_SERVER["SCRIPT_FILENAME"]))
exit("Do not access this file directly.");
class WP_Theme_Rewrites {
var $rewrites = array(
'events/(upcoming-events)/?$' => 'index.php?post_type=event&the_scope=$matches[1]',
'events/(past-events)/?$' => 'index.php?post_type=event&the_scope=$matches[1]',
);
function __construct(){
add_action( 'wp_loaded', array(&$this, 'flush_rewrite_rules') );
add_filter( 'rewrite_rules_array', array(&$this, 'insert_rewrite_rules') );
add_filter( 'query_vars', array(&$this, 'add_query_vars') );
add_action( 'pre_get_posts', array(&$this, 'filter_event_posts') );
add_action( 'template_redirect', array(&$this, 'switch_template') );
}
/**
*
* Perform flush_rules() operation if our rules are not yet included.
*
* @return void
*/
function flush_rewrite_rules() {
$rules = get_option( 'rewrite_rules' );
$rewrites = $this->rewrites;
for( $i=0; $i<count($rewrites); $i++) {
$key = key( $rewrites );
if ( ! isset( $rules[$key] ) ) {
global $wp_rewrite;
$wp_rewrite->flush_rules();
break;
}
}
}
/**
*
* Override rewrite rules.
*
* @return array
*/
function insert_rewrite_rules( $rules ) {
$rewrites = $this->rewrites;
return $rewrites + $rules;
}
/**
*
* Add query variable to global in order to switch the template.
*
* @return array
*/
function add_query_vars( $query_vars ) {
$query_vars[] = 'the_scope';
return $query_vars;
}
/**
*
* Switch template if custom route detected
*
* @return void
*/
function switch_template() {
$scope = get_query_var('the_scope');
$template = locate_template("archive.php");
if ( !empty($scope) && $template) {
load_template($template);
die;
}
}
/**
*
* Filter event posts for custom query var
*
* @return void
*/
function filter_event_posts( $query ) {
if ( is_admin() ) return;
$scope = get_query_var('the_scope');
if ( $query->is_main_query() && !empty($scope)) {
$compare_operator = array_shift(explode("-",$scope)) == "upcoming" ? ">=" : "<=";
$args = array(
'posts_per_page' => -1,
'post_type' => 'event',
'meta_query' => array(
'key' => '_start_ts',
'value' => current_time('timestamp'),
'compare' => $compare_operator,
'type'=>'numeric'
),
'orderby' => 'meta_value_num',
'order' => 'ASC',
'meta_key' => '_start_ts',
'meta_value' => current_time('timestamp'),
'meta_value_num' => current_time('timestamp'),
'meta_compare' => $compare_operator,
);
foreach($args as $key => $val) {
$query->set($key, $val);
}
}
if ( $query->is_search() ) {
$query->set('post_type', array('news','video','paper','book','course','page','event'));
}
}
}
new WP_Theme_Rewrites();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment