3 * WordPress Rewrite API
10 * Add a straight rewrite rule.
12 * @see WP_Rewrite::add_rule() for long description.
15 * @param string $regex Regular Expression to match request against.
16 * @param string $redirect Page to redirect to.
17 * @param string $after Optional, default is 'bottom'. Where to add rule, can also be 'top'.
19 function add_rewrite_rule($regex, $redirect, $after = 'bottom') {
21 $wp_rewrite->add_rule($regex, $redirect, $after);
25 * Add a new tag (like %postname%).
27 * Warning: you must call this on init or earlier, otherwise the query var
28 * addition stuff won't work.
32 * @param string $tagname
33 * @param string $regex
35 function add_rewrite_tag($tagname, $regex) {
37 if ( strlen($tagname) < 3 || $tagname[0] != '%' || $tagname[strlen($tagname)-1] != '%' )
40 $qv = trim($tagname, '%');
42 global $wp_rewrite, $wp;
43 $wp->add_query_var($qv);
44 $wp_rewrite->add_rewrite_tag($tagname, $regex, $qv . '=');
48 * Add permalink structure.
50 * @see WP_Rewrite::add_permastruct()
53 * @param string $name Name for permalink structure.
54 * @param string $struct Permalink structure.
55 * @param bool $with_front Prepend front base to permalink structure.
57 function add_permastruct( $name, $struct, $with_front = true, $ep_mask = EP_NONE ) {
59 return $wp_rewrite->add_permastruct( $name, $struct, $with_front, $ep_mask );
63 * Add a new feed type like /atom1/.
67 * @param string $feedname
68 * @param callback $function Callback to run on feed display.
69 * @return string Feed action name.
71 function add_feed($feedname, $function) {
73 if ( ! in_array($feedname, $wp_rewrite->feeds) ) //override the file if it is
74 $wp_rewrite->feeds[] = $feedname;
75 $hook = 'do_feed_' . $feedname;
76 // Remove default function hook
77 remove_action($hook, $hook, 10, 1);
78 add_action($hook, $function, 10, 1);
83 * Remove rewrite rules and then recreate rewrite rules.
85 * @see WP_Rewrite::flush_rules()
88 * @param bool $hard Whether to update .htaccess (hard flush) or just update
89 * rewrite_rules transient (soft flush). Default is true (hard).
91 function flush_rewrite_rules( $hard = true ) {
93 $wp_rewrite->flush_rules( $hard );
98 * Endpoint Mask for default, which is nothing.
102 define('EP_NONE', 0);
105 * Endpoint Mask for Permalink.
109 define('EP_PERMALINK', 1);
112 * Endpoint Mask for Attachment.
116 define('EP_ATTACHMENT', 2);
119 * Endpoint Mask for date.
123 define('EP_DATE', 4);
126 * Endpoint Mask for year
130 define('EP_YEAR', 8);
133 * Endpoint Mask for month.
137 define('EP_MONTH', 16);
140 * Endpoint Mask for day.
144 define('EP_DAY', 32);
147 * Endpoint Mask for root.
151 define('EP_ROOT', 64);
154 * Endpoint Mask for comments.
158 define('EP_COMMENTS', 128);
161 * Endpoint Mask for searches.
165 define('EP_SEARCH', 256);
168 * Endpoint Mask for categories.
172 define('EP_CATEGORIES', 512);
175 * Endpoint Mask for tags.
179 define('EP_TAGS', 1024);
182 * Endpoint Mask for authors.
186 define('EP_AUTHORS', 2048);
189 * Endpoint Mask for pages.
193 define('EP_PAGES', 4096);
196 * Endpoint Mask for everything.
200 define('EP_ALL', 8191);
203 * Add an endpoint, like /trackback/.
205 * The endpoints are added to the end of the request. So a request matching
206 * "/2008/10/14/my_post/myep/", the endpoint will be "/myep/".
208 * Be sure to flush the rewrite rules (wp_rewrite->flush_rules()) when your plugin gets
209 * activated (register_activation_hook()) and deactivated (register_deactivation_hook())
212 * @see WP_Rewrite::add_endpoint() Parameters and more description.
215 * @param unknown_type $name
216 * @param unknown_type $places
218 function add_rewrite_endpoint($name, $places) {
220 $wp_rewrite->add_endpoint($name, $places);
224 * Filter the URL base for taxonomies.
226 * To remove any manually prepended /index.php/.
231 * @param string $base The taxonomy base that we're going to filter
234 function _wp_filter_taxonomy_base( $base ) {
235 if ( !empty( $base ) ) {
236 $base = preg_replace( '|^/index\.php/|', '', $base );
237 $base = trim( $base, '/' );
243 * Examine a url and try to determine the post ID it represents.
245 * Checks are supposedly from the hosted site blog.
249 * @param string $url Permalink to check.
250 * @return int Post ID, or 0 on failure.
252 function url_to_postid($url) {
255 $url = apply_filters('url_to_postid', $url);
257 // First, check to see if there is a 'p=N' or 'page_id=N' to match against
258 if ( preg_match('#[?&](p|page_id|attachment_id)=(\d+)#', $url, $values) ) {
259 $id = absint($values[2]);
264 // Check to see if we are using rewrite rules
265 $rewrite = $wp_rewrite->wp_rewrite_rules();
267 // Not using rewrite rules, and 'p=N' and 'page_id=N' methods failed, so we're out of options
268 if ( empty($rewrite) )
271 // Get rid of the #anchor
272 $url_split = explode('#', $url);
273 $url = $url_split[0];
275 // Get rid of URL ?query=string
276 $url_split = explode('?', $url);
277 $url = $url_split[0];
279 // Add 'www.' if it is absent and should be there
280 if ( false !== strpos(home_url(), '://www.') && false === strpos($url, '://www.') )
281 $url = str_replace('://', '://www.', $url);
283 // Strip 'www.' if it is present and shouldn't be
284 if ( false === strpos(home_url(), '://www.') )
285 $url = str_replace('://www.', '://', $url);
287 // Strip 'index.php/' if we're not using path info permalinks
288 if ( !$wp_rewrite->using_index_permalinks() )
289 $url = str_replace('index.php/', '', $url);
291 if ( false !== strpos($url, home_url()) ) {
292 // Chop off http://domain.com
293 $url = str_replace(home_url(), '', $url);
295 // Chop off /path/to/blog
296 $home_path = parse_url(home_url());
297 $home_path = isset( $home_path['path'] ) ? $home_path['path'] : '' ;
298 $url = str_replace($home_path, '', $url);
301 // Trim leading and lagging slashes
302 $url = trim($url, '/');
307 $request_match = $request;
308 foreach ( (array)$rewrite as $match => $query) {
309 // If the requesting file is the anchor of the match, prepend it
311 if ( !empty($url) && ($url != $request) && (strpos($match, $url) === 0) )
312 $request_match = $url . '/' . $request;
314 if ( preg_match("!^$match!", $request_match, $matches) ) {
316 // Trim the query of everything up to the '?'.
317 $query = preg_replace("!^.+\?!", '', $query);
319 // Substitute the substring matches into the query.
320 $query = addslashes(WP_MatchesMapRegex::apply($query, $matches));
322 // Filter out non-public query vars
324 parse_str($query, $query_vars);
326 foreach ( (array) $query_vars as $key => $value ) {
327 if ( in_array($key, $wp->public_query_vars) )
328 $query[$key] = $value;
332 $query = new WP_Query($query);
333 if ( !empty($query->posts) && $query->is_singular )
334 return $query->post->ID;
343 * WordPress Rewrite Component.
345 * The WordPress Rewrite class writes the rewrite module rules to the .htaccess
346 * file. It also handles parsing the request to get the correct setup for the
347 * WordPress Query class.
349 * The Rewrite along with WP class function as a front controller for WordPress.
350 * You can add rules to trigger your page view and processing using this
351 * component. The full functionality of a front controller does not exist,
352 * meaning you can't define how the template files load based on the rewrite
359 * Default permalink structure for WordPress.
365 var $permalink_structure;
368 * Whether to add trailing slashes.
374 var $use_trailing_slashes;
377 * Permalink author request base ( example.com/author/authorname ).
383 var $author_base = 'author';
386 * Permalink request structure for author pages.
392 var $author_structure;
395 * Permalink request structure for dates.
404 * Permalink request structure for pages.
413 * Search permalink base ( example.com/search/query ).
419 var $search_base = 'search';
422 * Permalink request structure for searches.
428 var $search_structure;
431 * Comments permalink base.
437 var $comments_base = 'comments';
440 * Pagination permalink base.
446 var $pagination_base = 'page';
449 * Feed permalink base.
455 var $feed_base = 'feed';
458 * Comments feed request structure permalink.
464 var $comments_feed_structure;
467 * Feed request structure permalink.
478 * The difference between the root property is that WordPress might be
479 * located at example/WordPress/index.php, if permalinks are turned off. The
480 * WordPress/index.php will be the front portion. If permalinks are turned
481 * on, this will most likely be empty or not set.
490 * Root URL path to WordPress (without domain).
492 * The difference between front property is that WordPress might be located
493 * at example.com/WordPress/. The root is the 'WordPress/' portion.
502 * Permalink to the home page.
508 var $index = 'index.php';
511 * Request match string.
520 * Rewrite rules to match against the request to find the redirect or query.
529 * Additional rules added external to the rewrite class.
531 * Those not generated by the class, see add_rewrite_rule().
537 var $extra_rules = array(); //
540 * Additional rules that belong at the beginning to match first.
542 * Those not generated by the class, see add_rewrite_rule().
548 var $extra_rules_top = array(); //
551 * Rules that don't redirect to WP's index.php.
553 * These rules are written to the mod_rewrite portion of the .htaccess.
559 var $non_wp_rules = array(); //
562 * Extra permalink structures.
568 var $extra_permastructs = array();
571 * Endpoints permalinks
580 * Whether to write every mod_rewrite rule for WordPress.
582 * This is off by default, turning it on might print a lot of rewrite rules
583 * to the .htaccess file.
589 var $use_verbose_rules = false;
592 * Whether to write every mod_rewrite rule for WordPress pages.
598 var $use_verbose_page_rules = true;
601 * Permalink structure search for preg_replace.
623 * Preg_replace values for the search, see {@link WP_Rewrite::$rewritecode}.
629 var $rewritereplace =
645 * Search for the query to look for replacing.
667 * Supported default feeds.
673 var $feeds = array ( 'feed', 'rdf', 'rss', 'rss2', 'atom' );
676 * Whether permalinks are being used.
678 * This can be either rewrite module or permalink in the HTTP query string.
683 * @return bool True, if permalinks are enabled.
685 function using_permalinks() {
686 return ! empty($this->permalink_structure);
690 * Whether permalinks are being used and rewrite module is not enabled.
692 * Means that permalink links are enabled and index.php is in the URL.
699 function using_index_permalinks() {
700 if ( empty($this->permalink_structure) )
703 // If the index is not in the permalink, we're using mod_rewrite.
704 if ( preg_match('#^/*' . $this->index . '#', $this->permalink_structure) )
711 * Whether permalinks are being used and rewrite module is enabled.
713 * Using permalinks and index.php is not in the URL.
720 function using_mod_rewrite_permalinks() {
721 if ( $this->using_permalinks() && ! $this->using_index_permalinks() )
728 * Index for matches for usage in preg_*() functions.
730 * The format of the string is, with empty matches property value, '$NUM'.
731 * The 'NUM' will be replaced with the value in the $number parameter. With
732 * the matches property not empty, the value of the returned string will
733 * contain that value of the matches property. The format then will be
734 * '$MATCHES[NUM]', with MATCHES as the value in the property and NUM the
735 * value of the $number parameter.
740 * @param int $number Index number.
743 function preg_index($number) {
747 if ( ! empty($this->matches) ) {
748 $match_prefix = '$' . $this->matches . '[';
752 return "$match_prefix$number$match_suffix";
756 * Retrieve all page and attachments for pages URIs.
758 * The attachments are for those that have pages as parents and will be
764 * @return array Array of page URIs as first element and attachment URIs as second element.
766 function page_uri_index() {
769 //get pages in order of hierarchy, i.e. children after parents
770 $posts = get_page_hierarchy($wpdb->get_results("SELECT ID, post_name, post_parent FROM $wpdb->posts WHERE post_type = 'page'"));
772 // If we have no pages get out quick
774 return array( array(), array() );
776 //now reverse it, because we need parents after children for rewrite rules to work properly
777 $posts = array_reverse($posts, true);
779 $page_uris = array();
780 $page_attachment_uris = array();
782 foreach ( $posts as $id => $post ) {
784 $uri = get_page_uri($id);
785 $attachments = $wpdb->get_results( $wpdb->prepare( "SELECT ID, post_name, post_parent FROM $wpdb->posts WHERE post_type = 'attachment' AND post_parent = %d", $id ));
786 if ( !empty($attachments) ) {
787 foreach ( $attachments as $attachment ) {
788 $attach_uri = get_page_uri($attachment->ID);
789 $page_attachment_uris[$attach_uri] = $attachment->ID;
793 $page_uris[$uri] = $id;
796 return array( $page_uris, $page_attachment_uris );
800 * Retrieve all of the rewrite rules for pages.
802 * If the 'use_verbose_page_rules' property is false, then there will only
803 * be a single rewrite rule for pages for those matching '%pagename%'. With
804 * the property set to true, the attachments and the pages will be added for
805 * each individual attachment URI and page URI, respectively.
812 function page_rewrite_rules() {
813 $rewrite_rules = array();
814 $page_structure = $this->get_page_permastruct();
816 if ( ! $this->use_verbose_page_rules ) {
817 $this->add_rewrite_tag('%pagename%', "(.+?)", 'pagename=');
818 $rewrite_rules = array_merge($rewrite_rules, $this->generate_rewrite_rules($page_structure, EP_PAGES));
819 return $rewrite_rules;
822 $page_uris = $this->page_uri_index();
823 $uris = $page_uris[0];
824 $attachment_uris = $page_uris[1];
826 if ( is_array( $attachment_uris ) ) {
827 foreach ( $attachment_uris as $uri => $pagename ) {
828 $this->add_rewrite_tag('%pagename%', "($uri)", 'attachment=');
829 $rewrite_rules = array_merge($rewrite_rules, $this->generate_rewrite_rules($page_structure, EP_PAGES));
832 if ( is_array( $uris ) ) {
833 foreach ( $uris as $uri => $pagename ) {
834 $this->add_rewrite_tag('%pagename%', "($uri)", 'pagename=');
835 $rewrite_rules = array_merge($rewrite_rules, $this->generate_rewrite_rules($page_structure, EP_PAGES));
839 return $rewrite_rules;
843 * Retrieve date permalink structure, with year, month, and day.
845 * The permalink structure for the date, if not set already depends on the
846 * permalink structure. It can be one of three formats. The first is year,
847 * month, day; the second is day, month, year; and the last format is month,
848 * day, year. These are matched against the permalink structure for which
849 * one is used. If none matches, then the default will be used, which is
852 * Prevents post ID and date permalinks from overlapping. In the case of
853 * post_id, the date permalink will be prepended with front permalink with
854 * 'date/' before the actual permalink to form the complete date permalink
860 * @return bool|string False on no permalink structure. Date permalink structure.
862 function get_date_permastruct() {
863 if ( isset($this->date_structure) )
864 return $this->date_structure;
866 if ( empty($this->permalink_structure) ) {
867 $this->date_structure = '';
871 // The date permalink must have year, month, and day separated by slashes.
872 $endians = array('%year%/%monthnum%/%day%', '%day%/%monthnum%/%year%', '%monthnum%/%day%/%year%');
874 $this->date_structure = '';
877 foreach ( $endians as $endian ) {
878 if ( false !== strpos($this->permalink_structure, $endian) ) {
879 $date_endian= $endian;
884 if ( empty($date_endian) )
885 $date_endian = '%year%/%monthnum%/%day%';
887 // Do not allow the date tags and %post_id% to overlap in the permalink
888 // structure. If they do, move the date tags to $front/date/.
889 $front = $this->front;
890 preg_match_all('/%.+?%/', $this->permalink_structure, $tokens);
892 foreach ( (array) $tokens[0] as $token) {
893 if ( '%post_id%' == $token && ($tok_index <= 3) ) {
894 $front = $front . 'date/';
900 $this->date_structure = $front . $date_endian;
902 return $this->date_structure;
906 * Retrieve the year permalink structure without month and day.
908 * Gets the date permalink structure and strips out the month and day
909 * permalink structures.
914 * @return bool|string False on failure. Year structure on success.
916 function get_year_permastruct() {
917 $structure = $this->get_date_permastruct($this->permalink_structure);
919 if ( empty($structure) )
922 $structure = str_replace('%monthnum%', '', $structure);
923 $structure = str_replace('%day%', '', $structure);
925 $structure = preg_replace('#/+#', '/', $structure);
931 * Retrieve the month permalink structure without day and with year.
933 * Gets the date permalink structure and strips out the day permalink
934 * structures. Keeps the year permalink structure.
939 * @return bool|string False on failure. Year/Month structure on success.
941 function get_month_permastruct() {
942 $structure = $this->get_date_permastruct($this->permalink_structure);
944 if ( empty($structure) )
947 $structure = str_replace('%day%', '', $structure);
949 $structure = preg_replace('#/+#', '/', $structure);
955 * Retrieve the day permalink structure with month and year.
957 * Keeps date permalink structure with all year, month, and day.
962 * @return bool|string False on failure. Year/Month/Day structure on success.
964 function get_day_permastruct() {
965 return $this->get_date_permastruct($this->permalink_structure);
969 * Retrieve the permalink structure for categories.
971 * If the category_base property has no value, then the category structure
972 * will have the front property value, followed by 'category', and finally
973 * '%category%'. If it does, then the root property will be used, along with
974 * the category_base property value.
979 * @return bool|string False on failure. Category permalink structure.
981 function get_category_permastruct() {
982 return $this->get_extra_permastruct('category');
986 * Retrieve the permalink structure for tags.
988 * If the tag_base property has no value, then the tag structure will have
989 * the front property value, followed by 'tag', and finally '%tag%'. If it
990 * does, then the root property will be used, along with the tag_base
996 * @return bool|string False on failure. Tag permalink structure.
998 function get_tag_permastruct() {
999 return $this->get_extra_permastruct('post_tag');
1003 * Retrieve extra permalink structure by name.
1008 * @param string $name Permalink structure name.
1009 * @return string|bool False if not found. Permalink structure string.
1011 function get_extra_permastruct($name) {
1012 if ( empty($this->permalink_structure) )
1015 if ( isset($this->extra_permastructs[$name]) )
1016 return $this->extra_permastructs[$name][0];
1022 * Retrieve the author permalink structure.
1024 * The permalink structure is front property, author base, and finally
1025 * '/%author%'. Will set the author_structure property and then return it
1026 * without attempting to set the value again.
1031 * @return string|bool False if not found. Permalink structure string.
1033 function get_author_permastruct() {
1034 if ( isset($this->author_structure) )
1035 return $this->author_structure;
1037 if ( empty($this->permalink_structure) ) {
1038 $this->author_structure = '';
1042 $this->author_structure = $this->front . $this->author_base . '/%author%';
1044 return $this->author_structure;
1048 * Retrieve the search permalink structure.
1050 * The permalink structure is root property, search base, and finally
1051 * '/%search%'. Will set the search_structure property and then return it
1052 * without attempting to set the value again.
1057 * @return string|bool False if not found. Permalink structure string.
1059 function get_search_permastruct() {
1060 if ( isset($this->search_structure) )
1061 return $this->search_structure;
1063 if ( empty($this->permalink_structure) ) {
1064 $this->search_structure = '';
1068 $this->search_structure = $this->root . $this->search_base . '/%search%';
1070 return $this->search_structure;
1074 * Retrieve the page permalink structure.
1076 * The permalink structure is root property, and '%pagename%'. Will set the
1077 * page_structure property and then return it without attempting to set the
1083 * @return string|bool False if not found. Permalink structure string.
1085 function get_page_permastruct() {
1086 if ( isset($this->page_structure) )
1087 return $this->page_structure;
1089 if (empty($this->permalink_structure)) {
1090 $this->page_structure = '';
1094 $this->page_structure = $this->root . '%pagename%';
1096 return $this->page_structure;
1100 * Retrieve the feed permalink structure.
1102 * The permalink structure is root property, feed base, and finally
1103 * '/%feed%'. Will set the feed_structure property and then return it
1104 * without attempting to set the value again.
1109 * @return string|bool False if not found. Permalink structure string.
1111 function get_feed_permastruct() {
1112 if ( isset($this->feed_structure) )
1113 return $this->feed_structure;
1115 if ( empty($this->permalink_structure) ) {
1116 $this->feed_structure = '';
1120 $this->feed_structure = $this->root . $this->feed_base . '/%feed%';
1122 return $this->feed_structure;
1126 * Retrieve the comment feed permalink structure.
1128 * The permalink structure is root property, comment base property, feed
1129 * base and finally '/%feed%'. Will set the comment_feed_structure property
1130 * and then return it without attempting to set the value again.
1135 * @return string|bool False if not found. Permalink structure string.
1137 function get_comment_feed_permastruct() {
1138 if ( isset($this->comment_feed_structure) )
1139 return $this->comment_feed_structure;
1141 if (empty($this->permalink_structure)) {
1142 $this->comment_feed_structure = '';
1146 $this->comment_feed_structure = $this->root . $this->comments_base . '/' . $this->feed_base . '/%feed%';
1148 return $this->comment_feed_structure;
1152 * Append or update tag, pattern, and query for replacement.
1154 * If the tag already exists, replace the existing pattern and query for
1155 * that tag, otherwise add the new tag, pattern, and query to the end of the
1158 * @internal What is the purpose of this function again? Need to finish long
1164 * @param string $tag Append tag to rewritecode property array.
1165 * @param string $pattern Append pattern to rewritereplace property array.
1166 * @param string $query Append query to queryreplace property array.
1168 function add_rewrite_tag($tag, $pattern, $query) {
1169 $position = array_search($tag, $this->rewritecode);
1170 if ( false !== $position && null !== $position ) {
1171 $this->rewritereplace[$position] = $pattern;
1172 $this->queryreplace[$position] = $query;
1174 $this->rewritecode[] = $tag;
1175 $this->rewritereplace[] = $pattern;
1176 $this->queryreplace[] = $query;
1181 * Generate the rules from permalink structure.
1183 * The main WP_Rewrite function for building the rewrite rule list. The
1184 * contents of the function is a mix of black magic and regular expressions,
1185 * so best just ignore the contents and move to the parameters.
1190 * @param string $permalink_structure The permalink structure.
1191 * @param int $ep_mask Optional, default is EP_NONE. Endpoint constant, see EP_* constants.
1192 * @param bool $paged Optional, default is true. Whether permalink request is paged.
1193 * @param bool $feed Optional, default is true. Whether for feed.
1194 * @param bool $forcomments Optional, default is false. Whether for comments.
1195 * @param bool $walk_dirs Optional, default is true. Whether to create list of directories to walk over.
1196 * @param bool $endpoints Optional, default is true. Whether endpoints are enabled.
1197 * @return array Rewrite rule list.
1199 function generate_rewrite_rules($permalink_structure, $ep_mask = EP_NONE, $paged = true, $feed = true, $forcomments = false, $walk_dirs = true, $endpoints = true) {
1200 //build a regex to match the feed section of URLs, something like (feed|atom|rss|rss2)/?
1202 foreach ( (array) $this->feeds as $feed_name)
1203 $feedregex2 .= $feed_name . '|';
1204 $feedregex2 = '(' . trim($feedregex2, '|') . ')/?$';
1206 //$feedregex is identical but with /feed/ added on as well, so URLs like <permalink>/feed/atom
1207 //and <permalink>/atom are both possible
1208 $feedregex = $this->feed_base . '/' . $feedregex2;
1210 //build a regex to match the trackback and page/xx parts of URLs
1211 $trackbackregex = 'trackback/?$';
1212 $pageregex = $this->pagination_base . '/?([0-9]{1,})/?$';
1213 $commentregex = 'comment-page-([0-9]{1,})/?$';
1215 //build up an array of endpoint regexes to append => queries to append
1217 $ep_query_append = array ();
1218 foreach ( (array) $this->endpoints as $endpoint) {
1219 //match everything after the endpoint name, but allow for nothing to appear there
1220 $epmatch = $endpoint[1] . '(/(.*))?/?$';
1221 //this will be appended on to the rest of the query for each dir
1222 $epquery = '&' . $endpoint[1] . '=';
1223 $ep_query_append[$epmatch] = array ( $endpoint[0], $epquery );
1227 //get everything up to the first rewrite tag
1228 $front = substr($permalink_structure, 0, strpos($permalink_structure, '%'));
1229 //build an array of the tags (note that said array ends up being in $tokens[0])
1230 preg_match_all('/%.+?%/', $permalink_structure, $tokens);
1232 $num_tokens = count($tokens[0]);
1234 $index = $this->index; //probably 'index.php'
1235 $feedindex = $index;
1236 $trackbackindex = $index;
1237 //build a list from the rewritecode and queryreplace arrays, that will look something like
1238 //tagname=$matches[i] where i is the current $i
1239 for ( $i = 0; $i < $num_tokens; ++$i ) {
1241 $queries[$i] = $queries[$i - 1] . '&';
1245 $query_token = str_replace($this->rewritecode, $this->queryreplace, $tokens[0][$i]) . $this->preg_index($i+1);
1246 $queries[$i] .= $query_token;
1249 //get the structure, minus any cruft (stuff that isn't tags) at the front
1250 $structure = $permalink_structure;
1251 if ( $front != '/' )
1252 $structure = str_replace($front, '', $structure);
1254 //create a list of dirs to walk over, making rewrite rules for each level
1255 //so for example, a $structure of /%year%/%month%/%postname% would create
1256 //rewrite rules for /%year%/, /%year%/%month%/ and /%year%/%month%/%postname%
1257 $structure = trim($structure, '/');
1258 $dirs = $walk_dirs ? explode('/', $structure) : array( $structure );
1259 $num_dirs = count($dirs);
1261 //strip slashes from the front of $front
1262 $front = preg_replace('|^/+|', '', $front);
1264 //the main workhorse loop
1265 $post_rewrite = array();
1267 for ( $j = 0; $j < $num_dirs; ++$j ) {
1268 //get the struct for this dir, and trim slashes off the front
1269 $struct .= $dirs[$j] . '/'; //accumulate. see comment near explode('/', $structure) above
1270 $struct = ltrim($struct, '/');
1272 //replace tags with regexes
1273 $match = str_replace($this->rewritecode, $this->rewritereplace, $struct);
1275 //make a list of tags, and store how many there are in $num_toks
1276 $num_toks = preg_match_all('/%.+?%/', $struct, $toks);
1278 //get the 'tagname=$matches[i]'
1279 $query = ( isset($queries) && is_array($queries) ) ? $queries[$num_toks - 1] : '';
1281 //set up $ep_mask_specific which is used to match more specific URL types
1282 switch ( $dirs[$j] ) {
1284 $ep_mask_specific = EP_YEAR;
1287 $ep_mask_specific = EP_MONTH;
1290 $ep_mask_specific = EP_DAY;
1293 $ep_mask_specific = EP_NONE;
1296 //create query for /page/xx
1297 $pagematch = $match . $pageregex;
1298 $pagequery = $index . '?' . $query . '&paged=' . $this->preg_index($num_toks + 1);
1300 //create query for /comment-page-xx
1301 $commentmatch = $match . $commentregex;
1302 $commentquery = $index . '?' . $query . '&cpage=' . $this->preg_index($num_toks + 1);
1304 if ( get_option('page_on_front') ) {
1305 //create query for Root /comment-page-xx
1306 $rootcommentmatch = $match . $commentregex;
1307 $rootcommentquery = $index . '?' . $query . '&page_id=' . get_option('page_on_front') . '&cpage=' . $this->preg_index($num_toks + 1);
1310 //create query for /feed/(feed|atom|rss|rss2|rdf)
1311 $feedmatch = $match . $feedregex;
1312 $feedquery = $feedindex . '?' . $query . '&feed=' . $this->preg_index($num_toks + 1);
1314 //create query for /(feed|atom|rss|rss2|rdf) (see comment near creation of $feedregex)
1315 $feedmatch2 = $match . $feedregex2;
1316 $feedquery2 = $feedindex . '?' . $query . '&feed=' . $this->preg_index($num_toks + 1);
1318 //if asked to, turn the feed queries into comment feed ones
1319 if ( $forcomments ) {
1320 $feedquery .= '&withcomments=1';
1321 $feedquery2 .= '&withcomments=1';
1324 //start creating the array of rewrites for this dir
1326 if ( $feed ) //...adding on /feed/ regexes => queries
1327 $rewrite = array($feedmatch => $feedquery, $feedmatch2 => $feedquery2);
1328 if ( $paged ) //...and /page/xx ones
1329 $rewrite = array_merge($rewrite, array($pagematch => $pagequery));
1331 //only on pages with comments add ../comment-page-xx/
1332 if ( EP_PAGES & $ep_mask || EP_PERMALINK & $ep_mask )
1333 $rewrite = array_merge($rewrite, array($commentmatch => $commentquery));
1334 else if ( EP_ROOT & $ep_mask && get_option('page_on_front') )
1335 $rewrite = array_merge($rewrite, array($rootcommentmatch => $rootcommentquery));
1339 foreach ( (array) $ep_query_append as $regex => $ep) {
1340 //add the endpoints on if the mask fits
1341 if ( $ep[0] & $ep_mask || $ep[0] & $ep_mask_specific )
1342 $rewrite[$match . $regex] = $index . '?' . $query . $ep[1] . $this->preg_index($num_toks + 2);
1346 //if we've got some tags in this dir
1351 //check to see if this dir is permalink-level: i.e. the structure specifies an
1352 //individual post. Do this by checking it contains at least one of 1) post name,
1353 //2) post ID, 3) page name, 4) timestamp (year, month, day, hour, second and
1354 //minute all present). Set these flags now as we need them for the endpoints.
1355 if ( strpos($struct, '%postname%') !== false
1356 || strpos($struct, '%post_id%') !== false
1357 || strpos($struct, '%pagename%') !== false
1358 || (strpos($struct, '%year%') !== false && strpos($struct, '%monthnum%') !== false && strpos($struct, '%day%') !== false && strpos($struct, '%hour%') !== false && strpos($struct, '%minute%') !== false && strpos($struct, '%second%') !== false)
1361 if ( strpos($struct, '%pagename%') !== false )
1366 // For custom post types, we need to add on endpoints as well.
1367 foreach ( get_post_types( array('_builtin' => false ) ) as $ptype ) {
1368 if ( strpos($struct, "%$ptype%") !== false ) {
1370 $page = is_post_type_hierarchical( $ptype ); // This is for page style attachment url's
1376 //if we're creating rules for a permalink, do all the endpoints like attachments etc
1378 //create query and regex for trackback
1379 $trackbackmatch = $match . $trackbackregex;
1380 $trackbackquery = $trackbackindex . '?' . $query . '&tb=1';
1381 //trim slashes from the end of the regex for this dir
1382 $match = rtrim($match, '/');
1383 //get rid of brackets
1384 $submatchbase = str_replace( array('(', ')'), '', $match);
1386 //add a rule for at attachments, which take the form of <permalink>/some-text
1387 $sub1 = $submatchbase . '/([^/]+)/';
1388 $sub1tb = $sub1 . $trackbackregex; //add trackback regex <permalink>/trackback/...
1389 $sub1feed = $sub1 . $feedregex; //and <permalink>/feed/(atom|...)
1390 $sub1feed2 = $sub1 . $feedregex2; //and <permalink>/(feed|atom...)
1391 $sub1comment = $sub1 . $commentregex; //and <permalink>/comment-page-xx
1392 //add an ? as we don't have to match that last slash, and finally a $ so we
1393 //match to the end of the URL
1395 //add another rule to match attachments in the explicit form:
1396 //<permalink>/attachment/some-text
1397 $sub2 = $submatchbase . '/attachment/([^/]+)/';
1398 $sub2tb = $sub2 . $trackbackregex; //and add trackbacks <permalink>/attachment/trackback
1399 $sub2feed = $sub2 . $feedregex; //feeds, <permalink>/attachment/feed/(atom|...)
1400 $sub2feed2 = $sub2 . $feedregex2; //and feeds again on to this <permalink>/attachment/(feed|atom...)
1401 $sub2comment = $sub2 . $commentregex; //and <permalink>/comment-page-xx
1403 //create queries for these extra tag-ons we've just dealt with
1404 $subquery = $index . '?attachment=' . $this->preg_index(1);
1405 $subtbquery = $subquery . '&tb=1';
1406 $subfeedquery = $subquery . '&feed=' . $this->preg_index(2);
1407 $subcommentquery = $subquery . '&cpage=' . $this->preg_index(2);
1409 //do endpoints for attachments
1410 if ( !empty($endpoints) ) {
1411 foreach ( (array) $ep_query_append as $regex => $ep ) {
1412 if ( $ep[0] & EP_ATTACHMENT ) {
1413 $rewrite[$sub1 . $regex] = $subquery . $ep[1] . $this->preg_index(2);
1414 $rewrite[$sub2 . $regex] = $subquery . $ep[1] . $this->preg_index(2);
1419 //now we've finished with endpoints, finish off the $sub1 and $sub2 matches
1423 //allow URLs like <permalink>/2 for <permalink>/page/2
1424 $match = $match . '(/[0-9]+)?/?$';
1425 $query = $index . '?' . $query . '&page=' . $this->preg_index($num_toks + 1);
1426 } else { //not matching a permalink so this is a lot simpler
1427 //close the match and finalise the query
1429 $query = $index . '?' . $query;
1432 //create the final array for this dir by joining the $rewrite array (which currently
1433 //only contains rules/queries for trackback, pages etc) to the main regex/query for
1435 $rewrite = array_merge($rewrite, array($match => $query));
1437 //if we're matching a permalink, add those extras (attachments etc) on
1440 $rewrite = array_merge(array($trackbackmatch => $trackbackquery), $rewrite);
1442 //add regexes/queries for attachments, attachment trackbacks and so on
1443 if ( ! $page ) //require <permalink>/attachment/stuff form for pages because of confusion with subpages
1444 $rewrite = array_merge($rewrite, array($sub1 => $subquery, $sub1tb => $subtbquery, $sub1feed => $subfeedquery, $sub1feed2 => $subfeedquery, $sub1comment => $subcommentquery));
1445 $rewrite = array_merge(array($sub2 => $subquery, $sub2tb => $subtbquery, $sub2feed => $subfeedquery, $sub2feed2 => $subfeedquery, $sub2comment => $subcommentquery), $rewrite);
1448 //add the rules for this dir to the accumulating $post_rewrite
1449 $post_rewrite = array_merge($rewrite, $post_rewrite);
1451 return $post_rewrite; //the finished rules. phew!
1455 * Generate Rewrite rules with permalink structure and walking directory only.
1457 * Shorten version of {@link WP_Rewrite::generate_rewrite_rules()} that
1458 * allows for shorter list of parameters. See the method for longer
1459 * description of what generating rewrite rules does.
1461 * @uses WP_Rewrite::generate_rewrite_rules() See for long description and rest of parameters.
1465 * @param string $permalink_structure The permalink structure to generate rules.
1466 * @param bool $walk_dirs Optional, default is false. Whether to create list of directories to walk over.
1469 function generate_rewrite_rule($permalink_structure, $walk_dirs = false) {
1470 return $this->generate_rewrite_rules($permalink_structure, EP_NONE, false, false, false, $walk_dirs);
1474 * Construct rewrite matches and queries from permalink structure.
1476 * Runs the action 'generate_rewrite_rules' with the parameter that is an
1477 * reference to the current WP_Rewrite instance to further manipulate the
1478 * permalink structures and rewrite rules. Runs the 'rewrite_rules_array'
1479 * filter on the full rewrite rule array.
1481 * There are two ways to manipulate the rewrite rules, one by hooking into
1482 * the 'generate_rewrite_rules' action and gaining full control of the
1483 * object or just manipulating the rewrite rule array before it is passed
1484 * from the function.
1489 * @return array An associate array of matches and queries.
1491 function rewrite_rules() {
1494 if ( empty($this->permalink_structure) )
1497 // robots.txt -only if installed at the root
1498 $home_path = parse_url( home_url() );
1499 $robots_rewrite = ( empty( $home_path['path'] ) || '/' == $home_path['path'] ) ? array( 'robots\.txt$' => $this->index . '?robots=1' ) : array();
1501 // Default Feed rules - These are require to allow for the direct access files to work with permalink structure starting with %category%
1502 $default_feeds = array( '.*wp-atom.php$' => $this->index . '?feed=atom',
1503 '.*wp-rdf.php$' => $this->index . '?feed=rdf',
1504 '.*wp-rss.php$' => $this->index . '?feed=rss',
1505 '.*wp-rss2.php$' => $this->index . '?feed=rss2',
1506 '.*wp-feed.php$' => $this->index . '?feed=feed',
1507 '.*wp-commentsrss2.php$' => $this->index . '?feed=rss2&withcomments=1');
1509 // Registration rules
1510 $registration_pages = array();
1511 if ( is_multisite() && is_main_site() ) {
1512 $registration_pages['.*wp-signup.php$'] = $this->index . '?signup=true';
1513 $registration_pages['.*wp-activate.php$'] = $this->index . '?activate=true';
1517 $post_rewrite = $this->generate_rewrite_rules($this->permalink_structure, EP_PERMALINK);
1518 $post_rewrite = apply_filters('post_rewrite_rules', $post_rewrite);
1521 $date_rewrite = $this->generate_rewrite_rules($this->get_date_permastruct(), EP_DATE);
1522 $date_rewrite = apply_filters('date_rewrite_rules', $date_rewrite);
1525 $root_rewrite = $this->generate_rewrite_rules($this->root . '/', EP_ROOT);
1526 $root_rewrite = apply_filters('root_rewrite_rules', $root_rewrite);
1529 $comments_rewrite = $this->generate_rewrite_rules($this->root . $this->comments_base, EP_COMMENTS, true, true, true, false);
1530 $comments_rewrite = apply_filters('comments_rewrite_rules', $comments_rewrite);
1533 $search_structure = $this->get_search_permastruct();
1534 $search_rewrite = $this->generate_rewrite_rules($search_structure, EP_SEARCH);
1535 $search_rewrite = apply_filters('search_rewrite_rules', $search_rewrite);
1538 $author_rewrite = $this->generate_rewrite_rules($this->get_author_permastruct(), EP_AUTHORS);
1539 $author_rewrite = apply_filters('author_rewrite_rules', $author_rewrite);
1542 $page_rewrite = $this->page_rewrite_rules();
1543 $page_rewrite = apply_filters('page_rewrite_rules', $page_rewrite);
1545 // Extra permastructs
1546 foreach ( $this->extra_permastructs as $permastructname => $permastruct ) {
1547 if ( is_array($permastruct) )
1548 $rules = $this->generate_rewrite_rules($permastruct[0], $permastruct[1]);
1550 $rules = $this->generate_rewrite_rules($permastruct, EP_NONE);
1552 $rules = apply_filters($permastructname . '_rewrite_rules', $rules);
1553 if ( 'post_tag' == $permastructname )
1554 $rules = apply_filters('tag_rewrite_rules', $rules);
1556 $this->extra_rules_top = array_merge($this->extra_rules_top, $rules);
1559 // Put them together.
1560 if ( $this->use_verbose_page_rules )
1561 $this->rules = array_merge($this->extra_rules_top, $robots_rewrite, $default_feeds, $registration_pages, $page_rewrite, $root_rewrite, $comments_rewrite, $search_rewrite, $author_rewrite, $date_rewrite, $post_rewrite, $this->extra_rules);
1563 $this->rules = array_merge($this->extra_rules_top, $robots_rewrite, $default_feeds, $registration_pages, $root_rewrite, $comments_rewrite, $search_rewrite, $author_rewrite, $date_rewrite, $post_rewrite, $page_rewrite, $this->extra_rules);
1565 do_action_ref_array('generate_rewrite_rules', array(&$this));
1566 $this->rules = apply_filters('rewrite_rules_array', $this->rules);
1568 return $this->rules;
1572 * Retrieve the rewrite rules.
1574 * The difference between this method and {@link
1575 * WP_Rewrite::rewrite_rules()} is that this method stores the rewrite rules
1576 * in the 'rewrite_rules' option and retrieves it. This prevents having to
1577 * process all of the permalinks to get the rewrite rules in the form of
1583 * @return array Rewrite rules.
1585 function wp_rewrite_rules() {
1586 $this->rules = get_option('rewrite_rules');
1587 if ( empty($this->rules) ) {
1588 $this->matches = 'matches';
1589 $this->rewrite_rules();
1590 update_option('rewrite_rules', $this->rules);
1593 return $this->rules;
1597 * Retrieve mod_rewrite formatted rewrite rules to write to .htaccess.
1599 * Does not actually write to the .htaccess file, but creates the rules for
1600 * the process that will.
1602 * Will add the non_wp_rules property rules to the .htaccess file before
1603 * the WordPress rewrite rules one.
1610 function mod_rewrite_rules() {
1611 if ( ! $this->using_permalinks() )
1614 $site_root = parse_url(get_option('siteurl'));
1615 if ( isset( $site_root['path'] ) )
1616 $site_root = trailingslashit($site_root['path']);
1618 $home_root = parse_url(home_url());
1619 if ( isset( $home_root['path'] ) )
1620 $home_root = trailingslashit($home_root['path']);
1624 $rules = "<IfModule mod_rewrite.c>\n";
1625 $rules .= "RewriteEngine On\n";
1626 $rules .= "RewriteBase $home_root\n";
1627 $rules .= "RewriteRule ^index\.php$ - [L]\n"; // Prevent -f checks on index.php.
1629 //add in the rules that don't redirect to WP's index.php (and thus shouldn't be handled by WP at all)
1630 foreach ( (array) $this->non_wp_rules as $match => $query) {
1631 // Apache 1.3 does not support the reluctant (non-greedy) modifier.
1632 $match = str_replace('.+?', '.+', $match);
1634 // If the match is unanchored and greedy, prepend rewrite conditions
1635 // to avoid infinite redirects and eclipsing of real files.
1636 //if ($match == '(.+)/?$' || $match == '([^/]+)/?$' ) {
1640 $rules .= 'RewriteRule ^' . $match . ' ' . $home_root . $query . " [QSA,L]\n";
1643 if ( $this->use_verbose_rules ) {
1644 $this->matches = '';
1645 $rewrite = $this->rewrite_rules();
1646 $num_rules = count($rewrite);
1647 $rules .= "RewriteCond %{REQUEST_FILENAME} -f [OR]\n" .
1648 "RewriteCond %{REQUEST_FILENAME} -d\n" .
1649 "RewriteRule ^.*$ - [S=$num_rules]\n";
1651 foreach ( (array) $rewrite as $match => $query) {
1652 // Apache 1.3 does not support the reluctant (non-greedy) modifier.
1653 $match = str_replace('.+?', '.+', $match);
1655 // If the match is unanchored and greedy, prepend rewrite conditions
1656 // to avoid infinite redirects and eclipsing of real files.
1657 //if ($match == '(.+)/?$' || $match == '([^/]+)/?$' ) {
1661 if ( strpos($query, $this->index) !== false )
1662 $rules .= 'RewriteRule ^' . $match . ' ' . $home_root . $query . " [QSA,L]\n";
1664 $rules .= 'RewriteRule ^' . $match . ' ' . $site_root . $query . " [QSA,L]\n";
1667 $rules .= "RewriteCond %{REQUEST_FILENAME} !-f\n" .
1668 "RewriteCond %{REQUEST_FILENAME} !-d\n" .
1669 "RewriteRule . {$home_root}{$this->index} [L]\n";
1672 $rules .= "</IfModule>\n";
1674 $rules = apply_filters('mod_rewrite_rules', $rules);
1675 $rules = apply_filters('rewrite_rules', $rules); // Deprecated
1681 * Retrieve IIS7 URL Rewrite formatted rewrite rules to write to web.config file.
1683 * Does not actually write to the web.config file, but creates the rules for
1684 * the process that will.
1691 function iis7_url_rewrite_rules( $add_parent_tags = false ) {
1693 if ( ! $this->using_permalinks() )
1696 if ( $add_parent_tags ) {
1697 $rules .= '<configuration>
1702 if ( !is_multisite() ) {
1704 <rule name="wordpress" patternSyntax="Wildcard">
1707 <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
1708 <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
1710 <action type="Rewrite" url="index.php" />
1713 if (is_subdomain_install()) {
1715 <rule name="wordpress - Rule 1" stopProcessing="true">
1716 <match url="^index\.php$" ignoreCase="false" />
1717 <action type="None" />
1719 <rule name="wordpress - Rule 2" stopProcessing="true">
1720 <match url="^files/(.+)" ignoreCase="false" />
1721 <action type="Rewrite" url="wp-includes/ms-files.php?file={R:1}" appendQueryString="false" />
1723 <rule name="wordpress - Rule 3" stopProcessing="true">
1724 <match url="^" ignoreCase="false" />
1725 <conditions logicalGrouping="MatchAny">
1726 <add input="{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="false" />
1727 <add input="{REQUEST_FILENAME}" matchType="IsDirectory" ignoreCase="false" />
1729 <action type="None" />
1731 <rule name="wordpress - Rule 4" stopProcessing="true">
1732 <match url="." ignoreCase="false" />
1733 <action type="Rewrite" url="index.php" />
1737 <rule name="wordpress - Rule 1" stopProcessing="true">
1738 <match url="^index\.php$" ignoreCase="false" />
1739 <action type="None" />
1741 <rule name="wordpress - Rule 2" stopProcessing="true">
1742 <match url="^([_0-9a-zA-Z-]+/)?files/(.+)" ignoreCase="false" />
1743 <action type="Rewrite" url="wp-includes/ms-files.php?file={R:2}" appendQueryString="false" />
1745 <rule name="wordpress - Rule 3" stopProcessing="true">
1746 <match url="^([_0-9a-zA-Z-]+/)?wp-admin$" ignoreCase="false" />
1747 <action type="Redirect" url="{R:1}wp-admin/" redirectType="Permanent" />
1749 <rule name="wordpress - Rule 4" stopProcessing="true">
1750 <match url="^" ignoreCase="false" />
1751 <conditions logicalGrouping="MatchAny">
1752 <add input="{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="false" />
1753 <add input="{REQUEST_FILENAME}" matchType="IsDirectory" ignoreCase="false" />
1755 <action type="None" />
1757 <rule name="wordpress - Rule 5" stopProcessing="true">
1758 <match url="^([_0-9a-zA-Z-]+/)?(wp-(content|admin|includes).*)" ignoreCase="false" />
1759 <action type="Rewrite" url="{R:2}" />
1761 <rule name="wordpress - Rule 6" stopProcessing="true">
1762 <match url="^([_0-9a-zA-Z-]+/)?(.*\.php)$" ignoreCase="false" />
1763 <action type="Rewrite" url="{R:2}" />
1765 <rule name="wordpress - Rule 7" stopProcessing="true">
1766 <match url="." ignoreCase="false" />
1767 <action type="Rewrite" url="index.php" />
1771 if ( $add_parent_tags ) {
1779 $rules = apply_filters('iis7_url_rewrite_rules', $rules);
1785 * Add a straight rewrite rule.
1787 * Any value in the $after parameter that isn't 'bottom' will be placed at
1788 * the top of the rules.
1793 * @param string $regex Regular expression to match against request.
1794 * @param string $redirect URL regex redirects to when regex matches request.
1795 * @param string $after Optional, default is bottom. Location to place rule.
1797 function add_rule($regex, $redirect, $after = 'bottom') {
1798 //get everything up to the first ?
1799 $index = (strpos($redirect, '?') == false ? strlen($redirect) : strpos($redirect, '?'));
1800 $front = substr($redirect, 0, $index);
1801 if ( $front != $this->index ) { //it doesn't redirect to WP's index.php
1802 $this->add_external_rule($regex, $redirect);
1804 if ( 'bottom' == $after)
1805 $this->extra_rules = array_merge($this->extra_rules, array($regex => $redirect));
1807 $this->extra_rules_top = array_merge($this->extra_rules_top, array($regex => $redirect));
1808 //$this->extra_rules[$regex] = $redirect;
1813 * Add a rule that doesn't redirect to index.php.
1815 * Can redirect to any place.
1820 * @param string $regex Regular expression to match against request.
1821 * @param string $redirect URL regex redirects to when regex matches request.
1823 function add_external_rule($regex, $redirect) {
1824 $this->non_wp_rules[$regex] = $redirect;
1828 * Add an endpoint, like /trackback/.
1830 * To be inserted after certain URL types (specified in $places).
1835 * @param string $name Name of endpoint.
1836 * @param array $places URL types that endpoint can be used.
1838 function add_endpoint($name, $places) {
1840 $this->endpoints[] = array ( $places, $name );
1841 $wp->add_query_var($name);
1845 * Add permalink structure.
1847 * These are added along with the extra rewrite rules that are merged to the
1853 * @param string $name Name for permalink structure.
1854 * @param string $struct Permalink structure.
1855 * @param bool $with_front Prepend front base to permalink structure.
1857 function add_permastruct($name, $struct, $with_front = true, $ep_mask = EP_NONE) {
1859 $struct = $this->front . $struct;
1861 $struct = $this->root . $struct;
1862 $this->extra_permastructs[$name] = array($struct, $ep_mask);
1866 * Remove rewrite rules and then recreate rewrite rules.
1868 * Calls {@link WP_Rewrite::wp_rewrite_rules()} after removing the
1869 * 'rewrite_rules' option. If the function named 'save_mod_rewrite_rules'
1870 * exists, it will be called.
1874 * @param bool $hard Whether to update .htaccess (hard flush) or just update rewrite_rules option (soft flush). Default is true (hard).
1876 function flush_rules($hard = true) {
1877 delete_option('rewrite_rules');
1878 $this->wp_rewrite_rules();
1879 if ( $hard && function_exists('save_mod_rewrite_rules') )
1880 save_mod_rewrite_rules();
1881 if ( $hard && function_exists('iis7_save_url_rewrite_rules') )
1882 iis7_save_url_rewrite_rules();
1886 * Sets up the object's properties.
1888 * The 'use_verbose_page_rules' object property will be set to true if the
1889 * permalink structure begins with one of the following: '%postname%', '%category%',
1890 * '%tag%', or '%author%'.
1896 $this->extra_rules = $this->non_wp_rules = $this->endpoints = array();
1897 $this->permalink_structure = get_option('permalink_structure');
1898 $this->front = substr($this->permalink_structure, 0, strpos($this->permalink_structure, '%'));
1900 if ( $this->using_index_permalinks() )
1901 $this->root = $this->index . '/';
1902 unset($this->author_structure);
1903 unset($this->date_structure);
1904 unset($this->page_structure);
1905 unset($this->search_structure);
1906 unset($this->feed_structure);
1907 unset($this->comment_feed_structure);
1908 $this->use_trailing_slashes = ( '/' == substr($this->permalink_structure, -1, 1) );
1910 // Enable generic rules for pages if permalink structure doesn't begin with a wildcard.
1911 if ( preg_match("/^[^%]*%(?:postname|category|tag|author)%/", $this->permalink_structure) )
1912 $this->use_verbose_page_rules = true;
1914 $this->use_verbose_page_rules = false;
1918 * Set the main permalink structure for the blog.
1920 * Will update the 'permalink_structure' option, if there is a difference
1921 * between the current permalink structure and the parameter value. Calls
1922 * {@link WP_Rewrite::init()} after the option is updated.
1924 * Fires the 'permalink_structure_changed' action once the init call has
1925 * processed passing the old and new values
1930 * @param string $permalink_structure Permalink structure.
1932 function set_permalink_structure($permalink_structure) {
1933 if ( $permalink_structure != $this->permalink_structure ) {
1934 update_option('permalink_structure', $permalink_structure);
1936 do_action('permalink_structure_changed', $this->permalink_structure, $permalink_structure);
1941 * Set the category base for the category permalink.
1943 * Will update the 'category_base' option, if there is a difference between
1944 * the current category base and the parameter value. Calls
1945 * {@link WP_Rewrite::init()} after the option is updated.
1950 * @param string $category_base Category permalink structure base.
1952 function set_category_base($category_base) {
1953 if ( $category_base != get_option('category_base') ) {
1954 update_option('category_base', $category_base);
1960 * Set the tag base for the tag permalink.
1962 * Will update the 'tag_base' option, if there is a difference between the
1963 * current tag base and the parameter value. Calls
1964 * {@link WP_Rewrite::init()} after the option is updated.
1969 * @param string $tag_base Tag permalink structure base.
1971 function set_tag_base( $tag_base ) {
1972 if ( $tag_base != get_option( 'tag_base') ) {
1973 update_option( 'tag_base', $tag_base );
1979 * PHP4 Constructor - Calls init(), which runs setup.
1984 * @return WP_Rewrite
1986 function WP_Rewrite() {