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) {
310 // If the requesting file is the anchor of the match, prepend it
312 if ( !empty($url) && ($url != $request) && (strpos($match, $url) === 0) )
313 $request_match = $url . '/' . $request;
315 if ( preg_match("!^$match!", $request_match, $matches) ) {
317 if ( $wp_rewrite->use_verbose_page_rules && preg_match( '/pagename=\$matches\[([0-9]+)\]/', $query, $varmatch ) ) {
318 // this is a verbose page match, lets check to be sure about it
319 if ( ! get_page_by_path( $matches[ $varmatch[1] ] ) )
324 // Trim the query of everything up to the '?'.
325 $query = preg_replace("!^.+\?!", '', $query);
327 // Substitute the substring matches into the query.
328 $query = addslashes(WP_MatchesMapRegex::apply($query, $matches));
330 // Filter out non-public query vars
332 parse_str($query, $query_vars);
334 foreach ( (array) $query_vars as $key => $value ) {
335 if ( in_array($key, $wp->public_query_vars) )
336 $query[$key] = $value;
340 $query = new WP_Query($query);
341 if ( !empty($query->posts) && $query->is_singular )
342 return $query->post->ID;
351 * WordPress Rewrite Component.
353 * The WordPress Rewrite class writes the rewrite module rules to the .htaccess
354 * file. It also handles parsing the request to get the correct setup for the
355 * WordPress Query class.
357 * The Rewrite along with WP class function as a front controller for WordPress.
358 * You can add rules to trigger your page view and processing using this
359 * component. The full functionality of a front controller does not exist,
360 * meaning you can't define how the template files load based on the rewrite
367 * Default permalink structure for WordPress.
373 var $permalink_structure;
376 * Whether to add trailing slashes.
382 var $use_trailing_slashes;
385 * Permalink author request base ( example.com/author/authorname ).
391 var $author_base = 'author';
394 * Permalink request structure for author pages.
400 var $author_structure;
403 * Permalink request structure for dates.
412 * Permalink request structure for pages.
421 * Search permalink base ( example.com/search/query ).
427 var $search_base = 'search';
430 * Permalink request structure for searches.
436 var $search_structure;
439 * Comments permalink base.
445 var $comments_base = 'comments';
448 * Pagination permalink base.
454 var $pagination_base = 'page';
457 * Feed permalink base.
463 var $feed_base = 'feed';
466 * Comments feed request structure permalink.
472 var $comments_feed_structure;
475 * Feed request structure permalink.
486 * The difference between the root property is that WordPress might be
487 * located at example/WordPress/index.php, if permalinks are turned off. The
488 * WordPress/index.php will be the front portion. If permalinks are turned
489 * on, this will most likely be empty or not set.
498 * Root URL path to WordPress (without domain).
500 * The difference between front property is that WordPress might be located
501 * at example.com/WordPress/. The root is the 'WordPress/' portion.
510 * Permalink to the home page.
516 var $index = 'index.php';
519 * Request match string.
528 * Rewrite rules to match against the request to find the redirect or query.
537 * Additional rules added external to the rewrite class.
539 * Those not generated by the class, see add_rewrite_rule().
545 var $extra_rules = array(); //
548 * Additional rules that belong at the beginning to match first.
550 * Those not generated by the class, see add_rewrite_rule().
556 var $extra_rules_top = array(); //
559 * Rules that don't redirect to WP's index.php.
561 * These rules are written to the mod_rewrite portion of the .htaccess.
567 var $non_wp_rules = array(); //
570 * Extra permalink structures.
576 var $extra_permastructs = array();
579 * Endpoints permalinks
588 * Whether to write every mod_rewrite rule for WordPress.
590 * This is off by default, turning it on might print a lot of rewrite rules
591 * to the .htaccess file.
597 var $use_verbose_rules = false;
600 * Whether to write every mod_rewrite rule for WordPress pages.
606 var $use_verbose_page_rules = true;
609 * Permalink structure search for preg_replace.
631 * Preg_replace values for the search, see {@link WP_Rewrite::$rewritecode}.
637 var $rewritereplace =
653 * Search for the query to look for replacing.
675 * Supported default feeds.
681 var $feeds = array ( 'feed', 'rdf', 'rss', 'rss2', 'atom' );
684 * Whether permalinks are being used.
686 * This can be either rewrite module or permalink in the HTTP query string.
691 * @return bool True, if permalinks are enabled.
693 function using_permalinks() {
694 return ! empty($this->permalink_structure);
698 * Whether permalinks are being used and rewrite module is not enabled.
700 * Means that permalink links are enabled and index.php is in the URL.
707 function using_index_permalinks() {
708 if ( empty($this->permalink_structure) )
711 // If the index is not in the permalink, we're using mod_rewrite.
712 if ( preg_match('#^/*' . $this->index . '#', $this->permalink_structure) )
719 * Whether permalinks are being used and rewrite module is enabled.
721 * Using permalinks and index.php is not in the URL.
728 function using_mod_rewrite_permalinks() {
729 if ( $this->using_permalinks() && ! $this->using_index_permalinks() )
736 * Index for matches for usage in preg_*() functions.
738 * The format of the string is, with empty matches property value, '$NUM'.
739 * The 'NUM' will be replaced with the value in the $number parameter. With
740 * the matches property not empty, the value of the returned string will
741 * contain that value of the matches property. The format then will be
742 * '$MATCHES[NUM]', with MATCHES as the value in the property and NUM the
743 * value of the $number parameter.
748 * @param int $number Index number.
751 function preg_index($number) {
755 if ( ! empty($this->matches) ) {
756 $match_prefix = '$' . $this->matches . '[';
760 return "$match_prefix$number$match_suffix";
764 * Retrieve all page and attachments for pages URIs.
766 * The attachments are for those that have pages as parents and will be
772 * @return array Array of page URIs as first element and attachment URIs as second element.
774 function page_uri_index() {
777 //get pages in order of hierarchy, i.e. children after parents
778 $posts = get_page_hierarchy( $wpdb->get_results("SELECT ID, post_name, post_parent FROM $wpdb->posts WHERE post_type = 'page' AND post_status != 'auto-draft'") );
780 // If we have no pages get out quick
782 return array( array(), array() );
784 //now reverse it, because we need parents after children for rewrite rules to work properly
785 $posts = array_reverse($posts, true);
787 $page_uris = array();
788 $page_attachment_uris = array();
790 foreach ( $posts as $id => $post ) {
792 $uri = get_page_uri($id);
793 $attachments = $wpdb->get_results( $wpdb->prepare( "SELECT ID, post_name, post_parent FROM $wpdb->posts WHERE post_type = 'attachment' AND post_parent = %d", $id ));
794 if ( !empty($attachments) ) {
795 foreach ( $attachments as $attachment ) {
796 $attach_uri = get_page_uri($attachment->ID);
797 $page_attachment_uris[$attach_uri] = $attachment->ID;
801 $page_uris[$uri] = $id;
804 return array( $page_uris, $page_attachment_uris );
808 * Retrieve all of the rewrite rules for pages.
810 * If the 'use_verbose_page_rules' property is false, then there will only
811 * be a single rewrite rule for pages for those matching '%pagename%'. With
812 * the property set to true, the attachments and the pages will be added for
813 * each individual attachment URI and page URI, respectively.
820 function page_rewrite_rules() {
821 $rewrite_rules = array();
822 $page_structure = $this->get_page_permastruct();
824 // the extra .? at the beginning prevents clashes with other regular expressions in the rules array
825 $this->add_rewrite_tag('%pagename%', "(.?.+?)", 'pagename=');
826 $rewrite_rules = array_merge($rewrite_rules, $this->generate_rewrite_rules($page_structure, EP_PAGES));
827 return $rewrite_rules;
831 * Retrieve date permalink structure, with year, month, and day.
833 * The permalink structure for the date, if not set already depends on the
834 * permalink structure. It can be one of three formats. The first is year,
835 * month, day; the second is day, month, year; and the last format is month,
836 * day, year. These are matched against the permalink structure for which
837 * one is used. If none matches, then the default will be used, which is
840 * Prevents post ID and date permalinks from overlapping. In the case of
841 * post_id, the date permalink will be prepended with front permalink with
842 * 'date/' before the actual permalink to form the complete date permalink
848 * @return bool|string False on no permalink structure. Date permalink structure.
850 function get_date_permastruct() {
851 if ( isset($this->date_structure) )
852 return $this->date_structure;
854 if ( empty($this->permalink_structure) ) {
855 $this->date_structure = '';
859 // The date permalink must have year, month, and day separated by slashes.
860 $endians = array('%year%/%monthnum%/%day%', '%day%/%monthnum%/%year%', '%monthnum%/%day%/%year%');
862 $this->date_structure = '';
865 foreach ( $endians as $endian ) {
866 if ( false !== strpos($this->permalink_structure, $endian) ) {
867 $date_endian= $endian;
872 if ( empty($date_endian) )
873 $date_endian = '%year%/%monthnum%/%day%';
875 // Do not allow the date tags and %post_id% to overlap in the permalink
876 // structure. If they do, move the date tags to $front/date/.
877 $front = $this->front;
878 preg_match_all('/%.+?%/', $this->permalink_structure, $tokens);
880 foreach ( (array) $tokens[0] as $token) {
881 if ( '%post_id%' == $token && ($tok_index <= 3) ) {
882 $front = $front . 'date/';
888 $this->date_structure = $front . $date_endian;
890 return $this->date_structure;
894 * Retrieve the year permalink structure without month and day.
896 * Gets the date permalink structure and strips out the month and day
897 * permalink structures.
902 * @return bool|string False on failure. Year structure on success.
904 function get_year_permastruct() {
905 $structure = $this->get_date_permastruct($this->permalink_structure);
907 if ( empty($structure) )
910 $structure = str_replace('%monthnum%', '', $structure);
911 $structure = str_replace('%day%', '', $structure);
913 $structure = preg_replace('#/+#', '/', $structure);
919 * Retrieve the month permalink structure without day and with year.
921 * Gets the date permalink structure and strips out the day permalink
922 * structures. Keeps the year permalink structure.
927 * @return bool|string False on failure. Year/Month structure on success.
929 function get_month_permastruct() {
930 $structure = $this->get_date_permastruct($this->permalink_structure);
932 if ( empty($structure) )
935 $structure = str_replace('%day%', '', $structure);
937 $structure = preg_replace('#/+#', '/', $structure);
943 * Retrieve the day permalink structure with month and year.
945 * Keeps date permalink structure with all year, month, and day.
950 * @return bool|string False on failure. Year/Month/Day structure on success.
952 function get_day_permastruct() {
953 return $this->get_date_permastruct($this->permalink_structure);
957 * Retrieve the permalink structure for categories.
959 * If the category_base property has no value, then the category structure
960 * will have the front property value, followed by 'category', and finally
961 * '%category%'. If it does, then the root property will be used, along with
962 * the category_base property value.
967 * @return bool|string False on failure. Category permalink structure.
969 function get_category_permastruct() {
970 return $this->get_extra_permastruct('category');
974 * Retrieve the permalink structure for tags.
976 * If the tag_base property has no value, then the tag structure will have
977 * the front property value, followed by 'tag', and finally '%tag%'. If it
978 * does, then the root property will be used, along with the tag_base
984 * @return bool|string False on failure. Tag permalink structure.
986 function get_tag_permastruct() {
987 return $this->get_extra_permastruct('post_tag');
991 * Retrieve extra permalink structure by name.
996 * @param string $name Permalink structure name.
997 * @return string|bool False if not found. Permalink structure string.
999 function get_extra_permastruct($name) {
1000 if ( empty($this->permalink_structure) )
1003 if ( isset($this->extra_permastructs[$name]) )
1004 return $this->extra_permastructs[$name][0];
1010 * Retrieve the author permalink structure.
1012 * The permalink structure is front property, author base, and finally
1013 * '/%author%'. Will set the author_structure property and then return it
1014 * without attempting to set the value again.
1019 * @return string|bool False if not found. Permalink structure string.
1021 function get_author_permastruct() {
1022 if ( isset($this->author_structure) )
1023 return $this->author_structure;
1025 if ( empty($this->permalink_structure) ) {
1026 $this->author_structure = '';
1030 $this->author_structure = $this->front . $this->author_base . '/%author%';
1032 return $this->author_structure;
1036 * Retrieve the search permalink structure.
1038 * The permalink structure is root property, search base, and finally
1039 * '/%search%'. Will set the search_structure property and then return it
1040 * without attempting to set the value again.
1045 * @return string|bool False if not found. Permalink structure string.
1047 function get_search_permastruct() {
1048 if ( isset($this->search_structure) )
1049 return $this->search_structure;
1051 if ( empty($this->permalink_structure) ) {
1052 $this->search_structure = '';
1056 $this->search_structure = $this->root . $this->search_base . '/%search%';
1058 return $this->search_structure;
1062 * Retrieve the page permalink structure.
1064 * The permalink structure is root property, and '%pagename%'. Will set the
1065 * page_structure property and then return it without attempting to set the
1071 * @return string|bool False if not found. Permalink structure string.
1073 function get_page_permastruct() {
1074 if ( isset($this->page_structure) )
1075 return $this->page_structure;
1077 if (empty($this->permalink_structure)) {
1078 $this->page_structure = '';
1082 $this->page_structure = $this->root . '%pagename%';
1084 return $this->page_structure;
1088 * Retrieve the feed permalink structure.
1090 * The permalink structure is root property, feed base, and finally
1091 * '/%feed%'. Will set the feed_structure property and then return it
1092 * without attempting to set the value again.
1097 * @return string|bool False if not found. Permalink structure string.
1099 function get_feed_permastruct() {
1100 if ( isset($this->feed_structure) )
1101 return $this->feed_structure;
1103 if ( empty($this->permalink_structure) ) {
1104 $this->feed_structure = '';
1108 $this->feed_structure = $this->root . $this->feed_base . '/%feed%';
1110 return $this->feed_structure;
1114 * Retrieve the comment feed permalink structure.
1116 * The permalink structure is root property, comment base property, feed
1117 * base and finally '/%feed%'. Will set the comment_feed_structure property
1118 * and then return it without attempting to set the value again.
1123 * @return string|bool False if not found. Permalink structure string.
1125 function get_comment_feed_permastruct() {
1126 if ( isset($this->comment_feed_structure) )
1127 return $this->comment_feed_structure;
1129 if (empty($this->permalink_structure)) {
1130 $this->comment_feed_structure = '';
1134 $this->comment_feed_structure = $this->root . $this->comments_base . '/' . $this->feed_base . '/%feed%';
1136 return $this->comment_feed_structure;
1140 * Append or update tag, pattern, and query for replacement.
1142 * If the tag already exists, replace the existing pattern and query for
1143 * that tag, otherwise add the new tag, pattern, and query to the end of the
1146 * @internal What is the purpose of this function again? Need to finish long
1152 * @param string $tag Append tag to rewritecode property array.
1153 * @param string $pattern Append pattern to rewritereplace property array.
1154 * @param string $query Append query to queryreplace property array.
1156 function add_rewrite_tag($tag, $pattern, $query) {
1157 $position = array_search($tag, $this->rewritecode);
1158 if ( false !== $position && null !== $position ) {
1159 $this->rewritereplace[$position] = $pattern;
1160 $this->queryreplace[$position] = $query;
1162 $this->rewritecode[] = $tag;
1163 $this->rewritereplace[] = $pattern;
1164 $this->queryreplace[] = $query;
1169 * Generate the rules from permalink structure.
1171 * The main WP_Rewrite function for building the rewrite rule list. The
1172 * contents of the function is a mix of black magic and regular expressions,
1173 * so best just ignore the contents and move to the parameters.
1178 * @param string $permalink_structure The permalink structure.
1179 * @param int $ep_mask Optional, default is EP_NONE. Endpoint constant, see EP_* constants.
1180 * @param bool $paged Optional, default is true. Whether permalink request is paged.
1181 * @param bool $feed Optional, default is true. Whether for feed.
1182 * @param bool $forcomments Optional, default is false. Whether for comments.
1183 * @param bool $walk_dirs Optional, default is true. Whether to create list of directories to walk over.
1184 * @param bool $endpoints Optional, default is true. Whether endpoints are enabled.
1185 * @return array Rewrite rule list.
1187 function generate_rewrite_rules($permalink_structure, $ep_mask = EP_NONE, $paged = true, $feed = true, $forcomments = false, $walk_dirs = true, $endpoints = true) {
1188 //build a regex to match the feed section of URLs, something like (feed|atom|rss|rss2)/?
1190 foreach ( (array) $this->feeds as $feed_name)
1191 $feedregex2 .= $feed_name . '|';
1192 $feedregex2 = '(' . trim($feedregex2, '|') . ')/?$';
1194 //$feedregex is identical but with /feed/ added on as well, so URLs like <permalink>/feed/atom
1195 //and <permalink>/atom are both possible
1196 $feedregex = $this->feed_base . '/' . $feedregex2;
1198 //build a regex to match the trackback and page/xx parts of URLs
1199 $trackbackregex = 'trackback/?$';
1200 $pageregex = $this->pagination_base . '/?([0-9]{1,})/?$';
1201 $commentregex = 'comment-page-([0-9]{1,})/?$';
1203 //build up an array of endpoint regexes to append => queries to append
1205 $ep_query_append = array ();
1206 foreach ( (array) $this->endpoints as $endpoint) {
1207 //match everything after the endpoint name, but allow for nothing to appear there
1208 $epmatch = $endpoint[1] . '(/(.*))?/?$';
1209 //this will be appended on to the rest of the query for each dir
1210 $epquery = '&' . $endpoint[1] . '=';
1211 $ep_query_append[$epmatch] = array ( $endpoint[0], $epquery );
1215 //get everything up to the first rewrite tag
1216 $front = substr($permalink_structure, 0, strpos($permalink_structure, '%'));
1217 //build an array of the tags (note that said array ends up being in $tokens[0])
1218 preg_match_all('/%.+?%/', $permalink_structure, $tokens);
1220 $num_tokens = count($tokens[0]);
1222 $index = $this->index; //probably 'index.php'
1223 $feedindex = $index;
1224 $trackbackindex = $index;
1225 //build a list from the rewritecode and queryreplace arrays, that will look something like
1226 //tagname=$matches[i] where i is the current $i
1227 for ( $i = 0; $i < $num_tokens; ++$i ) {
1229 $queries[$i] = $queries[$i - 1] . '&';
1233 $query_token = str_replace($this->rewritecode, $this->queryreplace, $tokens[0][$i]) . $this->preg_index($i+1);
1234 $queries[$i] .= $query_token;
1237 //get the structure, minus any cruft (stuff that isn't tags) at the front
1238 $structure = $permalink_structure;
1239 if ( $front != '/' )
1240 $structure = str_replace($front, '', $structure);
1242 //create a list of dirs to walk over, making rewrite rules for each level
1243 //so for example, a $structure of /%year%/%month%/%postname% would create
1244 //rewrite rules for /%year%/, /%year%/%month%/ and /%year%/%month%/%postname%
1245 $structure = trim($structure, '/');
1246 $dirs = $walk_dirs ? explode('/', $structure) : array( $structure );
1247 $num_dirs = count($dirs);
1249 //strip slashes from the front of $front
1250 $front = preg_replace('|^/+|', '', $front);
1252 //the main workhorse loop
1253 $post_rewrite = array();
1255 for ( $j = 0; $j < $num_dirs; ++$j ) {
1256 //get the struct for this dir, and trim slashes off the front
1257 $struct .= $dirs[$j] . '/'; //accumulate. see comment near explode('/', $structure) above
1258 $struct = ltrim($struct, '/');
1260 //replace tags with regexes
1261 $match = str_replace($this->rewritecode, $this->rewritereplace, $struct);
1263 //make a list of tags, and store how many there are in $num_toks
1264 $num_toks = preg_match_all('/%.+?%/', $struct, $toks);
1266 //get the 'tagname=$matches[i]'
1267 $query = ( isset($queries) && is_array($queries) && !empty($num_toks) ) ? $queries[$num_toks - 1] : '';
1269 //set up $ep_mask_specific which is used to match more specific URL types
1270 switch ( $dirs[$j] ) {
1272 $ep_mask_specific = EP_YEAR;
1275 $ep_mask_specific = EP_MONTH;
1278 $ep_mask_specific = EP_DAY;
1281 $ep_mask_specific = EP_NONE;
1284 //create query for /page/xx
1285 $pagematch = $match . $pageregex;
1286 $pagequery = $index . '?' . $query . '&paged=' . $this->preg_index($num_toks + 1);
1288 //create query for /comment-page-xx
1289 $commentmatch = $match . $commentregex;
1290 $commentquery = $index . '?' . $query . '&cpage=' . $this->preg_index($num_toks + 1);
1292 if ( get_option('page_on_front') ) {
1293 //create query for Root /comment-page-xx
1294 $rootcommentmatch = $match . $commentregex;
1295 $rootcommentquery = $index . '?' . $query . '&page_id=' . get_option('page_on_front') . '&cpage=' . $this->preg_index($num_toks + 1);
1298 //create query for /feed/(feed|atom|rss|rss2|rdf)
1299 $feedmatch = $match . $feedregex;
1300 $feedquery = $feedindex . '?' . $query . '&feed=' . $this->preg_index($num_toks + 1);
1302 //create query for /(feed|atom|rss|rss2|rdf) (see comment near creation of $feedregex)
1303 $feedmatch2 = $match . $feedregex2;
1304 $feedquery2 = $feedindex . '?' . $query . '&feed=' . $this->preg_index($num_toks + 1);
1306 //if asked to, turn the feed queries into comment feed ones
1307 if ( $forcomments ) {
1308 $feedquery .= '&withcomments=1';
1309 $feedquery2 .= '&withcomments=1';
1312 //start creating the array of rewrites for this dir
1314 if ( $feed ) //...adding on /feed/ regexes => queries
1315 $rewrite = array($feedmatch => $feedquery, $feedmatch2 => $feedquery2);
1316 if ( $paged ) //...and /page/xx ones
1317 $rewrite = array_merge($rewrite, array($pagematch => $pagequery));
1319 //only on pages with comments add ../comment-page-xx/
1320 if ( EP_PAGES & $ep_mask || EP_PERMALINK & $ep_mask )
1321 $rewrite = array_merge($rewrite, array($commentmatch => $commentquery));
1322 else if ( EP_ROOT & $ep_mask && get_option('page_on_front') )
1323 $rewrite = array_merge($rewrite, array($rootcommentmatch => $rootcommentquery));
1327 foreach ( (array) $ep_query_append as $regex => $ep) {
1328 //add the endpoints on if the mask fits
1329 if ( $ep[0] & $ep_mask || $ep[0] & $ep_mask_specific )
1330 $rewrite[$match . $regex] = $index . '?' . $query . $ep[1] . $this->preg_index($num_toks + 2);
1334 //if we've got some tags in this dir
1339 //check to see if this dir is permalink-level: i.e. the structure specifies an
1340 //individual post. Do this by checking it contains at least one of 1) post name,
1341 //2) post ID, 3) page name, 4) timestamp (year, month, day, hour, second and
1342 //minute all present). Set these flags now as we need them for the endpoints.
1343 if ( strpos($struct, '%postname%') !== false
1344 || strpos($struct, '%post_id%') !== false
1345 || strpos($struct, '%pagename%') !== false
1346 || (strpos($struct, '%year%') !== false && strpos($struct, '%monthnum%') !== false && strpos($struct, '%day%') !== false && strpos($struct, '%hour%') !== false && strpos($struct, '%minute%') !== false && strpos($struct, '%second%') !== false)
1349 if ( strpos($struct, '%pagename%') !== false )
1354 // For custom post types, we need to add on endpoints as well.
1355 foreach ( get_post_types( array('_builtin' => false ) ) as $ptype ) {
1356 if ( strpos($struct, "%$ptype%") !== false ) {
1358 $page = is_post_type_hierarchical( $ptype ); // This is for page style attachment url's
1364 //if we're creating rules for a permalink, do all the endpoints like attachments etc
1366 //create query and regex for trackback
1367 $trackbackmatch = $match . $trackbackregex;
1368 $trackbackquery = $trackbackindex . '?' . $query . '&tb=1';
1369 //trim slashes from the end of the regex for this dir
1370 $match = rtrim($match, '/');
1371 //get rid of brackets
1372 $submatchbase = str_replace( array('(', ')'), '', $match);
1374 //add a rule for at attachments, which take the form of <permalink>/some-text
1375 $sub1 = $submatchbase . '/([^/]+)/';
1376 $sub1tb = $sub1 . $trackbackregex; //add trackback regex <permalink>/trackback/...
1377 $sub1feed = $sub1 . $feedregex; //and <permalink>/feed/(atom|...)
1378 $sub1feed2 = $sub1 . $feedregex2; //and <permalink>/(feed|atom...)
1379 $sub1comment = $sub1 . $commentregex; //and <permalink>/comment-page-xx
1380 //add an ? as we don't have to match that last slash, and finally a $ so we
1381 //match to the end of the URL
1383 //add another rule to match attachments in the explicit form:
1384 //<permalink>/attachment/some-text
1385 $sub2 = $submatchbase . '/attachment/([^/]+)/';
1386 $sub2tb = $sub2 . $trackbackregex; //and add trackbacks <permalink>/attachment/trackback
1387 $sub2feed = $sub2 . $feedregex; //feeds, <permalink>/attachment/feed/(atom|...)
1388 $sub2feed2 = $sub2 . $feedregex2; //and feeds again on to this <permalink>/attachment/(feed|atom...)
1389 $sub2comment = $sub2 . $commentregex; //and <permalink>/comment-page-xx
1391 //create queries for these extra tag-ons we've just dealt with
1392 $subquery = $index . '?attachment=' . $this->preg_index(1);
1393 $subtbquery = $subquery . '&tb=1';
1394 $subfeedquery = $subquery . '&feed=' . $this->preg_index(2);
1395 $subcommentquery = $subquery . '&cpage=' . $this->preg_index(2);
1397 //do endpoints for attachments
1398 if ( !empty($endpoints) ) {
1399 foreach ( (array) $ep_query_append as $regex => $ep ) {
1400 if ( $ep[0] & EP_ATTACHMENT ) {
1401 $rewrite[$sub1 . $regex] = $subquery . $ep[1] . $this->preg_index(2);
1402 $rewrite[$sub2 . $regex] = $subquery . $ep[1] . $this->preg_index(2);
1407 //now we've finished with endpoints, finish off the $sub1 and $sub2 matches
1411 //allow URLs like <permalink>/2 for <permalink>/page/2
1412 $match = $match . '(/[0-9]+)?/?$';
1413 $query = $index . '?' . $query . '&page=' . $this->preg_index($num_toks + 1);
1414 } else { //not matching a permalink so this is a lot simpler
1415 //close the match and finalise the query
1417 $query = $index . '?' . $query;
1420 //create the final array for this dir by joining the $rewrite array (which currently
1421 //only contains rules/queries for trackback, pages etc) to the main regex/query for
1423 $rewrite = array_merge($rewrite, array($match => $query));
1425 //if we're matching a permalink, add those extras (attachments etc) on
1428 $rewrite = array_merge(array($trackbackmatch => $trackbackquery), $rewrite);
1430 //add regexes/queries for attachments, attachment trackbacks and so on
1431 if ( ! $page ) //require <permalink>/attachment/stuff form for pages because of confusion with subpages
1432 $rewrite = array_merge($rewrite, array($sub1 => $subquery, $sub1tb => $subtbquery, $sub1feed => $subfeedquery, $sub1feed2 => $subfeedquery, $sub1comment => $subcommentquery));
1433 $rewrite = array_merge(array($sub2 => $subquery, $sub2tb => $subtbquery, $sub2feed => $subfeedquery, $sub2feed2 => $subfeedquery, $sub2comment => $subcommentquery), $rewrite);
1436 //add the rules for this dir to the accumulating $post_rewrite
1437 $post_rewrite = array_merge($rewrite, $post_rewrite);
1439 return $post_rewrite; //the finished rules. phew!
1443 * Generate Rewrite rules with permalink structure and walking directory only.
1445 * Shorten version of {@link WP_Rewrite::generate_rewrite_rules()} that
1446 * allows for shorter list of parameters. See the method for longer
1447 * description of what generating rewrite rules does.
1449 * @uses WP_Rewrite::generate_rewrite_rules() See for long description and rest of parameters.
1453 * @param string $permalink_structure The permalink structure to generate rules.
1454 * @param bool $walk_dirs Optional, default is false. Whether to create list of directories to walk over.
1457 function generate_rewrite_rule($permalink_structure, $walk_dirs = false) {
1458 return $this->generate_rewrite_rules($permalink_structure, EP_NONE, false, false, false, $walk_dirs);
1462 * Construct rewrite matches and queries from permalink structure.
1464 * Runs the action 'generate_rewrite_rules' with the parameter that is an
1465 * reference to the current WP_Rewrite instance to further manipulate the
1466 * permalink structures and rewrite rules. Runs the 'rewrite_rules_array'
1467 * filter on the full rewrite rule array.
1469 * There are two ways to manipulate the rewrite rules, one by hooking into
1470 * the 'generate_rewrite_rules' action and gaining full control of the
1471 * object or just manipulating the rewrite rule array before it is passed
1472 * from the function.
1477 * @return array An associate array of matches and queries.
1479 function rewrite_rules() {
1482 if ( empty($this->permalink_structure) )
1485 // robots.txt -only if installed at the root
1486 $home_path = parse_url( home_url() );
1487 $robots_rewrite = ( empty( $home_path['path'] ) || '/' == $home_path['path'] ) ? array( 'robots\.txt$' => $this->index . '?robots=1' ) : array();
1490 $old_feed_files = array( '.*wp-(atom|rdf|rss|rss2|feed|commentsrss2)\.php$' => $this->index . '?feed=old' );
1492 // Registration rules
1493 $registration_pages = array();
1494 if ( is_multisite() && is_main_site() ) {
1495 $registration_pages['.*wp-signup.php$'] = $this->index . '?signup=true';
1496 $registration_pages['.*wp-activate.php$'] = $this->index . '?activate=true';
1500 $post_rewrite = $this->generate_rewrite_rules($this->permalink_structure, EP_PERMALINK);
1501 $post_rewrite = apply_filters('post_rewrite_rules', $post_rewrite);
1504 $date_rewrite = $this->generate_rewrite_rules($this->get_date_permastruct(), EP_DATE);
1505 $date_rewrite = apply_filters('date_rewrite_rules', $date_rewrite);
1508 $root_rewrite = $this->generate_rewrite_rules($this->root . '/', EP_ROOT);
1509 $root_rewrite = apply_filters('root_rewrite_rules', $root_rewrite);
1512 $comments_rewrite = $this->generate_rewrite_rules($this->root . $this->comments_base, EP_COMMENTS, true, true, true, false);
1513 $comments_rewrite = apply_filters('comments_rewrite_rules', $comments_rewrite);
1516 $search_structure = $this->get_search_permastruct();
1517 $search_rewrite = $this->generate_rewrite_rules($search_structure, EP_SEARCH);
1518 $search_rewrite = apply_filters('search_rewrite_rules', $search_rewrite);
1521 $author_rewrite = $this->generate_rewrite_rules($this->get_author_permastruct(), EP_AUTHORS);
1522 $author_rewrite = apply_filters('author_rewrite_rules', $author_rewrite);
1525 $page_rewrite = $this->page_rewrite_rules();
1526 $page_rewrite = apply_filters('page_rewrite_rules', $page_rewrite);
1528 // Extra permastructs
1529 foreach ( $this->extra_permastructs as $permastructname => $permastruct ) {
1530 if ( is_array($permastruct) )
1531 $rules = $this->generate_rewrite_rules($permastruct[0], $permastruct[1]);
1533 $rules = $this->generate_rewrite_rules($permastruct, EP_NONE);
1535 $rules = apply_filters($permastructname . '_rewrite_rules', $rules);
1536 if ( 'post_tag' == $permastructname )
1537 $rules = apply_filters('tag_rewrite_rules', $rules);
1539 $this->extra_rules_top = array_merge($this->extra_rules_top, $rules);
1542 // Put them together.
1543 if ( $this->use_verbose_page_rules )
1544 $this->rules = array_merge($this->extra_rules_top, $robots_rewrite, $old_feed_files, $registration_pages, $root_rewrite, $comments_rewrite, $search_rewrite, $author_rewrite, $date_rewrite, $page_rewrite, $post_rewrite, $this->extra_rules);
1546 $this->rules = array_merge($this->extra_rules_top, $robots_rewrite, $old_feed_files, $registration_pages, $root_rewrite, $comments_rewrite, $search_rewrite, $author_rewrite, $date_rewrite, $post_rewrite, $page_rewrite, $this->extra_rules);
1548 do_action_ref_array('generate_rewrite_rules', array(&$this));
1549 $this->rules = apply_filters('rewrite_rules_array', $this->rules);
1551 return $this->rules;
1555 * Retrieve the rewrite rules.
1557 * The difference between this method and {@link
1558 * WP_Rewrite::rewrite_rules()} is that this method stores the rewrite rules
1559 * in the 'rewrite_rules' option and retrieves it. This prevents having to
1560 * process all of the permalinks to get the rewrite rules in the form of
1566 * @return array Rewrite rules.
1568 function wp_rewrite_rules() {
1569 $this->rules = get_option('rewrite_rules');
1570 if ( empty($this->rules) ) {
1571 $this->matches = 'matches';
1572 $this->rewrite_rules();
1573 update_option('rewrite_rules', $this->rules);
1576 return $this->rules;
1580 * Retrieve mod_rewrite formatted rewrite rules to write to .htaccess.
1582 * Does not actually write to the .htaccess file, but creates the rules for
1583 * the process that will.
1585 * Will add the non_wp_rules property rules to the .htaccess file before
1586 * the WordPress rewrite rules one.
1593 function mod_rewrite_rules() {
1594 if ( ! $this->using_permalinks() )
1597 $site_root = parse_url(get_option('siteurl'));
1598 if ( isset( $site_root['path'] ) )
1599 $site_root = trailingslashit($site_root['path']);
1601 $home_root = parse_url(home_url());
1602 if ( isset( $home_root['path'] ) )
1603 $home_root = trailingslashit($home_root['path']);
1607 $rules = "<IfModule mod_rewrite.c>\n";
1608 $rules .= "RewriteEngine On\n";
1609 $rules .= "RewriteBase $home_root\n";
1610 $rules .= "RewriteRule ^index\.php$ - [L]\n"; // Prevent -f checks on index.php.
1612 //add in the rules that don't redirect to WP's index.php (and thus shouldn't be handled by WP at all)
1613 foreach ( (array) $this->non_wp_rules as $match => $query) {
1614 // Apache 1.3 does not support the reluctant (non-greedy) modifier.
1615 $match = str_replace('.+?', '.+', $match);
1617 // If the match is unanchored and greedy, prepend rewrite conditions
1618 // to avoid infinite redirects and eclipsing of real files.
1619 //if ($match == '(.+)/?$' || $match == '([^/]+)/?$' ) {
1623 $rules .= 'RewriteRule ^' . $match . ' ' . $home_root . $query . " [QSA,L]\n";
1626 if ( $this->use_verbose_rules ) {
1627 $this->matches = '';
1628 $rewrite = $this->rewrite_rules();
1629 $num_rules = count($rewrite);
1630 $rules .= "RewriteCond %{REQUEST_FILENAME} -f [OR]\n" .
1631 "RewriteCond %{REQUEST_FILENAME} -d\n" .
1632 "RewriteRule ^.*$ - [S=$num_rules]\n";
1634 foreach ( (array) $rewrite as $match => $query) {
1635 // Apache 1.3 does not support the reluctant (non-greedy) modifier.
1636 $match = str_replace('.+?', '.+', $match);
1638 // If the match is unanchored and greedy, prepend rewrite conditions
1639 // to avoid infinite redirects and eclipsing of real files.
1640 //if ($match == '(.+)/?$' || $match == '([^/]+)/?$' ) {
1644 if ( strpos($query, $this->index) !== false )
1645 $rules .= 'RewriteRule ^' . $match . ' ' . $home_root . $query . " [QSA,L]\n";
1647 $rules .= 'RewriteRule ^' . $match . ' ' . $site_root . $query . " [QSA,L]\n";
1650 $rules .= "RewriteCond %{REQUEST_FILENAME} !-f\n" .
1651 "RewriteCond %{REQUEST_FILENAME} !-d\n" .
1652 "RewriteRule . {$home_root}{$this->index} [L]\n";
1655 $rules .= "</IfModule>\n";
1657 $rules = apply_filters('mod_rewrite_rules', $rules);
1658 $rules = apply_filters('rewrite_rules', $rules); // Deprecated
1664 * Retrieve IIS7 URL Rewrite formatted rewrite rules to write to web.config file.
1666 * Does not actually write to the web.config file, but creates the rules for
1667 * the process that will.
1674 function iis7_url_rewrite_rules( $add_parent_tags = false ) {
1676 if ( ! $this->using_permalinks() )
1679 if ( $add_parent_tags ) {
1680 $rules .= '<configuration>
1685 if ( !is_multisite() ) {
1687 <rule name="wordpress" patternSyntax="Wildcard">
1690 <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
1691 <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
1693 <action type="Rewrite" url="index.php" />
1696 if (is_subdomain_install()) {
1698 <rule name="wordpress - Rule 1" stopProcessing="true">
1699 <match url="^index\.php$" ignoreCase="false" />
1700 <action type="None" />
1702 <rule name="wordpress - Rule 2" stopProcessing="true">
1703 <match url="^files/(.+)" ignoreCase="false" />
1704 <action type="Rewrite" url="wp-includes/ms-files.php?file={R:1}" appendQueryString="false" />
1706 <rule name="wordpress - Rule 3" stopProcessing="true">
1707 <match url="^" ignoreCase="false" />
1708 <conditions logicalGrouping="MatchAny">
1709 <add input="{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="false" />
1710 <add input="{REQUEST_FILENAME}" matchType="IsDirectory" ignoreCase="false" />
1712 <action type="None" />
1714 <rule name="wordpress - Rule 4" stopProcessing="true">
1715 <match url="." ignoreCase="false" />
1716 <action type="Rewrite" url="index.php" />
1720 <rule name="wordpress - Rule 1" stopProcessing="true">
1721 <match url="^index\.php$" ignoreCase="false" />
1722 <action type="None" />
1724 <rule name="wordpress - Rule 2" stopProcessing="true">
1725 <match url="^([_0-9a-zA-Z-]+/)?files/(.+)" ignoreCase="false" />
1726 <action type="Rewrite" url="wp-includes/ms-files.php?file={R:2}" appendQueryString="false" />
1728 <rule name="wordpress - Rule 3" stopProcessing="true">
1729 <match url="^([_0-9a-zA-Z-]+/)?wp-admin$" ignoreCase="false" />
1730 <action type="Redirect" url="{R:1}wp-admin/" redirectType="Permanent" />
1732 <rule name="wordpress - Rule 4" stopProcessing="true">
1733 <match url="^" ignoreCase="false" />
1734 <conditions logicalGrouping="MatchAny">
1735 <add input="{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="false" />
1736 <add input="{REQUEST_FILENAME}" matchType="IsDirectory" ignoreCase="false" />
1738 <action type="None" />
1740 <rule name="wordpress - Rule 5" stopProcessing="true">
1741 <match url="^([_0-9a-zA-Z-]+/)?(wp-(content|admin|includes).*)" ignoreCase="false" />
1742 <action type="Rewrite" url="{R:1}" />
1744 <rule name="wordpress - Rule 6" stopProcessing="true">
1745 <match url="^([_0-9a-zA-Z-]+/)?(.*\.php)$" ignoreCase="false" />
1746 <action type="Rewrite" url="{R:2}" />
1748 <rule name="wordpress - Rule 7" stopProcessing="true">
1749 <match url="." ignoreCase="false" />
1750 <action type="Rewrite" url="index.php" />
1754 if ( $add_parent_tags ) {
1762 $rules = apply_filters('iis7_url_rewrite_rules', $rules);
1768 * Add a straight rewrite rule.
1770 * Any value in the $after parameter that isn't 'bottom' will be placed at
1771 * the top of the rules.
1776 * @param string $regex Regular expression to match against request.
1777 * @param string $redirect URL regex redirects to when regex matches request.
1778 * @param string $after Optional, default is bottom. Location to place rule.
1780 function add_rule($regex, $redirect, $after = 'bottom') {
1781 //get everything up to the first ?
1782 $index = (strpos($redirect, '?') == false ? strlen($redirect) : strpos($redirect, '?'));
1783 $front = substr($redirect, 0, $index);
1784 if ( $front != $this->index ) { //it doesn't redirect to WP's index.php
1785 $this->add_external_rule($regex, $redirect);
1787 if ( 'bottom' == $after)
1788 $this->extra_rules = array_merge($this->extra_rules, array($regex => $redirect));
1790 $this->extra_rules_top = array_merge($this->extra_rules_top, array($regex => $redirect));
1791 //$this->extra_rules[$regex] = $redirect;
1796 * Add a rule that doesn't redirect to index.php.
1798 * Can redirect to any place.
1803 * @param string $regex Regular expression to match against request.
1804 * @param string $redirect URL regex redirects to when regex matches request.
1806 function add_external_rule($regex, $redirect) {
1807 $this->non_wp_rules[$regex] = $redirect;
1811 * Add an endpoint, like /trackback/.
1813 * To be inserted after certain URL types (specified in $places).
1818 * @param string $name Name of endpoint.
1819 * @param array $places URL types that endpoint can be used.
1821 function add_endpoint($name, $places) {
1823 $this->endpoints[] = array ( $places, $name );
1824 $wp->add_query_var($name);
1828 * Add permalink structure.
1830 * These are added along with the extra rewrite rules that are merged to the
1836 * @param string $name Name for permalink structure.
1837 * @param string $struct Permalink structure.
1838 * @param bool $with_front Prepend front base to permalink structure.
1840 function add_permastruct($name, $struct, $with_front = true, $ep_mask = EP_NONE) {
1842 $struct = $this->front . $struct;
1844 $struct = $this->root . $struct;
1845 $this->extra_permastructs[$name] = array($struct, $ep_mask);
1849 * Remove rewrite rules and then recreate rewrite rules.
1851 * Calls {@link WP_Rewrite::wp_rewrite_rules()} after removing the
1852 * 'rewrite_rules' option. If the function named 'save_mod_rewrite_rules'
1853 * exists, it will be called.
1857 * @param bool $hard Whether to update .htaccess (hard flush) or just update rewrite_rules option (soft flush). Default is true (hard).
1859 function flush_rules($hard = true) {
1860 delete_option('rewrite_rules');
1861 $this->wp_rewrite_rules();
1862 if ( $hard && function_exists('save_mod_rewrite_rules') )
1863 save_mod_rewrite_rules();
1864 if ( $hard && function_exists('iis7_save_url_rewrite_rules') )
1865 iis7_save_url_rewrite_rules();
1869 * Sets up the object's properties.
1871 * The 'use_verbose_page_rules' object property will be set to true if the
1872 * permalink structure begins with one of the following: '%postname%', '%category%',
1873 * '%tag%', or '%author%'.
1879 $this->extra_rules = $this->non_wp_rules = $this->endpoints = array();
1880 $this->permalink_structure = get_option('permalink_structure');
1881 $this->front = substr($this->permalink_structure, 0, strpos($this->permalink_structure, '%'));
1883 if ( $this->using_index_permalinks() )
1884 $this->root = $this->index . '/';
1885 unset($this->author_structure);
1886 unset($this->date_structure);
1887 unset($this->page_structure);
1888 unset($this->search_structure);
1889 unset($this->feed_structure);
1890 unset($this->comment_feed_structure);
1891 $this->use_trailing_slashes = ( '/' == substr($this->permalink_structure, -1, 1) );
1893 // Enable generic rules for pages if permalink structure doesn't begin with a wildcard.
1894 if ( preg_match("/^[^%]*%(?:postname|category|tag|author)%/", $this->permalink_structure) )
1895 $this->use_verbose_page_rules = true;
1897 $this->use_verbose_page_rules = false;
1901 * Set the main permalink structure for the blog.
1903 * Will update the 'permalink_structure' option, if there is a difference
1904 * between the current permalink structure and the parameter value. Calls
1905 * {@link WP_Rewrite::init()} after the option is updated.
1907 * Fires the 'permalink_structure_changed' action once the init call has
1908 * processed passing the old and new values
1913 * @param string $permalink_structure Permalink structure.
1915 function set_permalink_structure($permalink_structure) {
1916 if ( $permalink_structure != $this->permalink_structure ) {
1917 update_option('permalink_structure', $permalink_structure);
1919 do_action('permalink_structure_changed', $this->permalink_structure, $permalink_structure);
1924 * Set the category base for the category permalink.
1926 * Will update the 'category_base' option, if there is a difference between
1927 * the current category base and the parameter value. Calls
1928 * {@link WP_Rewrite::init()} after the option is updated.
1933 * @param string $category_base Category permalink structure base.
1935 function set_category_base($category_base) {
1936 if ( $category_base != get_option('category_base') ) {
1937 update_option('category_base', $category_base);
1943 * Set the tag base for the tag permalink.
1945 * Will update the 'tag_base' option, if there is a difference between the
1946 * current tag base and the parameter value. Calls
1947 * {@link WP_Rewrite::init()} after the option is updated.
1952 * @param string $tag_base Tag permalink structure base.
1954 function set_tag_base( $tag_base ) {
1955 if ( $tag_base != get_option( 'tag_base') ) {
1956 update_option( 'tag_base', $tag_base );
1962 * Constructor - Calls init(), which runs setup.
1967 * @return WP_Rewrite
1969 function __construct() {