Created
September 11, 2014 14:17
-
-
Save wpscholar/f99859232a45b133aedc to your computer and use it in GitHub Desktop.
Create an alias for a query var
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 | |
/** | |
* Class Query_Var_Alias | |
*/ | |
class Query_Var_Alias { | |
/** | |
* Query var being aliased | |
* | |
* @var string | |
*/ | |
protected $_query_var; | |
/** | |
* Aliased name for query var | |
* | |
* @var string | |
*/ | |
protected $_alias; | |
/** | |
* Setup logic for query var aliasing | |
* | |
* @param string $query_var | |
* @param string $alias | |
*/ | |
public function __construct( $query_var, $alias ) { | |
$this->_query_var = $query_var; | |
$this->_alias = $alias; | |
add_action( 'parse_request', array( $this, 'parse_request' ) ); | |
add_filter( 'query_vars', array( $this, 'query_vars' ) ); | |
} | |
/** | |
* Add our alias to the list of query vars | |
* | |
* @param string $query_vars | |
* @return array | |
*/ | |
public function query_vars( $query_vars ) { | |
$query_vars[] = $this->_alias; | |
return $query_vars; | |
} | |
/** | |
* Replace our aliased query var with the actual query var | |
* | |
* @param WP $wp | |
*/ | |
public function parse_request( $wp ) { | |
if ( isset( $wp->query_vars[$this->_alias] ) ) { | |
$wp->query_vars[$this->_query_var] = $wp->query_vars[$this->_alias]; | |
unset( $wp->query_vars[$this->_alias] ); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment