Class WP_Rewrite

Description

WordPress Rewrite Component.

The WordPress Rewrite class writes the rewrite module rules to the .htaccess file. It also handles parsing the request to get the correct setup for the WordPress Query class.

The Rewrite along with WP class function as a front controller for WordPress. You can add rules to trigger your page view and processing using this component. The full functionality of a front controller does not exist, meaning you can't define how the template files load based on the rewrite rules.

  • since: 1.5.0

Located in /wp-includes/rewrite.php (line 394)


	
			
Variable Summary
Method Summary
void add_endpoint (string $name, int $places)
void add_external_rule (string $regex, string $redirect)
void add_permastruct (string $name, string $struct, [array $args = array()])
void add_rewrite_tag (string $tag, string $regex, string $query)
void add_rule (string $regex, string $redirect, [string $after = 'bottom'])
void flush_rules ([bool $hard = true])
array generate_rewrite_rule (string $permalink_structure, [bool $walk_dirs = false])
array generate_rewrite_rules (string $permalink_structure, [int $ep_mask = EP_NONE], [bool $paged = true], [bool $feed = true], [bool $forcomments = false], [bool $walk_dirs = true], [bool $endpoints = true])
string|bool get_author_permastruct ()
bool|string get_category_permastruct ()
bool|string get_date_permastruct ()
bool|string get_day_permastruct ()
string|bool get_extra_permastruct (string $name)
string|bool get_feed_permastruct ()
bool|string get_month_permastruct ()
string|bool get_page_permastruct ()
string|bool get_search_permastruct ()
bool|string get_tag_permastruct ()
bool|string get_year_permastruct ()
string iis7_url_rewrite_rules ([ $add_parent_tags = false])
void init ()
string mod_rewrite_rules ()
array page_uri_index ()
string preg_index (int $number)
array rewrite_rules ()
void set_category_base (string $category_base)
void set_permalink_structure (string $permalink_structure)
void set_tag_base (string $tag_base)
array wp_rewrite_rules ()
Variables
string $index = 'index.php' (line 548)

The name of the index file which is the entry point to all requests.

  • since: 1.5.0
  • access: public
bool $use_verbose_page_rules = true (line 649)

Could post permalinks be confused with those of pages?

If the first rewrite tag in the post permalink structure is one that could also match a page name (e.g. %postname% or %author%) then this flag is set to true. Prior to WordPress 3.3 this flag indicated that every page would have a set of rules added to the top of the rewrite rules array. Now it tells WP::parse_request() to check if a URL matching the page permastruct is actually a page before accepting it.

bool $use_verbose_rules = false (line 631)

Whether to write every mod_rewrite rule for WordPress into the .htaccess file.

This is off by default, turning it on might print a lot of rewrite rules to the .htaccess file.

Methods
Constructor __construct (line 1996)

Constructor - Calls init(), which runs setup.

  • since: 1.5.0
  • access: public
WP_Rewrite __construct ()
add_endpoint (line 1812)

Add an endpoint, like /trackback/.

See add_rewrite_endpoint() for full documentation.

void add_endpoint (string $name, int $places)
  • string $name: Name of the endpoint.
  • int $places: Endpoint mask describing the places the endpoint should be added.
add_external_rule (line 1795)

Add a rule that doesn't redirect to index.php.

Can redirect to any place.

  • since: 2.1.0
  • access: public
void add_external_rule (string $regex, string $redirect)
  • string $regex: Regular expression to match against request.
  • string $redirect: URL regex redirects to when regex matches request.
add_permastruct (line 1846)

Add a new permalink structure.

A permalink structure (permastruct) is an abstract definition of a set of rewrite rules; it is an easy way of expressing a set of regular expressions that rewrite to a set of query strings. The new permastruct is added to the WP_Rewrite::$extra_permastructs array. When the rewrite rules are built by WP_Rewrite::rewrite_rules() all of these extra permastructs are passed to WP_Rewrite::generate_rewrite_rules() which transforms them into the regular expressions that many love to hate.

The $args parameter gives you control over how WP_Rewrite::generate_rewrite_rules() works on the new permastruct.

  • since: 2.5.0
  • access: public
void add_permastruct (string $name, string $struct, [array $args = array()])
  • string $name: Name for permalink structure.
  • string $struct: Permalink structure (e.g. category/%category%)
  • array $args: Optional configuration for building the rules from the permalink structure:
    • with_front (bool) - Should the structure be prepended with WP_Rewrite::$front? Default is true.
    • ep_mask (int) - Endpoint mask defining what endpoints are added to the structure. Default is EP_NONE.
    • paged (bool) - Should archive pagination rules be added for the structure? Default is true.
    • feed (bool) - Should feed rewrite rules be added for the structure? Default is true.
    • forcomments (bool) - Should the feed rules be a query for a comments feed? Default is false.
    • walk_dirs (bool) - Should the 'directories' making up the structure be walked over and rewrite rules built for each in turn? Default is true.
    • endpoints (bool) - Should endpoints be applied to the generated rewrite rules? Default is true.
add_rewrite_tag (line 1194)

Add or update existing rewrite tags (e.g. %postname%).

If the tag already exists, replace the existing pattern and query for that tag, otherwise add the new tag.

  • see: WP_Rewrite::$rewritecode
  • see: WP_Rewrite::$queryreplace
  • see: WP_Rewrite::$rewritereplace
  • since: 1.5.0
  • access: public
void add_rewrite_tag (string $tag, string $regex, string $query)
  • string $tag: Name of the rewrite tag to add or update.
  • string $regex: Regular expression to substitute the tag for in rewrite rules.
  • string $query: String to append to the rewritten query. Must end in '='.
add_rule (line 1769)

Add a straight rewrite rule.

Any value in the $after parameter that isn't 'bottom' will be placed at the top of the rules.

  • since: 2.1.0
  • access: public
void add_rule (string $regex, string $redirect, [string $after = 'bottom'])
  • string $regex: Regular expression to match against request.
  • string $redirect: URL regex redirects to when regex matches request.
  • string $after: Optional, default is bottom. Location to place rule.
flush_rules (line 1885)

Remove rewrite rules and then recreate rewrite rules.

Calls WP_Rewrite::wp_rewrite_rules() after removing the 'rewrite_rules' option. If the function named 'save_mod_rewrite_rules' exists, it will be called.

  • since: 2.0.1
  • access: public
void flush_rules ([bool $hard = true])
  • bool $hard: Whether to update .htaccess (hard flush) or just update rewrite_rules option (soft flush). Default is true (hard).
generate_rewrite_rule (line 1496)

Generate Rewrite rules with permalink structure and walking directory only.

Shorten version of WP_Rewrite::generate_rewrite_rules() that allows for shorter list of parameters. See the method for longer description of what generating rewrite rules does.

array generate_rewrite_rule (string $permalink_structure, [bool $walk_dirs = false])
  • string $permalink_structure: The permalink structure to generate rules.
  • bool $walk_dirs: Optional, default is false. Whether to create list of directories to walk over.
generate_rewrite_rules (line 1226)

Generate rewrite rules from a permalink structure.

The main WP_Rewrite function for building the rewrite rule list. The contents of the function is a mix of black magic and regular expressions, so best just ignore the contents and move to the parameters.

array generate_rewrite_rules (string $permalink_structure, [int $ep_mask = EP_NONE], [bool $paged = true], [bool $feed = true], [bool $forcomments = false], [bool $walk_dirs = true], [bool $endpoints = true])
  • string $permalink_structure: The permalink structure.
  • int $ep_mask: Endpoint mask defining what endpoints are added to the structure. Default is EP_NONE.
  • bool $paged: Should archive pagination rules be added for the structure? Default is true.
  • bool $feed: Should feed rewrite rules be added for the structure? Default is true.
  • bool $forcomments: Should the feed rules be a query for a comments feed? Default is false.
  • bool $walk_dirs: Should the 'directories' making up the structure be walked over and rewrite rules built for each in turn? Default is true.
  • bool $endpoints: Should endpoints be applied to the generated rewrite rules? Default is true.
get_author_permastruct (line 1060)

Retrieve the author permalink structure.

The permalink structure is front property, author base, and finally '/%author%'. Will set the author_structure property and then return it without attempting to set the value again.

  • return: False if not found. Permalink structure string.
  • since: 1.5.0
  • access: public
string|bool get_author_permastruct ()
get_category_permastruct (line 1008)

Retrieve the permalink structure for categories.

If the category_base property has no value, then the category structure will have the front property value, followed by 'category', and finally '%category%'. If it does, then the root property will be used, along with the category_base property value.

  • return: False on failure. Category permalink structure.
  • since: 1.5.0
  • access: public
bool|string get_category_permastruct ()
get_comment_feed_permastruct (line 1164)

Retrieve the comment feed permalink structure.

The permalink structure is root property, comment base property, feed base and finally '/%feed%'. Will set the comment_feed_structure property and then return it without attempting to set the value again.

  • return: False if not found. Permalink structure string.
  • since: 1.5.0
  • access: public
string|bool get_comment_feed_permastruct ()
get_date_permastruct (line 889)

Retrieve date permalink structure, with year, month, and day.

The permalink structure for the date, if not set already depends on the permalink structure. It can be one of three formats. The first is year, month, day; the second is day, month, year; and the last format is month, day, year. These are matched against the permalink structure for which one is used. If none matches, then the default will be used, which is year, month, day.

Prevents post ID and date permalinks from overlapping. In the case of post_id, the date permalink will be prepended with front permalink with 'date/' before the actual permalink to form the complete date permalink structure.

  • return: False on no permalink structure. Date permalink structure.
  • since: 1.5.0
  • access: public
bool|string get_date_permastruct ()
get_day_permastruct (line 991)

Retrieve the day permalink structure with month and year.

Keeps date permalink structure with all year, month, and day.

  • return: False on failure. Year/Month/Day structure on success.
  • since: 1.5.0
  • access: public
bool|string get_day_permastruct ()
get_extra_permastruct (line 1038)

Retrieve extra permalink structure by name.

  • return: False if not found. Permalink structure string.
  • since: 2.5.0
  • access: public
string|bool get_extra_permastruct (string $name)
  • string $name: Permalink structure name.
get_feed_permastruct (line 1138)

Retrieve the feed permalink structure.

The permalink structure is root property, feed base, and finally '/%feed%'. Will set the feed_structure property and then return it without attempting to set the value again.

  • return: False if not found. Permalink structure string.
  • since: 1.5.0
  • access: public
string|bool get_feed_permastruct ()
get_month_permastruct (line 968)

Retrieve the month permalink structure without day and with year.

Gets the date permalink structure and strips out the day permalink structures. Keeps the year permalink structure.

  • return: False on failure. Year/Month structure on success.
  • since: 1.5.0
  • access: public
bool|string get_month_permastruct ()
get_page_permastruct (line 1112)

Retrieve the page permalink structure.

The permalink structure is root property, and '%pagename%'. Will set the page_structure property and then return it without attempting to set the value again.

  • return: False if not found. Permalink structure string.
  • since: 1.5.0
  • access: public
string|bool get_page_permastruct ()
get_search_permastruct (line 1086)

Retrieve the search permalink structure.

The permalink structure is root property, search base, and finally '/%search%'. Will set the search_structure property and then return it without attempting to set the value again.

  • return: False if not found. Permalink structure string.
  • since: 1.5.0
  • access: public
string|bool get_search_permastruct ()
get_tag_permastruct (line 1025)

Retrieve the permalink structure for tags.

If the tag_base property has no value, then the tag structure will have the front property value, followed by 'tag', and finally '%tag%'. If it does, then the root property will be used, along with the tag_base property value.

  • return: False on failure. Tag permalink structure.
  • since: 2.3.0
  • access: public
bool|string get_tag_permastruct ()
get_year_permastruct (line 943)

Retrieve the year permalink structure without month and day.

Gets the date permalink structure and strips out the month and day permalink structures.

  • return: False on failure. Year structure on success.
  • since: 1.5.0
  • access: public
bool|string get_year_permastruct ()
iis7_url_rewrite_rules (line 1721)

Retrieve IIS7 URL Rewrite formatted rewrite rules to write to web.config file.

Does not actually write to the web.config file, but creates the rules for the process that will.

  • since: 2.8.0
  • access: public
string iis7_url_rewrite_rules ([ $add_parent_tags = false])
  • $add_parent_tags
init (line 1904)

Sets up the object's properties.

The 'use_verbose_page_rules' object property will be set to true if the permalink structure begins with one of the following: '%postname%', '%category%', '%tag%', or '%author%'.

  • since: 1.5.0
  • access: public
void init ()
mod_rewrite_rules (line 1640)

Retrieve mod_rewrite formatted rewrite rules to write to .htaccess.

Does not actually write to the .htaccess file, but creates the rules for the process that will.

Will add the non_wp_rules property rules to the .htaccess file before the WordPress rewrite rules one.

  • since: 1.5.0
  • access: public
string mod_rewrite_rules ()
page_rewrite_rules (line 862)

Retrieve all of the rewrite rules for pages.

  • since: 1.5.0
  • access: public
array page_rewrite_rules ()
page_uri_index (line 821)

Retrieve all page and attachments for pages URIs.

The attachments are for those that have pages as parents and will be retrieved.

  • return: Array of page URIs as first element and attachment URIs as second element.
  • since: 2.5.0
  • access: public
array page_uri_index ()
preg_index (line 798)

Index for matches for usage in preg_*() functions.

The format of the string is, with empty matches property value, '$NUM'. The 'NUM' will be replaced with the value in the $number parameter. With the matches property not empty, the value of the returned string will contain that value of the matches property. The format then will be '$MATCHES[NUM]', with MATCHES as the value in the property and NUM the value of the $number parameter.

  • since: 1.5.0
  • access: public
string preg_index (int $number)
  • int $number: Index number.
rewrite_rules (line 1518)

Construct rewrite matches and queries from permalink structure.

Runs the action 'generate_rewrite_rules' with the parameter that is an reference to the current WP_Rewrite instance to further manipulate the permalink structures and rewrite rules. Runs the 'rewrite_rules_array' filter on the full rewrite rule array.

There are two ways to manipulate the rewrite rules, one by hooking into the 'generate_rewrite_rules' action and gaining full control of the object or just manipulating the rewrite rule array before it is passed from the function.

  • return: An associate array of matches and queries.
  • since: 1.5.0
  • access: public
array rewrite_rules ()
set_category_base (line 1962)

Set the category base for the category permalink.

Will update the 'category_base' option, if there is a difference between the current category base and the parameter value. Calls WP_Rewrite::init() after the option is updated.

  • since: 1.5.0
  • access: public
void set_category_base (string $category_base)
  • string $category_base: Category permalink structure base.
set_permalink_structure (line 1941)

Set the main permalink structure for the blog.

Will update the 'permalink_structure' option, if there is a difference between the current permalink structure and the parameter value. Calls WP_Rewrite::init() after the option is updated.

Fires the 'permalink_structure_changed' action once the init call has processed passing the old and new values

  • since: 1.5.0
  • access: public
void set_permalink_structure (string $permalink_structure)
  • string $permalink_structure: Permalink structure.
set_tag_base (line 1981)

Set the tag base for the tag permalink.

Will update the 'tag_base' option, if there is a difference between the current tag base and the parameter value. Calls WP_Rewrite::init() after the option is updated.

  • since: 2.3.0
  • access: public
void set_tag_base (string $tag_base)
  • string $tag_base: Tag permalink structure base.
using_index_permalinks (line 754)

Whether permalinks are being used and rewrite module is not enabled.

Means that permalink links are enabled and index.php is in the URL.

  • since: 1.5.0
  • access: public
bool using_index_permalinks ()
using_mod_rewrite_permalinks (line 775)

Whether permalinks are being used and rewrite module is enabled.

Using permalinks and index.php is not in the URL.

  • since: 1.5.0
  • access: public
bool using_mod_rewrite_permalinks ()
using_permalinks (line 740)

Whether permalinks are being used.

This can be either rewrite module or permalink in the HTTP query string.

  • return: True, if permalinks are enabled.
  • since: 1.5.0
  • access: public
bool using_permalinks ()
wp_rewrite_rules (line 1615)

Retrieve the rewrite rules.

The difference between this method and WP_Rewrite::rewrite_rules() is that this method stores the rewrite rules in the 'rewrite_rules' option and retrieves it. This prevents having to process all of the permalinks to get the rewrite rules in the form of caching.

  • return: Rewrite rules.
  • since: 1.5.0
  • access: public
array wp_rewrite_rules ()

Documentation generated on Sat, 18 May 2013 23:19:04 +0000 by phpDocumentor 1.4.1