This page redirects to an external site: https://developer.wordpress.org/reference/classes/wp_rewrite/
This document assumes familiarity with Apache's mod_rewrite. If you've never heard of this before, try reading Sitepoint's Beginner's Guide to URL Rewriting. Also see Otto's explanation of hierarchy of rewrite rules in the wp-hackers email list.
WP_Rewrite is WordPress' class for managing the rewrite rules that allow you to use Pretty Permalinks feature. It has several methods that generate the rewrite rules from values in the database. It is used internally when updating the rewrite rules, and also to find the URL of a specific post, Page, category archive, etc.. It's defined in wp-includes/rewrite.php as a single instance global variable, $wp_rewrite, is initialised in wp-settings.php.
This is the formal documentation of WP_Rewrite. Try not to access or set the properties directly, instead use the methods to interact with the $wp_rewrite object. See also Rewrite_API.
'Pattern' => 'Substitution' (see below).As the rewrite rules are a crucial part of your website's functionality, WordPress allows plugins to hook into the generation process at several points. rewrite_rules(), specifically, contains nine filters and one hook for really precise control over the rewrite rules process. Here's what you can filter in rewrite_rules():
mod_rewrite_rules() is the function that takes the array generated by rewrite_rules() and actually turns it into a set of rewrite rules for the .htaccess file. This function also has a filter, mod_rewrite_rules, which will pass functions the string of all the rules to be written out to .htaccess, including the <IfModule> surrounding section. (Note: you may also see plugins using the rewrite_rules hook, but this is deprecated).
(See also: Permalinks for Custom Archives) The most obvious thing a plugin would do with the $wp_rewrite object is add its own rewrite rules. This is remarkably simple. Filter the generic rewrite_rules_array.
A Quick and dirty example for rewriting http://mysite/project/1 into http://mysite/index.php?pagename=project&id=1:
add_filter( 'rewrite_rules_array','my_insert_rewrite_rules' );
add_filter( 'query_vars','my_insert_query_vars' );
add_action( 'wp_loaded','my_flush_rules' );
// flush_rules() if our rules are not yet included
function my_flush_rules(){
$rules = get_option( 'rewrite_rules' );
if ( ! isset( $rules['(project)/(\d*)$'] ) ) {
global $wp_rewrite;
$wp_rewrite->flush_rules();
}
}
// Adding a new rule
function my_insert_rewrite_rules( $rules )
{
$newrules = array();
$newrules['(project)/(\d*)$'] = 'index.php?pagename=$matches[1]&id=$matches[2]';
return $newrules + $rules;
}
// Adding the id var so that WP recognizes it
function my_insert_query_vars( $vars )
{
array_push($vars, 'id');
return $vars;
}
Keep in mind that the flush_rules function is quite slow, so in practice you never want to call it from the wp_loaded action that gets executed on each page load. Instead, call this function only when the rewrite rules change. If the rules get set up and then never change, it is enough to flush in register_activation_hook of your plugin.
The Jerome's Keywords plugin does this to enable URLs like http://example.com/tag/sausages.
function keywords_create_rewrite_rules( $rewrite ) {
global $wp_rewrite;
// add rewrite tokens
$keytag_token = '%tag%';
$wp_rewrite->add_rewrite_tag( $keytag_token, '(.+)', 'tag=' );
$keywords_structure = $wp_rewrite->root . "tag/$keytag_token";
$keywords_rewrite = $wp_rewrite->generate_rewrite_rules( $keywords_structure );
return ( $rewrite + $keywords_rewrite );
}
Instead of inserting the rewrite rules into the $rewrite array itself, Jerome chose to create a second array, $keywords_rewrite, using the WP_Rewrite function generate_rewrite_rules(). Using that function means that the plugin doesn't have to create rewrite rules for extra pages (like page/2), or feeds (like feed/atom), etc. This array is then appended onto the $rewrite array and returned.
A simpler example of this is Ryan Boren's Feed Director plugin. This simply redirects URLs like http://example.com/feed.xml to http://example.com/feed/rss2:
function feed_dir_rewrite( $wp_rewrite ) {
$feed_rules = array(
'index.rdf' => 'index.php?feed=rdf',
'index.xml' => 'index.php?feed=rss2',
'(.+).xml' => 'index.php?feed=' . $wp_rewrite->preg_index(1)
);
$wp_rewrite->rules = $feed_rules + $wp_rewrite->rules;
return $wp_rewrite->rules;
}
// Hook in.
add_filter( 'generate_rewrite_rules', 'feed_dir_rewrite' );
As the array is so simple here, there is no need to call generate_rewrite_rules(). Again, the plugin's rules are added to WordPress'. Notice that as this function filters generate_rewrite_rules, it accepts a reference to the entire $wp_rewrite object as a parameter, not just the rewrite rules.
Of course, as you're adding your rewrite rules to the array before WordPress does anything with them, your plugins rewrite rules will be included in anything WordPress does with the rewrite rules, like write them to the .htaccess file.
<code style="color: #000000"> <span style="color: #0000BB"><?php
$wp_rewrite</span><span style="color: #007700">-></span><span style="color: #0000BB">non_wp_rules </span><span style="color: #007700">= array( </span><span style="color: #DD0000">'Pattern' </span><span style="color: #007700">=> </span><span style="color: #DD0000">'Substitution' </span><span style="color: #007700">);
</span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">$wp_rewrite</span><span style="color: #007700">-></span><span style="color: #0000BB">mod_rewrite_rules</span><span style="color: #007700">());
</span><span style="color: #0000BB">?></span> </code>
prints
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /wp_home/
RewriteRule ^Pattern /wp_home/Substitution [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /wp_home/index.php [L]
</IfModule>
where /wp_home/ is WordPress' home directory (or the root URL / if WordPress is installed in your web root.)
WP_Rewrite() is located in wp-includes/class-wp-rewrite.php