Created
October 31, 2012 15:21
-
-
Save JPry/3987624 to your computer and use it in GitHub Desktop.
WP Rewrite example
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 | |
class jpry_custom_rewrites { | |
/** | |
* Additional query vars that WordPress should recognize | |
* | |
* @var array | |
*/ | |
var $query_vars = array( 'date_hash', 'partner_hash', 'partner_id' ); | |
/** | |
* Rewrite rules that will be added to WordPress | |
* | |
* @var array | |
*/ | |
var $rewrites = array( | |
array( | |
'rule' => 'partner_check_in/([a-zA-Z0-9]{32})/([^/]+)/([a-zA-Z0-9]{32})/?$', | |
'rewrite' => 'index.php?name=partner_check_in&date_hash=$matches[1]&partner_id=$matches[2]&partner_hash=$matches[3]', | |
'position' => 'top', | |
), | |
array( | |
'rule' => 'partner_single_check_in/([^/]+)/([a-zA-Z0-9]{32})/?$', | |
'rewrite' => 'index.php?name=partner_single_check_in&partner_id=$matches[1]&partner_hash=$matches[2]', | |
'position' => 'top', | |
), | |
); | |
/** | |
* Constructor for the class. Sets up certain class variables, and adds actions and filters | |
*/ | |
public function __construct() { | |
add_action( 'init', array( $this, 'custom_rewrite_rules' ) ); | |
add_filter( 'query_vars', array( $this, 'filter_query_vars' ) ); | |
} | |
/** | |
* Adds our custom rewrites to the rewrite rules. | |
* | |
* @since 0.5 | |
*/ | |
public function custom_rewrite_rules() { | |
foreach ( $this->rewrites as $rewrite ) { | |
add_rewrite_rule( $rewrite['rule'], $rewrite['rewrite'], $rewrite['position'] ); | |
} | |
} | |
/** | |
* Adds additional variables to the query_vars array. | |
* | |
* @param array $query_vars Initial query_vars array | |
* @return array Modified string of query vars | |
*/ | |
public function filter_query_vars( $query_vars ) { | |
$new_query_vars = array_merge( $query_vars, $this->query_vars ); | |
return $new_query_vars; | |
} | |
} // End of "jpry_custom_rewrites" class | |
// Example of initializing the class: | |
$_jpry_custom_rewrites = new jpry_custom_rewrites(); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment