add_rewrite_rule

Adds a rewrite rule that transforms a URL structure to a set of query vars.

Description

add_rewrite_rule( (string) $regex, (string|array) $query, (string) $after = 'bottom' ); 

Any value in the $after parameter that isn't bottom will result in the rule being placed at the top of the rewrite rules.

Parameters (3)

0. $regex (string)
Regular expression to match request against.
1. $query (string|array)
The corresponding query vars for this rewrite rule.
2. $after — Optional. (string) => 'bottom'
Priority of the new rule. Accepts top or bottom.. Default bottom..

Usage

  1. if ( !function_exists( 'add_rewrite_rule' ) ) { 
  2. require_once ABSPATH . WPINC . '/rewrite.php'; 
  3.  
  4. // Regular expression to match request against. 
  5. $regex = ''; 
  6.  
  7. // The corresponding query vars for this rewrite rule. 
  8. $query = null; 
  9.  
  10. // Optional. Priority of the new rule. Accepts 'top' 
  11. // or 'bottom'. Default 'bottom'. 
  12. $after = 'bottom'; 
  13.  
  14. // NOTICE! Understand what this does before running. 
  15. $result = add_rewrite_rule($regex, $query, $after); 
  16.  

Defined (1)

The function is defined in the following location(s).

/wp-includes/rewrite.php  
  1. function add_rewrite_rule( $regex, $query, $after = 'bottom' ) { 
  2. global $wp_rewrite; 
  3.  
  4. $wp_rewrite->add_rule( $regex, $query, $after );