3 * Rewrite API: WP_Rewrite class
11 * Core class used to implement a rewrite component API.
13 * The WordPress Rewrite class writes the rewrite module rules to the .htaccess
14 * file. It also handles parsing the request to get the correct setup for the
15 * WordPress Query class.
17 * The Rewrite along with WP class function as a front controller for WordPress.
18 * You can add rules to trigger your page view and processing using this
19 * component. The full functionality of a front controller does not exist,
20 * meaning you can't define how the template files load based on the rewrite
27 * Permalink structure for posts.
32 public $permalink_structure;
35 * Whether to add trailing slashes.
40 public $use_trailing_slashes;
43 * Base for the author permalink structure (example.com/$author_base/authorname).
49 var $author_base = 'author';
52 * Permalink structure for author archives.
58 var $author_structure;
61 * Permalink structure for date archives.
70 * Permalink structure for pages.
79 * Base of the search permalink structure (example.com/$search_base/query).
85 var $search_base = 'search';
88 * Permalink structure for searches.
94 var $search_structure;
97 * Comments permalink base.
103 var $comments_base = 'comments';
106 * Pagination permalink base.
111 public $pagination_base = 'page';
114 * Comments pagination permalink base.
120 var $comments_pagination_base = 'comment-page';
123 * Feed permalink base.
129 var $feed_base = 'feed';
132 * Comments feed permalink structure.
138 var $comment_feed_structure;
141 * Feed request permalink structure.
150 * The static portion of the post permalink structure.
152 * If the permalink structure is "/archive/%post_id%" then the front
153 * is "/archive/". If the permalink structure is "/%year%/%postname%/"
154 * then the front is "/".
160 * @see WP_Rewrite::init()
165 * The prefix for all permalink structures.
167 * If PATHINFO/index permalinks are in use then the root is the value of
168 * `WP_Rewrite::$index` with a trailing slash appended. Otherwise the root
175 * @see WP_Rewrite::init()
176 * @see WP_Rewrite::using_index_permalinks()
181 * The name of the index file which is the entry point to all requests.
187 public $index = 'index.php';
190 * Variable name to use for regex matches in the rewritten query.
199 * Rewrite rules to match against the request to find the redirect or query.
208 * Additional rules added external to the rewrite class.
210 * Those not generated by the class, see add_rewrite_rule().
216 var $extra_rules = array();
219 * Additional rules that belong at the beginning to match first.
221 * Those not generated by the class, see add_rewrite_rule().
227 var $extra_rules_top = array();
230 * Rules that don't redirect to WordPress' index.php.
232 * These rules are written to the mod_rewrite portion of the .htaccess,
233 * and are added by add_external_rule().
239 var $non_wp_rules = array();
242 * Extra permalink structures, e.g. categories, added by add_permastruct().
248 var $extra_permastructs = array();
251 * Endpoints (like /trackback/) added by add_rewrite_endpoint().
260 * Whether to write every mod_rewrite rule for WordPress into the .htaccess file.
262 * This is off by default, turning it on might print a lot of rewrite rules
263 * to the .htaccess file.
269 * @see WP_Rewrite::mod_rewrite_rules()
271 public $use_verbose_rules = false;
274 * Could post permalinks be confused with those of pages?
276 * If the first rewrite tag in the post permalink structure is one that could
277 * also match a page name (e.g. %postname% or %author%) then this flag is
278 * set to true. Prior to WordPress 3.3 this flag indicated that every page
279 * would have a set of rules added to the top of the rewrite rules array.
280 * Now it tells WP::parse_request() to check if a URL matching the page
281 * permastruct is actually a page before accepting it.
287 * @see WP_Rewrite::init()
289 public $use_verbose_page_rules = true;
292 * Rewrite tags that can be used in permalink structures.
294 * These are translated into the regular expressions stored in
295 * `WP_Rewrite::$rewritereplace` and are rewritten to the query
296 * variables listed in WP_Rewrite::$queryreplace.
298 * Additional tags can be added with add_rewrite_tag().
304 var $rewritecode = array(
319 * Regular expressions to be substituted into rewrite rules in place
320 * of rewrite tags, see WP_Rewrite::$rewritecode.
326 var $rewritereplace = array(
341 * Query variables that rewrite tags map to, see WP_Rewrite::$rewritecode.
347 var $queryreplace = array(
362 * Supported default feeds.
367 public $feeds = array( 'feed', 'rdf', 'rss', 'rss2', 'atom' );
370 * Determines whether permalinks are being used.
372 * This can be either rewrite module or permalink in the HTTP query string.
377 * @return bool True, if permalinks are enabled.
379 public function using_permalinks() {
380 return ! empty($this->permalink_structure);
384 * Determines whether permalinks are being used and rewrite module is not enabled.
386 * Means that permalink links are enabled and index.php is in the URL.
391 * @return bool Whether permalink links are enabled and index.php is in the URL.
393 public function using_index_permalinks() {
394 if ( empty( $this->permalink_structure ) ) {
398 // If the index is not in the permalink, we're using mod_rewrite.
399 return preg_match( '#^/*' . $this->index . '#', $this->permalink_structure );
403 * Determines whether permalinks are being used and rewrite module is enabled.
405 * Using permalinks and index.php is not in the URL.
410 * @return bool Whether permalink links are enabled and index.php is NOT in the URL.
412 public function using_mod_rewrite_permalinks() {
413 return $this->using_permalinks() && ! $this->using_index_permalinks();
417 * Indexes for matches for usage in preg_*() functions.
419 * The format of the string is, with empty matches property value, '$NUM'.
420 * The 'NUM' will be replaced with the value in the $number parameter. With
421 * the matches property not empty, the value of the returned string will
422 * contain that value of the matches property. The format then will be
423 * '$MATCHES[NUM]', with MATCHES as the value in the property and NUM the
424 * value of the $number parameter.
429 * @param int $number Index number.
432 public function preg_index($number) {
436 if ( ! empty($this->matches) ) {
437 $match_prefix = '$' . $this->matches . '[';
441 return "$match_prefix$number$match_suffix";
445 * Retrieves all page and attachments for pages URIs.
447 * The attachments are for those that have pages as parents and will be
453 * @global wpdb $wpdb WordPress database abstraction object.
455 * @return array Array of page URIs as first element and attachment URIs as second element.
457 public function page_uri_index() {
460 // Get pages in order of hierarchy, i.e. children after parents.
461 $pages = $wpdb->get_results("SELECT ID, post_name, post_parent FROM $wpdb->posts WHERE post_type = 'page' AND post_status != 'auto-draft'");
462 $posts = get_page_hierarchy( $pages );
464 // If we have no pages get out quick.
466 return array( array(), array() );
468 // Now reverse it, because we need parents after children for rewrite rules to work properly.
469 $posts = array_reverse($posts, true);
471 $page_uris = array();
472 $page_attachment_uris = array();
474 foreach ( $posts as $id => $post ) {
476 $uri = get_page_uri($id);
477 $attachments = $wpdb->get_results( $wpdb->prepare( "SELECT ID, post_name, post_parent FROM $wpdb->posts WHERE post_type = 'attachment' AND post_parent = %d", $id ));
478 if ( !empty($attachments) ) {
479 foreach ( $attachments as $attachment ) {
480 $attach_uri = get_page_uri($attachment->ID);
481 $page_attachment_uris[$attach_uri] = $attachment->ID;
485 $page_uris[$uri] = $id;
488 return array( $page_uris, $page_attachment_uris );
492 * Retrieves all of the rewrite rules for pages.
497 * @return array Page rewrite rules.
499 public function page_rewrite_rules() {
500 // The extra .? at the beginning prevents clashes with other regular expressions in the rules array.
501 $this->add_rewrite_tag( '%pagename%', '(.?.+?)', 'pagename=' );
503 return $this->generate_rewrite_rules( $this->get_page_permastruct(), EP_PAGES, true, true, false, false );
507 * Retrieves date permalink structure, with year, month, and day.
509 * The permalink structure for the date, if not set already depends on the
510 * permalink structure. It can be one of three formats. The first is year,
511 * month, day; the second is day, month, year; and the last format is month,
512 * day, year. These are matched against the permalink structure for which
513 * one is used. If none matches, then the default will be used, which is
516 * Prevents post ID and date permalinks from overlapping. In the case of
517 * post_id, the date permalink will be prepended with front permalink with
518 * 'date/' before the actual permalink to form the complete date permalink
524 * @return string|false False on no permalink structure. Date permalink structure.
526 public function get_date_permastruct() {
527 if ( isset($this->date_structure) )
528 return $this->date_structure;
530 if ( empty($this->permalink_structure) ) {
531 $this->date_structure = '';
535 // The date permalink must have year, month, and day separated by slashes.
536 $endians = array('%year%/%monthnum%/%day%', '%day%/%monthnum%/%year%', '%monthnum%/%day%/%year%');
538 $this->date_structure = '';
541 foreach ( $endians as $endian ) {
542 if ( false !== strpos($this->permalink_structure, $endian) ) {
543 $date_endian= $endian;
548 if ( empty($date_endian) )
549 $date_endian = '%year%/%monthnum%/%day%';
552 * Do not allow the date tags and %post_id% to overlap in the permalink
553 * structure. If they do, move the date tags to $front/date/.
555 $front = $this->front;
556 preg_match_all('/%.+?%/', $this->permalink_structure, $tokens);
558 foreach ( (array) $tokens[0] as $token) {
559 if ( '%post_id%' == $token && ($tok_index <= 3) ) {
560 $front = $front . 'date/';
566 $this->date_structure = $front . $date_endian;
568 return $this->date_structure;
572 * Retrieves the year permalink structure without month and day.
574 * Gets the date permalink structure and strips out the month and day
575 * permalink structures.
580 * @return false|string False on failure. Year structure on success.
582 public function get_year_permastruct() {
583 $structure = $this->get_date_permastruct();
585 if ( empty($structure) )
588 $structure = str_replace('%monthnum%', '', $structure);
589 $structure = str_replace('%day%', '', $structure);
590 $structure = preg_replace('#/+#', '/', $structure);
596 * Retrieves the month permalink structure without day and with year.
598 * Gets the date permalink structure and strips out the day permalink
599 * structures. Keeps the year permalink structure.
604 * @return false|string False on failure. Year/Month structure on success.
606 public function get_month_permastruct() {
607 $structure = $this->get_date_permastruct();
609 if ( empty($structure) )
612 $structure = str_replace('%day%', '', $structure);
613 $structure = preg_replace('#/+#', '/', $structure);
619 * Retrieves the day permalink structure with month and year.
621 * Keeps date permalink structure with all year, month, and day.
626 * @return string|false False on failure. Year/Month/Day structure on success.
628 public function get_day_permastruct() {
629 return $this->get_date_permastruct();
633 * Retrieves the permalink structure for categories.
635 * If the category_base property has no value, then the category structure
636 * will have the front property value, followed by 'category', and finally
637 * '%category%'. If it does, then the root property will be used, along with
638 * the category_base property value.
643 * @return string|false False on failure. Category permalink structure.
645 public function get_category_permastruct() {
646 return $this->get_extra_permastruct('category');
650 * Retrieve the permalink structure for tags.
652 * If the tag_base property has no value, then the tag structure will have
653 * the front property value, followed by 'tag', and finally '%tag%'. If it
654 * does, then the root property will be used, along with the tag_base
660 * @return string|false False on failure. Tag permalink structure.
662 public function get_tag_permastruct() {
663 return $this->get_extra_permastruct('post_tag');
667 * Retrieves an extra permalink structure by name.
672 * @param string $name Permalink structure name.
673 * @return string|false False if not found. Permalink structure string.
675 public function get_extra_permastruct($name) {
676 if ( empty($this->permalink_structure) )
679 if ( isset($this->extra_permastructs[$name]) )
680 return $this->extra_permastructs[$name]['struct'];
686 * Retrieves the author permalink structure.
688 * The permalink structure is front property, author base, and finally
689 * '/%author%'. Will set the author_structure property and then return it
690 * without attempting to set the value again.
695 * @return string|false False if not found. Permalink structure string.
697 public function get_author_permastruct() {
698 if ( isset($this->author_structure) )
699 return $this->author_structure;
701 if ( empty($this->permalink_structure) ) {
702 $this->author_structure = '';
706 $this->author_structure = $this->front . $this->author_base . '/%author%';
708 return $this->author_structure;
712 * Retrieves the search permalink structure.
714 * The permalink structure is root property, search base, and finally
715 * '/%search%'. Will set the search_structure property and then return it
716 * without attempting to set the value again.
721 * @return string|false False if not found. Permalink structure string.
723 public function get_search_permastruct() {
724 if ( isset($this->search_structure) )
725 return $this->search_structure;
727 if ( empty($this->permalink_structure) ) {
728 $this->search_structure = '';
732 $this->search_structure = $this->root . $this->search_base . '/%search%';
734 return $this->search_structure;
738 * Retrieves the page permalink structure.
740 * The permalink structure is root property, and '%pagename%'. Will set the
741 * page_structure property and then return it without attempting to set the
747 * @return string|false False if not found. Permalink structure string.
749 public function get_page_permastruct() {
750 if ( isset($this->page_structure) )
751 return $this->page_structure;
753 if (empty($this->permalink_structure)) {
754 $this->page_structure = '';
758 $this->page_structure = $this->root . '%pagename%';
760 return $this->page_structure;
764 * Retrieves the feed permalink structure.
766 * The permalink structure is root property, feed base, and finally
767 * '/%feed%'. Will set the feed_structure property and then return it
768 * without attempting to set the value again.
773 * @return string|false False if not found. Permalink structure string.
775 public function get_feed_permastruct() {
776 if ( isset($this->feed_structure) )
777 return $this->feed_structure;
779 if ( empty($this->permalink_structure) ) {
780 $this->feed_structure = '';
784 $this->feed_structure = $this->root . $this->feed_base . '/%feed%';
786 return $this->feed_structure;
790 * Retrieves the comment feed permalink structure.
792 * The permalink structure is root property, comment base property, feed
793 * base and finally '/%feed%'. Will set the comment_feed_structure property
794 * and then return it without attempting to set the value again.
799 * @return string|false False if not found. Permalink structure string.
801 public function get_comment_feed_permastruct() {
802 if ( isset($this->comment_feed_structure) )
803 return $this->comment_feed_structure;
805 if (empty($this->permalink_structure)) {
806 $this->comment_feed_structure = '';
810 $this->comment_feed_structure = $this->root . $this->comments_base . '/' . $this->feed_base . '/%feed%';
812 return $this->comment_feed_structure;
816 * Adds or updates existing rewrite tags (e.g. %postname%).
818 * If the tag already exists, replace the existing pattern and query for
819 * that tag, otherwise add the new tag.
824 * @see WP_Rewrite::$rewritecode
825 * @see WP_Rewrite::$rewritereplace
826 * @see WP_Rewrite::$queryreplace
828 * @param string $tag Name of the rewrite tag to add or update.
829 * @param string $regex Regular expression to substitute the tag for in rewrite rules.
830 * @param string $query String to append to the rewritten query. Must end in '='.
832 public function add_rewrite_tag( $tag, $regex, $query ) {
833 $position = array_search( $tag, $this->rewritecode );
834 if ( false !== $position && null !== $position ) {
835 $this->rewritereplace[ $position ] = $regex;
836 $this->queryreplace[ $position ] = $query;
838 $this->rewritecode[] = $tag;
839 $this->rewritereplace[] = $regex;
840 $this->queryreplace[] = $query;
845 * Generates rewrite rules from a permalink structure.
847 * The main WP_Rewrite function for building the rewrite rule list. The
848 * contents of the function is a mix of black magic and regular expressions,
849 * so best just ignore the contents and move to the parameters.
854 * @param string $permalink_structure The permalink structure.
855 * @param int $ep_mask Optional. Endpoint mask defining what endpoints are added to the structure.
856 * Accepts `EP_NONE`, `EP_PERMALINK`, `EP_ATTACHMENT`, `EP_DATE`, `EP_YEAR`,
857 * `EP_MONTH`, `EP_DAY`, `EP_ROOT`, `EP_COMMENTS`, `EP_SEARCH`, `EP_CATEGORIES`,
858 * `EP_TAGS`, `EP_AUTHORS`, `EP_PAGES`, `EP_ALL_ARCHIVES`, and `EP_ALL`.
860 * @param bool $paged Optional. Whether archive pagination rules should be added for the structure.
862 * @param bool $feed Optional Whether feed rewrite rules should be added for the structure.
864 * @param bool $forcomments Optional. Whether the feed rules should be a query for a comments feed.
866 * @param bool $walk_dirs Optional. Whether the 'directories' making up the structure should be walked
867 * over and rewrite rules built for each in-turn. Default true.
868 * @param bool $endpoints Optional. Whether endpoints should be applied to the generated rewrite rules.
870 * @return array Rewrite rule list.
872 public function generate_rewrite_rules($permalink_structure, $ep_mask = EP_NONE, $paged = true, $feed = true, $forcomments = false, $walk_dirs = true, $endpoints = true) {
873 // Build a regex to match the feed section of URLs, something like (feed|atom|rss|rss2)/?
875 foreach ( (array) $this->feeds as $feed_name)
876 $feedregex2 .= $feed_name . '|';
877 $feedregex2 = '(' . trim($feedregex2, '|') . ')/?$';
880 * $feedregex is identical but with /feed/ added on as well, so URLs like <permalink>/feed/atom
881 * and <permalink>/atom are both possible
883 $feedregex = $this->feed_base . '/' . $feedregex2;
885 // Build a regex to match the trackback and page/xx parts of URLs.
886 $trackbackregex = 'trackback/?$';
887 $pageregex = $this->pagination_base . '/?([0-9]{1,})/?$';
888 $commentregex = $this->comments_pagination_base . '-([0-9]{1,})/?$';
889 $embedregex = 'embed/?$';
891 // Build up an array of endpoint regexes to append => queries to append.
893 $ep_query_append = array ();
894 foreach ( (array) $this->endpoints as $endpoint) {
895 // Match everything after the endpoint name, but allow for nothing to appear there.
896 $epmatch = $endpoint[1] . '(/(.*))?/?$';
898 // This will be appended on to the rest of the query for each dir.
899 $epquery = '&' . $endpoint[2] . '=';
900 $ep_query_append[$epmatch] = array ( $endpoint[0], $epquery );
904 // Get everything up to the first rewrite tag.
905 $front = substr($permalink_structure, 0, strpos($permalink_structure, '%'));
907 // Build an array of the tags (note that said array ends up being in $tokens[0]).
908 preg_match_all('/%.+?%/', $permalink_structure, $tokens);
910 $num_tokens = count($tokens[0]);
912 $index = $this->index; //probably 'index.php'
914 $trackbackindex = $index;
915 $embedindex = $index;
918 * Build a list from the rewritecode and queryreplace arrays, that will look something
919 * like tagname=$matches[i] where i is the current $i.
922 for ( $i = 0; $i < $num_tokens; ++$i ) {
924 $queries[$i] = $queries[$i - 1] . '&';
928 $query_token = str_replace($this->rewritecode, $this->queryreplace, $tokens[0][$i]) . $this->preg_index($i+1);
929 $queries[$i] .= $query_token;
932 // Get the structure, minus any cruft (stuff that isn't tags) at the front.
933 $structure = $permalink_structure;
935 $structure = str_replace($front, '', $structure);
938 * Create a list of dirs to walk over, making rewrite rules for each level
939 * so for example, a $structure of /%year%/%monthnum%/%postname% would create
940 * rewrite rules for /%year%/, /%year%/%monthnum%/ and /%year%/%monthnum%/%postname%
942 $structure = trim($structure, '/');
943 $dirs = $walk_dirs ? explode('/', $structure) : array( $structure );
944 $num_dirs = count($dirs);
946 // Strip slashes from the front of $front.
947 $front = preg_replace('|^/+|', '', $front);
949 // The main workhorse loop.
950 $post_rewrite = array();
952 for ( $j = 0; $j < $num_dirs; ++$j ) {
953 // Get the struct for this dir, and trim slashes off the front.
954 $struct .= $dirs[$j] . '/'; // Accumulate. see comment near explode('/', $structure) above.
955 $struct = ltrim($struct, '/');
957 // Replace tags with regexes.
958 $match = str_replace($this->rewritecode, $this->rewritereplace, $struct);
960 // Make a list of tags, and store how many there are in $num_toks.
961 $num_toks = preg_match_all('/%.+?%/', $struct, $toks);
963 // Get the 'tagname=$matches[i]'.
964 $query = ( ! empty( $num_toks ) && isset( $queries[$num_toks - 1] ) ) ? $queries[$num_toks - 1] : '';
966 // Set up $ep_mask_specific which is used to match more specific URL types.
967 switch ( $dirs[$j] ) {
969 $ep_mask_specific = EP_YEAR;
972 $ep_mask_specific = EP_MONTH;
975 $ep_mask_specific = EP_DAY;
978 $ep_mask_specific = EP_NONE;
981 // Create query for /page/xx.
982 $pagematch = $match . $pageregex;
983 $pagequery = $index . '?' . $query . '&paged=' . $this->preg_index($num_toks + 1);
985 // Create query for /comment-page-xx.
986 $commentmatch = $match . $commentregex;
987 $commentquery = $index . '?' . $query . '&cpage=' . $this->preg_index($num_toks + 1);
989 if ( get_option('page_on_front') ) {
990 // Create query for Root /comment-page-xx.
991 $rootcommentmatch = $match . $commentregex;
992 $rootcommentquery = $index . '?' . $query . '&page_id=' . get_option('page_on_front') . '&cpage=' . $this->preg_index($num_toks + 1);
995 // Create query for /feed/(feed|atom|rss|rss2|rdf).
996 $feedmatch = $match . $feedregex;
997 $feedquery = $feedindex . '?' . $query . '&feed=' . $this->preg_index($num_toks + 1);
999 // Create query for /(feed|atom|rss|rss2|rdf) (see comment near creation of $feedregex).
1000 $feedmatch2 = $match . $feedregex2;
1001 $feedquery2 = $feedindex . '?' . $query . '&feed=' . $this->preg_index($num_toks + 1);
1003 // If asked to, turn the feed queries into comment feed ones.
1004 if ( $forcomments ) {
1005 $feedquery .= '&withcomments=1';
1006 $feedquery2 .= '&withcomments=1';
1009 // Start creating the array of rewrites for this dir.
1012 // ...adding on /feed/ regexes => queries
1014 $rewrite = array( $feedmatch => $feedquery, $feedmatch2 => $feedquery2 );
1017 //...and /page/xx ones
1019 $rewrite = array_merge( $rewrite, array( $pagematch => $pagequery ) );
1022 // Only on pages with comments add ../comment-page-xx/.
1023 if ( EP_PAGES & $ep_mask || EP_PERMALINK & $ep_mask ) {
1024 $rewrite = array_merge($rewrite, array($commentmatch => $commentquery));
1025 } elseif ( EP_ROOT & $ep_mask && get_option('page_on_front') ) {
1026 $rewrite = array_merge($rewrite, array($rootcommentmatch => $rootcommentquery));
1031 foreach ( (array) $ep_query_append as $regex => $ep) {
1032 // Add the endpoints on if the mask fits.
1033 if ( $ep[0] & $ep_mask || $ep[0] & $ep_mask_specific )
1034 $rewrite[$match . $regex] = $index . '?' . $query . $ep[1] . $this->preg_index($num_toks + 2);
1038 // If we've got some tags in this dir.
1044 * Check to see if this dir is permalink-level: i.e. the structure specifies an
1045 * individual post. Do this by checking it contains at least one of 1) post name,
1046 * 2) post ID, 3) page name, 4) timestamp (year, month, day, hour, second and
1047 * minute all present). Set these flags now as we need them for the endpoints.
1049 if ( strpos($struct, '%postname%') !== false
1050 || strpos($struct, '%post_id%') !== false
1051 || strpos($struct, '%pagename%') !== false
1052 || (strpos($struct, '%year%') !== false && strpos($struct, '%monthnum%') !== false && strpos($struct, '%day%') !== false && strpos($struct, '%hour%') !== false && strpos($struct, '%minute%') !== false && strpos($struct, '%second%') !== false)
1055 if ( strpos($struct, '%pagename%') !== false )
1060 // For custom post types, we need to add on endpoints as well.
1061 foreach ( get_post_types( array('_builtin' => false ) ) as $ptype ) {
1062 if ( strpos($struct, "%$ptype%") !== false ) {
1065 // This is for page style attachment URLs.
1066 $page = is_post_type_hierarchical( $ptype );
1072 // If creating rules for a permalink, do all the endpoints like attachments etc.
1074 // Create query and regex for trackback.
1075 $trackbackmatch = $match . $trackbackregex;
1076 $trackbackquery = $trackbackindex . '?' . $query . '&tb=1';
1078 // Create query and regex for embeds.
1079 $embedmatch = $match . $embedregex;
1080 $embedquery = $embedindex . '?' . $query . '&embed=true';
1082 // Trim slashes from the end of the regex for this dir.
1083 $match = rtrim($match, '/');
1085 // Get rid of brackets.
1086 $submatchbase = str_replace( array('(', ')'), '', $match);
1088 // Add a rule for at attachments, which take the form of <permalink>/some-text.
1089 $sub1 = $submatchbase . '/([^/]+)/';
1091 // Add trackback regex <permalink>/trackback/...
1092 $sub1tb = $sub1 . $trackbackregex;
1094 // And <permalink>/feed/(atom|...)
1095 $sub1feed = $sub1 . $feedregex;
1097 // And <permalink>/(feed|atom...)
1098 $sub1feed2 = $sub1 . $feedregex2;
1100 // And <permalink>/comment-page-xx
1101 $sub1comment = $sub1 . $commentregex;
1103 // And <permalink>/embed/...
1104 $sub1embed = $sub1 . $embedregex;
1107 * Add another rule to match attachments in the explicit form:
1108 * <permalink>/attachment/some-text
1110 $sub2 = $submatchbase . '/attachment/([^/]+)/';
1112 // And add trackbacks <permalink>/attachment/trackback.
1113 $sub2tb = $sub2 . $trackbackregex;
1115 // Feeds, <permalink>/attachment/feed/(atom|...)
1116 $sub2feed = $sub2 . $feedregex;
1118 // And feeds again on to this <permalink>/attachment/(feed|atom...)
1119 $sub2feed2 = $sub2 . $feedregex2;
1121 // And <permalink>/comment-page-xx
1122 $sub2comment = $sub2 . $commentregex;
1124 // And <permalink>/embed/...
1125 $sub2embed = $sub2 . $embedregex;
1127 // Create queries for these extra tag-ons we've just dealt with.
1128 $subquery = $index . '?attachment=' . $this->preg_index(1);
1129 $subtbquery = $subquery . '&tb=1';
1130 $subfeedquery = $subquery . '&feed=' . $this->preg_index(2);
1131 $subcommentquery = $subquery . '&cpage=' . $this->preg_index(2);
1132 $subembedquery = $subquery . '&embed=true';
1134 // Do endpoints for attachments.
1135 if ( !empty($endpoints) ) {
1136 foreach ( (array) $ep_query_append as $regex => $ep ) {
1137 if ( $ep[0] & EP_ATTACHMENT ) {
1138 $rewrite[$sub1 . $regex] = $subquery . $ep[1] . $this->preg_index(3);
1139 $rewrite[$sub2 . $regex] = $subquery . $ep[1] . $this->preg_index(3);
1145 * Now we've finished with endpoints, finish off the $sub1 and $sub2 matches
1146 * add a ? as we don't have to match that last slash, and finally a $ so we
1147 * match to the end of the URL
1153 * Post pagination, e.g. <permalink>/2/
1154 * Previously: '(/[0-9]+)?/?$', which produced '/2' for page.
1155 * When cast to int, returned 0.
1157 $match = $match . '(?:/([0-9]+))?/?$';
1158 $query = $index . '?' . $query . '&page=' . $this->preg_index($num_toks + 1);
1160 // Not matching a permalink so this is a lot simpler.
1162 // Close the match and finalise the query.
1164 $query = $index . '?' . $query;
1168 * Create the final array for this dir by joining the $rewrite array (which currently
1169 * only contains rules/queries for trackback, pages etc) to the main regex/query for
1172 $rewrite = array_merge($rewrite, array($match => $query));
1174 // If we're matching a permalink, add those extras (attachments etc) on.
1177 $rewrite = array_merge(array($trackbackmatch => $trackbackquery), $rewrite);
1180 $rewrite = array_merge( array( $embedmatch => $embedquery ), $rewrite );
1182 // Add regexes/queries for attachments, attachment trackbacks and so on.
1184 // Require <permalink>/attachment/stuff form for pages because of confusion with subpages.
1185 $rewrite = array_merge( $rewrite, array(
1187 $sub1tb => $subtbquery,
1188 $sub1feed => $subfeedquery,
1189 $sub1feed2 => $subfeedquery,
1190 $sub1comment => $subcommentquery,
1191 $sub1embed => $subembedquery
1195 $rewrite = array_merge( array( $sub2 => $subquery, $sub2tb => $subtbquery, $sub2feed => $subfeedquery, $sub2feed2 => $subfeedquery, $sub2comment => $subcommentquery, $sub2embed => $subembedquery ), $rewrite );
1198 // Add the rules for this dir to the accumulating $post_rewrite.
1199 $post_rewrite = array_merge($rewrite, $post_rewrite);
1202 // The finished rules. phew!
1203 return $post_rewrite;
1207 * Generates rewrite rules with permalink structure and walking directory only.
1209 * Shorten version of WP_Rewrite::generate_rewrite_rules() that allows for shorter
1210 * list of parameters. See the method for longer description of what generating
1211 * rewrite rules does.
1216 * @see WP_Rewrite::generate_rewrite_rules() See for long description and rest of parameters.
1218 * @param string $permalink_structure The permalink structure to generate rules.
1219 * @param bool $walk_dirs Optional, default is false. Whether to create list of directories to walk over.
1222 public function generate_rewrite_rule($permalink_structure, $walk_dirs = false) {
1223 return $this->generate_rewrite_rules($permalink_structure, EP_NONE, false, false, false, $walk_dirs);
1227 * Constructs rewrite matches and queries from permalink structure.
1229 * Runs the action 'generate_rewrite_rules' with the parameter that is an
1230 * reference to the current WP_Rewrite instance to further manipulate the
1231 * permalink structures and rewrite rules. Runs the 'rewrite_rules_array'
1232 * filter on the full rewrite rule array.
1234 * There are two ways to manipulate the rewrite rules, one by hooking into
1235 * the 'generate_rewrite_rules' action and gaining full control of the
1236 * object or just manipulating the rewrite rule array before it is passed
1237 * from the function.
1242 * @return array An associate array of matches and queries.
1244 public function rewrite_rules() {
1247 if ( empty($this->permalink_structure) )
1250 // robots.txt -only if installed at the root
1251 $home_path = parse_url( home_url() );
1252 $robots_rewrite = ( empty( $home_path['path'] ) || '/' == $home_path['path'] ) ? array( 'robots\.txt$' => $this->index . '?robots=1' ) : array();
1254 // Old feed and service files.
1255 $deprecated_files = array(
1256 '.*wp-(atom|rdf|rss|rss2|feed|commentsrss2)\.php$' => $this->index . '?feed=old',
1257 '.*wp-app\.php(/.*)?$' => $this->index . '?error=403',
1260 // Registration rules.
1261 $registration_pages = array();
1262 if ( is_multisite() && is_main_site() ) {
1263 $registration_pages['.*wp-signup.php$'] = $this->index . '?signup=true';
1264 $registration_pages['.*wp-activate.php$'] = $this->index . '?activate=true';
1268 $registration_pages['.*wp-register.php$'] = $this->index . '?register=true';
1270 // Post rewrite rules.
1271 $post_rewrite = $this->generate_rewrite_rules( $this->permalink_structure, EP_PERMALINK );
1274 * Filter rewrite rules used for "post" archives.
1278 * @param array $post_rewrite The rewrite rules for posts.
1280 $post_rewrite = apply_filters( 'post_rewrite_rules', $post_rewrite );
1282 // Date rewrite rules.
1283 $date_rewrite = $this->generate_rewrite_rules($this->get_date_permastruct(), EP_DATE);
1286 * Filter rewrite rules used for date archives.
1288 * Likely date archives would include /yyyy/, /yyyy/mm/, and /yyyy/mm/dd/.
1292 * @param array $date_rewrite The rewrite rules for date archives.
1294 $date_rewrite = apply_filters( 'date_rewrite_rules', $date_rewrite );
1296 // Root-level rewrite rules.
1297 $root_rewrite = $this->generate_rewrite_rules($this->root . '/', EP_ROOT);
1300 * Filter rewrite rules used for root-level archives.
1302 * Likely root-level archives would include pagination rules for the homepage
1303 * as well as site-wide post feeds (e.g. /feed/, and /feed/atom/).
1307 * @param array $root_rewrite The root-level rewrite rules.
1309 $root_rewrite = apply_filters( 'root_rewrite_rules', $root_rewrite );
1311 // Comments rewrite rules.
1312 $comments_rewrite = $this->generate_rewrite_rules($this->root . $this->comments_base, EP_COMMENTS, false, true, true, false);
1315 * Filter rewrite rules used for comment feed archives.
1317 * Likely comments feed archives include /comments/feed/, and /comments/feed/atom/.
1321 * @param array $comments_rewrite The rewrite rules for the site-wide comments feeds.
1323 $comments_rewrite = apply_filters( 'comments_rewrite_rules', $comments_rewrite );
1325 // Search rewrite rules.
1326 $search_structure = $this->get_search_permastruct();
1327 $search_rewrite = $this->generate_rewrite_rules($search_structure, EP_SEARCH);
1330 * Filter rewrite rules used for search archives.
1332 * Likely search-related archives include /search/search+query/ as well as
1333 * pagination and feed paths for a search.
1337 * @param array $search_rewrite The rewrite rules for search queries.
1339 $search_rewrite = apply_filters( 'search_rewrite_rules', $search_rewrite );
1341 // Author rewrite rules.
1342 $author_rewrite = $this->generate_rewrite_rules($this->get_author_permastruct(), EP_AUTHORS);
1345 * Filter rewrite rules used for author archives.
1347 * Likely author archives would include /author/author-name/, as well as
1348 * pagination and feed paths for author archives.
1352 * @param array $author_rewrite The rewrite rules for author archives.
1354 $author_rewrite = apply_filters( 'author_rewrite_rules', $author_rewrite );
1356 // Pages rewrite rules.
1357 $page_rewrite = $this->page_rewrite_rules();
1360 * Filter rewrite rules used for "page" post type archives.
1364 * @param array $page_rewrite The rewrite rules for the "page" post type.
1366 $page_rewrite = apply_filters( 'page_rewrite_rules', $page_rewrite );
1368 // Extra permastructs.
1369 foreach ( $this->extra_permastructs as $permastructname => $struct ) {
1370 if ( is_array( $struct ) ) {
1371 if ( count( $struct ) == 2 )
1372 $rules = $this->generate_rewrite_rules( $struct[0], $struct[1] );
1374 $rules = $this->generate_rewrite_rules( $struct['struct'], $struct['ep_mask'], $struct['paged'], $struct['feed'], $struct['forcomments'], $struct['walk_dirs'], $struct['endpoints'] );
1376 $rules = $this->generate_rewrite_rules( $struct );
1380 * Filter rewrite rules used for individual permastructs.
1382 * The dynamic portion of the hook name, `$permastructname`, refers
1383 * to the name of the registered permastruct, e.g. 'post_tag' (tags),
1384 * 'category' (categories), etc.
1388 * @param array $rules The rewrite rules generated for the current permastruct.
1390 $rules = apply_filters( $permastructname . '_rewrite_rules', $rules );
1391 if ( 'post_tag' == $permastructname ) {
1394 * Filter rewrite rules used specifically for Tags.
1397 * @deprecated 3.1.0 Use 'post_tag_rewrite_rules' instead
1399 * @param array $rules The rewrite rules generated for tags.
1401 $rules = apply_filters( 'tag_rewrite_rules', $rules );
1404 $this->extra_rules_top = array_merge($this->extra_rules_top, $rules);
1407 // Put them together.
1408 if ( $this->use_verbose_page_rules )
1409 $this->rules = array_merge($this->extra_rules_top, $robots_rewrite, $deprecated_files, $registration_pages, $root_rewrite, $comments_rewrite, $search_rewrite, $author_rewrite, $date_rewrite, $page_rewrite, $post_rewrite, $this->extra_rules);
1411 $this->rules = array_merge($this->extra_rules_top, $robots_rewrite, $deprecated_files, $registration_pages, $root_rewrite, $comments_rewrite, $search_rewrite, $author_rewrite, $date_rewrite, $post_rewrite, $page_rewrite, $this->extra_rules);
1414 * Fires after the rewrite rules are generated.
1418 * @param WP_Rewrite $this Current WP_Rewrite instance, passed by reference.
1420 do_action_ref_array( 'generate_rewrite_rules', array( &$this ) );
1423 * Filter the full set of generated rewrite rules.
1427 * @param array $this->rules The compiled array of rewrite rules.
1429 $this->rules = apply_filters( 'rewrite_rules_array', $this->rules );
1431 return $this->rules;
1435 * Retrieves the rewrite rules.
1437 * The difference between this method and WP_Rewrite::rewrite_rules() is that
1438 * this method stores the rewrite rules in the 'rewrite_rules' option and retrieves
1439 * it. This prevents having to process all of the permalinks to get the rewrite rules
1440 * in the form of caching.
1445 * @return array Rewrite rules.
1447 public function wp_rewrite_rules() {
1448 $this->rules = get_option('rewrite_rules');
1449 if ( empty($this->rules) ) {
1450 $this->matches = 'matches';
1451 $this->rewrite_rules();
1452 update_option('rewrite_rules', $this->rules);
1455 return $this->rules;
1459 * Retrieves mod_rewrite-formatted rewrite rules to write to .htaccess.
1461 * Does not actually write to the .htaccess file, but creates the rules for
1462 * the process that will.
1464 * Will add the non_wp_rules property rules to the .htaccess file before
1465 * the WordPress rewrite rules one.
1472 public function mod_rewrite_rules() {
1473 if ( ! $this->using_permalinks() )
1476 $site_root = parse_url( site_url() );
1477 if ( isset( $site_root['path'] ) )
1478 $site_root = trailingslashit($site_root['path']);
1480 $home_root = parse_url(home_url());
1481 if ( isset( $home_root['path'] ) )
1482 $home_root = trailingslashit($home_root['path']);
1486 $rules = "<IfModule mod_rewrite.c>\n";
1487 $rules .= "RewriteEngine On\n";
1488 $rules .= "RewriteBase $home_root\n";
1490 // Prevent -f checks on index.php.
1491 $rules .= "RewriteRule ^index\.php$ - [L]\n";
1493 // Add in the rules that don't redirect to WP's index.php (and thus shouldn't be handled by WP at all).
1494 foreach ( (array) $this->non_wp_rules as $match => $query) {
1495 // Apache 1.3 does not support the reluctant (non-greedy) modifier.
1496 $match = str_replace('.+?', '.+', $match);
1498 $rules .= 'RewriteRule ^' . $match . ' ' . $home_root . $query . " [QSA,L]\n";
1501 if ( $this->use_verbose_rules ) {
1502 $this->matches = '';
1503 $rewrite = $this->rewrite_rules();
1504 $num_rules = count($rewrite);
1505 $rules .= "RewriteCond %{REQUEST_FILENAME} -f [OR]\n" .
1506 "RewriteCond %{REQUEST_FILENAME} -d\n" .
1507 "RewriteRule ^.*$ - [S=$num_rules]\n";
1509 foreach ( (array) $rewrite as $match => $query) {
1510 // Apache 1.3 does not support the reluctant (non-greedy) modifier.
1511 $match = str_replace('.+?', '.+', $match);
1513 if ( strpos($query, $this->index) !== false )
1514 $rules .= 'RewriteRule ^' . $match . ' ' . $home_root . $query . " [QSA,L]\n";
1516 $rules .= 'RewriteRule ^' . $match . ' ' . $site_root . $query . " [QSA,L]\n";
1519 $rules .= "RewriteCond %{REQUEST_FILENAME} !-f\n" .
1520 "RewriteCond %{REQUEST_FILENAME} !-d\n" .
1521 "RewriteRule . {$home_root}{$this->index} [L]\n";
1524 $rules .= "</IfModule>\n";
1527 * Filter the list of rewrite rules formatted for output to an .htaccess file.
1531 * @param string $rules mod_rewrite Rewrite rules formatted for .htaccess.
1533 $rules = apply_filters( 'mod_rewrite_rules', $rules );
1536 * Filter the list of rewrite rules formatted for output to an .htaccess file.
1539 * @deprecated 1.5.0 Use the mod_rewrite_rules filter instead.
1541 * @param string $rules mod_rewrite Rewrite rules formatted for .htaccess.
1543 return apply_filters( 'rewrite_rules', $rules );
1547 * Retrieves IIS7 URL Rewrite formatted rewrite rules to write to web.config file.
1549 * Does not actually write to the web.config file, but creates the rules for
1550 * the process that will.
1557 public function iis7_url_rewrite_rules( $add_parent_tags = false ) {
1558 if ( ! $this->using_permalinks() )
1561 if ( $add_parent_tags ) {
1562 $rules .= '<configuration>
1569 <rule name="wordpress" patternSyntax="Wildcard">
1572 <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
1573 <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
1575 <action type="Rewrite" url="index.php" />
1578 if ( $add_parent_tags ) {
1587 * Filter the list of rewrite rules formatted for output to a web.config.
1591 * @param string $rules Rewrite rules formatted for IIS web.config.
1593 return apply_filters( 'iis7_url_rewrite_rules', $rules );
1597 * Adds a rewrite rule that transforms a URL structure to a set of query vars.
1599 * Any value in the $after parameter that isn't 'bottom' will result in the rule
1600 * being placed at the top of the rewrite rules.
1603 * @since 4.4.0 Array support was added to the `$query` parameter.
1606 * @param string $regex Regular expression to match request against.
1607 * @param string|array $query The corresponding query vars for this rewrite rule.
1608 * @param string $after Optional. Priority of the new rule. Accepts 'top'
1609 * or 'bottom'. Default 'bottom'.
1611 public function add_rule( $regex, $query, $after = 'bottom' ) {
1612 if ( is_array( $query ) ) {
1614 $query = add_query_arg( $query, 'index.php' );
1616 $index = false === strpos( $query, '?' ) ? strlen( $query ) : strpos( $query, '?' );
1617 $front = substr( $query, 0, $index );
1619 $external = $front != $this->index;
1622 // "external" = it doesn't correspond to index.php.
1624 $this->add_external_rule( $regex, $query );
1626 if ( 'bottom' == $after ) {
1627 $this->extra_rules = array_merge( $this->extra_rules, array( $regex => $query ) );
1629 $this->extra_rules_top = array_merge( $this->extra_rules_top, array( $regex => $query ) );
1635 * Adds a rewrite rule that doesn't correspond to index.php.
1640 * @param string $regex Regular expression to match request against.
1641 * @param string $query The corresponding query vars for this rewrite rule.
1643 public function add_external_rule( $regex, $query ) {
1644 $this->non_wp_rules[ $regex ] = $query;
1648 * Adds an endpoint, like /trackback/.
1651 * @since 3.9.0 $query_var parameter added.
1652 * @since 4.3.0 Added support for skipping query var registration by passing `false` to `$query_var`.
1655 * @see add_rewrite_endpoint() for full documentation.
1658 * @param string $name Name of the endpoint.
1659 * @param int $places Endpoint mask describing the places the endpoint should be added.
1660 * @param string|bool $query_var Optional. Name of the corresponding query variable. Pass `false` to
1661 * skip registering a query_var for this endpoint. Defaults to the
1664 public function add_endpoint( $name, $places, $query_var = true ) {
1667 // For backward compatibility, if null has explicitly been passed as `$query_var`, assume `true`.
1668 if ( true === $query_var || null === func_get_arg( 2 ) ) {
1671 $this->endpoints[] = array( $places, $name, $query_var );
1674 $wp->add_query_var( $query_var );
1679 * Adds a new permalink structure.
1681 * A permalink structure (permastruct) is an abstract definition of a set of rewrite rules;
1682 * it is an easy way of expressing a set of regular expressions that rewrite to a set of
1683 * query strings. The new permastruct is added to the WP_Rewrite::$extra_permastructs array.
1685 * When the rewrite rules are built by WP_Rewrite::rewrite_rules(), all of these extra
1686 * permastructs are passed to WP_Rewrite::generate_rewrite_rules() which transforms them
1687 * into the regular expressions that many love to hate.
1689 * The `$args` parameter gives you control over how WP_Rewrite::generate_rewrite_rules()
1690 * works on the new permastruct.
1695 * @param string $name Name for permalink structure.
1696 * @param string $struct Permalink structure (e.g. category/%category%)
1697 * @param array $args {
1698 * Optional. Arguments for building rewrite rules based on the permalink structure.
1699 * Default empty array.
1701 * @type bool $with_front Whether the structure should be prepended with `WP_Rewrite::$front`.
1703 * @type int $ep_mask The endpoint mask defining which endpoints are added to the structure.
1704 * Accepts `EP_NONE`, `EP_PERMALINK`, `EP_ATTACHMENT`, `EP_DATE`, `EP_YEAR`,
1705 * `EP_MONTH`, `EP_DAY`, `EP_ROOT`, `EP_COMMENTS`, `EP_SEARCH`, `EP_CATEGORIES`,
1706 * `EP_TAGS`, `EP_AUTHORS`, `EP_PAGES`, `EP_ALL_ARCHIVES`, and `EP_ALL`.
1707 * Default `EP_NONE`.
1708 * @type bool $paged Whether archive pagination rules should be added for the structure.
1710 * @type bool $feed Whether feed rewrite rules should be added for the structure. Default true.
1711 * @type bool $forcomments Whether the feed rules should be a query for a comments feed. Default false.
1712 * @type bool $walk_dirs Whether the 'directories' making up the structure should be walked over
1713 * and rewrite rules built for each in-turn. Default true.
1714 * @type bool $endpoints Whether endpoints should be applied to the generated rules. Default true.
1717 public function add_permastruct( $name, $struct, $args = array() ) {
1718 // Backwards compatibility for the old parameters: $with_front and $ep_mask.
1719 if ( ! is_array( $args ) )
1720 $args = array( 'with_front' => $args );
1721 if ( func_num_args() == 4 )
1722 $args['ep_mask'] = func_get_arg( 3 );
1725 'with_front' => true,
1726 'ep_mask' => EP_NONE,
1729 'forcomments' => false,
1730 'walk_dirs' => true,
1731 'endpoints' => true,
1733 $args = array_intersect_key( $args, $defaults );
1734 $args = wp_parse_args( $args, $defaults );
1736 if ( $args['with_front'] )
1737 $struct = $this->front . $struct;
1739 $struct = $this->root . $struct;
1740 $args['struct'] = $struct;
1742 $this->extra_permastructs[ $name ] = $args;
1746 * Removes rewrite rules and then recreate rewrite rules.
1748 * Calls WP_Rewrite::wp_rewrite_rules() after removing the 'rewrite_rules' option.
1749 * If the function named 'save_mod_rewrite_rules' exists, it will be called.
1754 * @staticvar bool $do_hard_later
1756 * @param bool $hard Whether to update .htaccess (hard flush) or just update rewrite_rules option (soft flush). Default is true (hard).
1758 public function flush_rules( $hard = true ) {
1759 static $do_hard_later = null;
1761 // Prevent this action from running before everyone has registered their rewrites.
1762 if ( ! did_action( 'wp_loaded' ) ) {
1763 add_action( 'wp_loaded', array( $this, 'flush_rules' ) );
1764 $do_hard_later = ( isset( $do_hard_later ) ) ? $do_hard_later || $hard : $hard;
1768 if ( isset( $do_hard_later ) ) {
1769 $hard = $do_hard_later;
1770 unset( $do_hard_later );
1773 delete_option('rewrite_rules');
1774 $this->wp_rewrite_rules();
1777 * Filter whether a "hard" rewrite rule flush should be performed when requested.
1779 * A "hard" flush updates .htaccess (Apache) or web.config (IIS).
1783 * @param bool $hard Whether to flush rewrite rules "hard". Default true.
1785 if ( ! $hard || ! apply_filters( 'flush_rewrite_rules_hard', true ) ) {
1788 if ( function_exists( 'save_mod_rewrite_rules' ) )
1789 save_mod_rewrite_rules();
1790 if ( function_exists( 'iis7_save_url_rewrite_rules' ) )
1791 iis7_save_url_rewrite_rules();
1795 * Sets up the object's properties.
1797 * The 'use_verbose_page_rules' object property will be set to true if the
1798 * permalink structure begins with one of the following: '%postname%', '%category%',
1799 * '%tag%', or '%author%'.
1804 public function init() {
1805 $this->extra_rules = $this->non_wp_rules = $this->endpoints = array();
1806 $this->permalink_structure = get_option('permalink_structure');
1807 $this->front = substr($this->permalink_structure, 0, strpos($this->permalink_structure, '%'));
1810 if ( $this->using_index_permalinks() )
1811 $this->root = $this->index . '/';
1813 unset($this->author_structure);
1814 unset($this->date_structure);
1815 unset($this->page_structure);
1816 unset($this->search_structure);
1817 unset($this->feed_structure);
1818 unset($this->comment_feed_structure);
1819 $this->use_trailing_slashes = ( '/' == substr($this->permalink_structure, -1, 1) );
1821 // Enable generic rules for pages if permalink structure doesn't begin with a wildcard.
1822 if ( preg_match("/^[^%]*%(?:postname|category|tag|author)%/", $this->permalink_structure) )
1823 $this->use_verbose_page_rules = true;
1825 $this->use_verbose_page_rules = false;
1829 * Sets the main permalink structure for the site.
1831 * Will update the 'permalink_structure' option, if there is a difference
1832 * between the current permalink structure and the parameter value. Calls
1833 * WP_Rewrite::init() after the option is updated.
1835 * Fires the 'permalink_structure_changed' action once the init call has
1836 * processed passing the old and new values
1841 * @param string $permalink_structure Permalink structure.
1843 public function set_permalink_structure($permalink_structure) {
1844 if ( $permalink_structure != $this->permalink_structure ) {
1845 $old_permalink_structure = $this->permalink_structure;
1846 update_option('permalink_structure', $permalink_structure);
1851 * Fires after the permalink structure is updated.
1855 * @param string $old_permalink_structure The previous permalink structure.
1856 * @param string $permalink_structure The new permalink structure.
1858 do_action( 'permalink_structure_changed', $old_permalink_structure, $permalink_structure );
1863 * Sets the category base for the category permalink.
1865 * Will update the 'category_base' option, if there is a difference between
1866 * the current category base and the parameter value. Calls WP_Rewrite::init()
1867 * after the option is updated.
1872 * @param string $category_base Category permalink structure base.
1874 public function set_category_base($category_base) {
1875 if ( $category_base != get_option('category_base') ) {
1876 update_option('category_base', $category_base);
1882 * Sets the tag base for the tag permalink.
1884 * Will update the 'tag_base' option, if there is a difference between the
1885 * current tag base and the parameter value. Calls WP_Rewrite::init() after
1886 * the option is updated.
1891 * @param string $tag_base Tag permalink structure base.
1893 public function set_tag_base( $tag_base ) {
1894 if ( $tag_base != get_option( 'tag_base') ) {
1895 update_option( 'tag_base', $tag_base );
1901 * Constructor - Calls init(), which runs setup.
1907 public function __construct() {