Tribe__Rewrite::add( array|string $regex, array $args = array() )
The base method for creating a new Rewrite rule
Contents
Parameters
- $regex
-
(array|string) (Required) The regular expression to catch the URL
- $args
-
(array) (Optional) The arguments in which the regular expression "alias" to
Default value: array()
Return
Source
File: src/Tribe/Rewrite.php
public function add( $regex, $args = array() ) {
$regex = (array) $regex;
$default = array();
$args = array_filter( wp_parse_args( $args, $default ) );
$url = add_query_arg( $args, 'index.php' );
// Optional Trailing Slash
$regex[] = '?$';
// Glue the pieces with slashes
$regex = implode( '/', array_filter( $regex ) );
// Add the Bases to the regex
foreach ( $this->bases as $key => $value ) {
$regex = str_replace( array( '{{ ' . $key . ' }}', '{{' . $key . '}}' ), $value, $regex );
}
// Apply the Preg Indexes to the URL
preg_match_all( '/%([0-9])/', $url, $matches );
foreach ( end( $matches ) as $index ) {
$url = str_replace( '%' . $index, $this->rewrite->preg_index( $index ), $url );
}
// Add the rule
$this->rules[ $regex ] = $url;
return $this;
}