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()) 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 // $url cleanup by Mark Jaquith
272 // This fixes things like #anchors, ?query=strings, missing 'www.',
273 // added 'www.', or added 'index.php/' that will mess up our WP_Query
274 // and return a false negative
276 // Get rid of the #anchor
277 $url_split = explode('#', $url);
278 $url = $url_split[0];
280 // Get rid of URL ?query=string
281 $url_split = explode('?', $url);
282 $url = $url_split[0];
284 // Add 'www.' if it is absent and should be there
285 if ( false !== strpos(home_url(), '://www.') && false === strpos($url, '://www.') )
286 $url = str_replace('://', '://www.', $url);
288 // Strip 'www.' if it is present and shouldn't be
289 if ( false === strpos(home_url(), '://www.') )
290 $url = str_replace('://www.', '://', $url);
292 // Strip 'index.php/' if we're not using path info permalinks
293 if ( !$wp_rewrite->using_index_permalinks() )
294 $url = str_replace('index.php/', '', $url);
296 if ( false !== strpos($url, home_url()) ) {
297 // Chop off http://domain.com
298 $url = str_replace(home_url(), '', $url);
300 // Chop off /path/to/blog
301 $home_path = parse_url(home_url());
302 $home_path = $home_path['path'];
303 $url = str_replace($home_path, '', $url);
306 // Trim leading and lagging slashes
307 $url = trim($url, '/');
314 $request_match = $request;
315 foreach ( (array)$rewrite as $match => $query) {
316 // If the requesting file is the anchor of the match, prepend it
318 if ( !empty($url) && ($url != $request) && (strpos($match, $url) === 0) )
319 $request_match = $url . '/' . $request;
321 if ( preg_match("!^$match!", $request_match, $matches) ) {
323 // Trim the query of everything up to the '?'.
324 $query = preg_replace("!^.+\?!", '', $query);
326 // Substitute the substring matches into the query.
327 $query = addslashes(WP_MatchesMapRegex::apply($query, $matches));
329 // Filter out non-public query vars
331 parse_str($query, $query_vars);
333 foreach ( (array) $query_vars as $key => $value ) {
334 if ( in_array($key, $wp->public_query_vars) )
335 $query[$key] = $value;
339 $query = new WP_Query($query);
340 if ( $query->is_single || $query->is_page )
341 return $query->post->ID;
350 * WordPress Rewrite Component.
352 * The WordPress Rewrite class writes the rewrite module rules to the .htaccess
353 * file. It also handles parsing the request to get the correct setup for the
354 * WordPress Query class.
356 * The Rewrite along with WP class function as a front controller for WordPress.
357 * You can add rules to trigger your page view and processing using this
358 * component. The full functionality of a front controller does not exist,
359 * meaning you can't define how the template files load based on the rewrite
366 * Default permalink structure for WordPress.
372 var $permalink_structure;
375 * Whether to add trailing slashes.
381 var $use_trailing_slashes;
384 * Customized or default category permalink base ( example.com/xx/tagname ).
393 * Customized or default tag permalink base ( example.com/xx/tagname ).
402 * Permalink request structure for categories.
408 var $category_structure;
411 * Permalink request structure for tags.
420 * Permalink author request base ( example.com/author/authorname ).
426 var $author_base = 'author';
429 * Permalink request structure for author pages.
435 var $author_structure;
438 * Permalink request structure for dates.
447 * Permalink request structure for pages.
456 * Search permalink base ( example.com/search/query ).
462 var $search_base = 'search';
465 * Permalink request structure for searches.
471 var $search_structure;
474 * Comments permalink base.
480 var $comments_base = 'comments';
483 * Feed permalink base.
489 var $feed_base = 'feed';
492 * Comments feed request structure permalink.
498 var $comments_feed_structure;
501 * Feed request structure permalink.
512 * The difference between the root property is that WordPress might be
513 * located at example/WordPress/index.php, if permalinks are turned off. The
514 * WordPress/index.php will be the front portion. If permalinks are turned
515 * on, this will most likely be empty or not set.
524 * Root URL path to WordPress (without domain).
526 * The difference between front property is that WordPress might be located
527 * at example.com/WordPress/. The root is the 'WordPress/' portion.
536 * Permalink to the home page.
542 var $index = 'index.php';
545 * Request match string.
554 * Rewrite rules to match against the request to find the redirect or query.
563 * Additional rules added external to the rewrite class.
565 * Those not generated by the class, see add_rewrite_rule().
571 var $extra_rules = array(); //
574 * Additional rules that belong at the beginning to match first.
576 * Those not generated by the class, see add_rewrite_rule().
582 var $extra_rules_top = array(); //
585 * Rules that don't redirect to WP's index.php.
587 * These rules are written to the mod_rewrite portion of the .htaccess.
593 var $non_wp_rules = array(); //
596 * Extra permalink structures.
602 var $extra_permastructs = array();
605 * Endpoints permalinks
614 * Whether to write every mod_rewrite rule for WordPress.
616 * This is off by default, turning it on might print a lot of rewrite rules
617 * to the .htaccess file.
623 var $use_verbose_rules = false;
626 * Whether to write every mod_rewrite rule for WordPress pages.
632 var $use_verbose_page_rules = true;
635 * Permalink structure search for preg_replace.
659 * Preg_replace values for the search, see {@link WP_Rewrite::$rewritecode}.
665 var $rewritereplace =
683 * Search for the query to look for replacing.
707 * Supported default feeds.
713 var $feeds = array ( 'feed', 'rdf', 'rss', 'rss2', 'atom' );
716 * Whether permalinks are being used.
718 * This can be either rewrite module or permalink in the HTTP query string.
723 * @return bool True, if permalinks are enabled.
725 function using_permalinks() {
726 return ! empty($this->permalink_structure);
730 * Whether permalinks are being used and rewrite module is not enabled.
732 * Means that permalink links are enabled and index.php is in the URL.
739 function using_index_permalinks() {
740 if ( empty($this->permalink_structure) )
743 // If the index is not in the permalink, we're using mod_rewrite.
744 if ( preg_match('#^/*' . $this->index . '#', $this->permalink_structure) )
751 * Whether permalinks are being used and rewrite module is enabled.
753 * Using permalinks and index.php is not in the URL.
760 function using_mod_rewrite_permalinks() {
761 if ( $this->using_permalinks() && ! $this->using_index_permalinks() )
768 * Index for matches for usage in preg_*() functions.
770 * The format of the string is, with empty matches property value, '$NUM'.
771 * The 'NUM' will be replaced with the value in the $number parameter. With
772 * the matches property not empty, the value of the returned string will
773 * contain that value of the matches property. The format then will be
774 * '$MATCHES[NUM]', with MATCHES as the value in the property and NUM the
775 * value of the $number parameter.
780 * @param int $number Index number.
783 function preg_index($number) {
787 if ( ! empty($this->matches) ) {
788 $match_prefix = '$' . $this->matches . '[';
792 return "$match_prefix$number$match_suffix";
796 * Retrieve all page and attachments for pages URIs.
798 * The attachments are for those that have pages as parents and will be
804 * @return array Array of page URIs as first element and attachment URIs as second element.
806 function page_uri_index() {
809 //get pages in order of hierarchy, i.e. children after parents
810 $posts = get_page_hierarchy($wpdb->get_results("SELECT ID, post_name, post_parent FROM $wpdb->posts WHERE post_type = 'page'"));
812 // If we have no pages get out quick
814 return array( array(), array() );
816 //now reverse it, because we need parents after children for rewrite rules to work properly
817 $posts = array_reverse($posts, true);
819 $page_uris = array();
820 $page_attachment_uris = array();
822 foreach ( $posts as $id => $post ) {
824 $uri = get_page_uri($id);
825 $attachments = $wpdb->get_results( $wpdb->prepare( "SELECT ID, post_name, post_parent FROM $wpdb->posts WHERE post_type = 'attachment' AND post_parent = %d", $id ));
826 if ( !empty($attachments) ) {
827 foreach ( $attachments as $attachment ) {
828 $attach_uri = get_page_uri($attachment->ID);
829 $page_attachment_uris[$attach_uri] = $attachment->ID;
833 $page_uris[$uri] = $id;
836 return array( $page_uris, $page_attachment_uris );
840 * Retrieve all of the rewrite rules for pages.
842 * If the 'use_verbose_page_rules' property is false, then there will only
843 * be a single rewrite rule for pages for those matching '%pagename%'. With
844 * the property set to true, the attachments and the pages will be added for
845 * each individual attachment URI and page URI, respectively.
852 function page_rewrite_rules() {
853 $rewrite_rules = array();
854 $page_structure = $this->get_page_permastruct();
856 if ( ! $this->use_verbose_page_rules ) {
857 $this->add_rewrite_tag('%pagename%', "(.+?)", 'pagename=');
858 $rewrite_rules = array_merge($rewrite_rules, $this->generate_rewrite_rules($page_structure, EP_PAGES));
859 return $rewrite_rules;
862 $page_uris = $this->page_uri_index();
863 $uris = $page_uris[0];
864 $attachment_uris = $page_uris[1];
866 if ( is_array( $attachment_uris ) ) {
867 foreach ( $attachment_uris as $uri => $pagename ) {
868 $this->add_rewrite_tag('%pagename%', "($uri)", 'attachment=');
869 $rewrite_rules = array_merge($rewrite_rules, $this->generate_rewrite_rules($page_structure, EP_PAGES));
872 if ( is_array( $uris ) ) {
873 foreach ( $uris as $uri => $pagename ) {
874 $this->add_rewrite_tag('%pagename%', "($uri)", 'pagename=');
875 $rewrite_rules = array_merge($rewrite_rules, $this->generate_rewrite_rules($page_structure, EP_PAGES));
879 return $rewrite_rules;
883 * Retrieve date permalink structure, with year, month, and day.
885 * The permalink structure for the date, if not set already depends on the
886 * permalink structure. It can be one of three formats. The first is year,
887 * month, day; the second is day, month, year; and the last format is month,
888 * day, year. These are matched against the permalink structure for which
889 * one is used. If none matches, then the default will be used, which is
892 * Prevents post ID and date permalinks from overlapping. In the case of
893 * post_id, the date permalink will be prepended with front permalink with
894 * 'date/' before the actual permalink to form the complete date permalink
900 * @return bool|string False on no permalink structure. Date permalink structure.
902 function get_date_permastruct() {
903 if ( isset($this->date_structure) )
904 return $this->date_structure;
906 if ( empty($this->permalink_structure) ) {
907 $this->date_structure = '';
911 // The date permalink must have year, month, and day separated by slashes.
912 $endians = array('%year%/%monthnum%/%day%', '%day%/%monthnum%/%year%', '%monthnum%/%day%/%year%');
914 $this->date_structure = '';
917 foreach ( $endians as $endian ) {
918 if ( false !== strpos($this->permalink_structure, $endian) ) {
919 $date_endian= $endian;
924 if ( empty($date_endian) )
925 $date_endian = '%year%/%monthnum%/%day%';
927 // Do not allow the date tags and %post_id% to overlap in the permalink
928 // structure. If they do, move the date tags to $front/date/.
929 $front = $this->front;
930 preg_match_all('/%.+?%/', $this->permalink_structure, $tokens);
932 foreach ( (array) $tokens[0] as $token) {
933 if ( '%post_id%' == $token && ($tok_index <= 3) ) {
934 $front = $front . 'date/';
940 $this->date_structure = $front . $date_endian;
942 return $this->date_structure;
946 * Retrieve the year permalink structure without month and day.
948 * Gets the date permalink structure and strips out the month and day
949 * permalink structures.
954 * @return bool|string False on failure. Year structure on success.
956 function get_year_permastruct() {
957 $structure = $this->get_date_permastruct($this->permalink_structure);
959 if ( empty($structure) )
962 $structure = str_replace('%monthnum%', '', $structure);
963 $structure = str_replace('%day%', '', $structure);
965 $structure = preg_replace('#/+#', '/', $structure);
971 * Retrieve the month permalink structure without day and with year.
973 * Gets the date permalink structure and strips out the day permalink
974 * structures. Keeps the year permalink structure.
979 * @return bool|string False on failure. Year/Month structure on success.
981 function get_month_permastruct() {
982 $structure = $this->get_date_permastruct($this->permalink_structure);
984 if ( empty($structure) )
987 $structure = str_replace('%day%', '', $structure);
989 $structure = preg_replace('#/+#', '/', $structure);
995 * Retrieve the day permalink structure with month and year.
997 * Keeps date permalink structure with all year, month, and day.
1002 * @return bool|string False on failure. Year/Month/Day structure on success.
1004 function get_day_permastruct() {
1005 return $this->get_date_permastruct($this->permalink_structure);
1009 * Retrieve the permalink structure for categories.
1011 * If the category_base property has no value, then the category structure
1012 * will have the front property value, followed by 'category', and finally
1013 * '%category%'. If it does, then the root property will be used, along with
1014 * the category_base property value.
1019 * @return bool|string False on failure. Category permalink structure.
1021 function get_category_permastruct() {
1022 if ( isset($this->category_structure) )
1023 return $this->category_structure;
1025 if ( empty($this->permalink_structure) ) {
1026 $this->category_structure = '';
1030 if ( empty($this->category_base) )
1031 $this->category_structure = trailingslashit( $this->front . 'category' );
1033 $this->category_structure = trailingslashit( '/' . $this->root . $this->category_base );
1035 $this->category_structure .= '%category%';
1037 return $this->category_structure;
1041 * Retrieve the permalink structure for tags.
1043 * If the tag_base property has no value, then the tag structure will have
1044 * the front property value, followed by 'tag', and finally '%tag%'. If it
1045 * does, then the root property will be used, along with the tag_base
1051 * @return bool|string False on failure. Tag permalink structure.
1053 function get_tag_permastruct() {
1054 if ( isset($this->tag_structure) )
1055 return $this->tag_structure;
1057 if ( empty($this->permalink_structure) ) {
1058 $this->tag_structure = '';
1062 if ( empty($this->tag_base) )
1063 $this->tag_structure = trailingslashit( $this->front . 'tag' );
1065 $this->tag_structure = trailingslashit( '/' . $this->root . $this->tag_base );
1067 $this->tag_structure .= '%tag%';
1069 return $this->tag_structure;
1073 * Retrieve extra permalink structure by name.
1078 * @param string $name Permalink structure name.
1079 * @return string|bool False if not found. Permalink structure string.
1081 function get_extra_permastruct($name) {
1082 if ( empty($this->permalink_structure) )
1085 if ( isset($this->extra_permastructs[$name]) )
1086 return $this->extra_permastructs[$name][0];
1092 * Retrieve the author permalink structure.
1094 * The permalink structure is front property, author base, and finally
1095 * '/%author%'. Will set the author_structure property and then return it
1096 * without attempting to set the value again.
1101 * @return string|bool False if not found. Permalink structure string.
1103 function get_author_permastruct() {
1104 if ( isset($this->author_structure) )
1105 return $this->author_structure;
1107 if ( empty($this->permalink_structure) ) {
1108 $this->author_structure = '';
1112 $this->author_structure = $this->front . $this->author_base . '/%author%';
1114 return $this->author_structure;
1118 * Retrieve the search permalink structure.
1120 * The permalink structure is root property, search base, and finally
1121 * '/%search%'. Will set the search_structure property and then return it
1122 * without attempting to set the value again.
1127 * @return string|bool False if not found. Permalink structure string.
1129 function get_search_permastruct() {
1130 if ( isset($this->search_structure) )
1131 return $this->search_structure;
1133 if ( empty($this->permalink_structure) ) {
1134 $this->search_structure = '';
1138 $this->search_structure = $this->root . $this->search_base . '/%search%';
1140 return $this->search_structure;
1144 * Retrieve the page permalink structure.
1146 * The permalink structure is root property, and '%pagename%'. Will set the
1147 * page_structure property and then return it without attempting to set the
1153 * @return string|bool False if not found. Permalink structure string.
1155 function get_page_permastruct() {
1156 if ( isset($this->page_structure) )
1157 return $this->page_structure;
1159 if (empty($this->permalink_structure)) {
1160 $this->page_structure = '';
1164 $this->page_structure = $this->root . '%pagename%';
1166 return $this->page_structure;
1170 * Retrieve the feed permalink structure.
1172 * The permalink structure is root property, feed base, and finally
1173 * '/%feed%'. Will set the feed_structure property and then return it
1174 * without attempting to set the value again.
1179 * @return string|bool False if not found. Permalink structure string.
1181 function get_feed_permastruct() {
1182 if ( isset($this->feed_structure) )
1183 return $this->feed_structure;
1185 if ( empty($this->permalink_structure) ) {
1186 $this->feed_structure = '';
1190 $this->feed_structure = $this->root . $this->feed_base . '/%feed%';
1192 return $this->feed_structure;
1196 * Retrieve the comment feed permalink structure.
1198 * The permalink structure is root property, comment base property, feed
1199 * base and finally '/%feed%'. Will set the comment_feed_structure property
1200 * and then return it without attempting to set the value again.
1205 * @return string|bool False if not found. Permalink structure string.
1207 function get_comment_feed_permastruct() {
1208 if ( isset($this->comment_feed_structure) )
1209 return $this->comment_feed_structure;
1211 if (empty($this->permalink_structure)) {
1212 $this->comment_feed_structure = '';
1216 $this->comment_feed_structure = $this->root . $this->comments_base . '/' . $this->feed_base . '/%feed%';
1218 return $this->comment_feed_structure;
1222 * Append or update tag, pattern, and query for replacement.
1224 * If the tag already exists, replace the existing pattern and query for
1225 * that tag, otherwise add the new tag, pattern, and query to the end of the
1228 * @internal What is the purpose of this function again? Need to finish long
1234 * @param string $tag Append tag to rewritecode property array.
1235 * @param string $pattern Append pattern to rewritereplace property array.
1236 * @param string $query Append query to queryreplace property array.
1238 function add_rewrite_tag($tag, $pattern, $query) {
1239 $position = array_search($tag, $this->rewritecode);
1240 if ( false !== $position && null !== $position ) {
1241 $this->rewritereplace[$position] = $pattern;
1242 $this->queryreplace[$position] = $query;
1244 $this->rewritecode[] = $tag;
1245 $this->rewritereplace[] = $pattern;
1246 $this->queryreplace[] = $query;
1251 * Generate the rules from permalink structure.
1253 * The main WP_Rewrite function for building the rewrite rule list. The
1254 * contents of the function is a mix of black magic and regular expressions,
1255 * so best just ignore the contents and move to the parameters.
1260 * @param string $permalink_structure The permalink structure.
1261 * @param int $ep_mask Optional, default is EP_NONE. Endpoint constant, see EP_* constants.
1262 * @param bool $paged Optional, default is true. Whether permalink request is paged.
1263 * @param bool $feed Optional, default is true. Whether for feed.
1264 * @param bool $forcomments Optional, default is false. Whether for comments.
1265 * @param bool $walk_dirs Optional, default is true. Whether to create list of directories to walk over.
1266 * @param bool $endpoints Optional, default is true. Whether endpoints are enabled.
1267 * @return array Rewrite rule list.
1269 function generate_rewrite_rules($permalink_structure, $ep_mask = EP_NONE, $paged = true, $feed = true, $forcomments = false, $walk_dirs = true, $endpoints = true) {
1270 //build a regex to match the feed section of URLs, something like (feed|atom|rss|rss2)/?
1272 foreach ( (array) $this->feeds as $feed_name)
1273 $feedregex2 .= $feed_name . '|';
1274 $feedregex2 = '(' . trim($feedregex2, '|') . ')/?$';
1276 //$feedregex is identical but with /feed/ added on as well, so URLs like <permalink>/feed/atom
1277 //and <permalink>/atom are both possible
1278 $feedregex = $this->feed_base . '/' . $feedregex2;
1280 //build a regex to match the trackback and page/xx parts of URLs
1281 $trackbackregex = 'trackback/?$';
1282 $pageregex = 'page/?([0-9]{1,})/?$';
1283 $commentregex = 'comment-page-([0-9]{1,})/?$';
1285 //build up an array of endpoint regexes to append => queries to append
1287 $ep_query_append = array ();
1288 foreach ( (array) $this->endpoints as $endpoint) {
1289 //match everything after the endpoint name, but allow for nothing to appear there
1290 $epmatch = $endpoint[1] . '(/(.*))?/?$';
1291 //this will be appended on to the rest of the query for each dir
1292 $epquery = '&' . $endpoint[1] . '=';
1293 $ep_query_append[$epmatch] = array ( $endpoint[0], $epquery );
1297 //get everything up to the first rewrite tag
1298 $front = substr($permalink_structure, 0, strpos($permalink_structure, '%'));
1299 //build an array of the tags (note that said array ends up being in $tokens[0])
1300 preg_match_all('/%.+?%/', $permalink_structure, $tokens);
1302 $num_tokens = count($tokens[0]);
1304 $index = $this->index; //probably 'index.php'
1305 $feedindex = $index;
1306 $trackbackindex = $index;
1307 //build a list from the rewritecode and queryreplace arrays, that will look something like
1308 //tagname=$matches[i] where i is the current $i
1309 for ( $i = 0; $i < $num_tokens; ++$i ) {
1311 $queries[$i] = $queries[$i - 1] . '&';
1315 $query_token = str_replace($this->rewritecode, $this->queryreplace, $tokens[0][$i]) . $this->preg_index($i+1);
1316 $queries[$i] .= $query_token;
1319 //get the structure, minus any cruft (stuff that isn't tags) at the front
1320 $structure = $permalink_structure;
1321 if ( $front != '/' )
1322 $structure = str_replace($front, '', $structure);
1324 //create a list of dirs to walk over, making rewrite rules for each level
1325 //so for example, a $structure of /%year%/%month%/%postname% would create
1326 //rewrite rules for /%year%/, /%year%/%month%/ and /%year%/%month%/%postname%
1327 $structure = trim($structure, '/');
1328 $dirs = $walk_dirs ? explode('/', $structure) : array( $structure );
1329 $num_dirs = count($dirs);
1331 //strip slashes from the front of $front
1332 $front = preg_replace('|^/+|', '', $front);
1334 //the main workhorse loop
1335 $post_rewrite = array();
1337 for ( $j = 0; $j < $num_dirs; ++$j ) {
1338 //get the struct for this dir, and trim slashes off the front
1339 $struct .= $dirs[$j] . '/'; //accumulate. see comment near explode('/', $structure) above
1340 $struct = ltrim($struct, '/');
1342 //replace tags with regexes
1343 $match = str_replace($this->rewritecode, $this->rewritereplace, $struct);
1345 //make a list of tags, and store how many there are in $num_toks
1346 $num_toks = preg_match_all('/%.+?%/', $struct, $toks);
1348 //get the 'tagname=$matches[i]'
1349 $query = ( isset($queries) && is_array($queries) ) ? $queries[$num_toks - 1] : '';
1351 //set up $ep_mask_specific which is used to match more specific URL types
1352 switch ( $dirs[$j] ) {
1354 $ep_mask_specific = EP_YEAR;
1357 $ep_mask_specific = EP_MONTH;
1360 $ep_mask_specific = EP_DAY;
1363 $ep_mask_specific = EP_NONE;
1366 //create query for /page/xx
1367 $pagematch = $match . $pageregex;
1368 $pagequery = $index . '?' . $query . '&paged=' . $this->preg_index($num_toks + 1);
1370 //create query for /comment-page-xx
1371 $commentmatch = $match . $commentregex;
1372 $commentquery = $index . '?' . $query . '&cpage=' . $this->preg_index($num_toks + 1);
1374 if ( get_option('page_on_front') ) {
1375 //create query for Root /comment-page-xx
1376 $rootcommentmatch = $match . $commentregex;
1377 $rootcommentquery = $index . '?' . $query . '&page_id=' . get_option('page_on_front') . '&cpage=' . $this->preg_index($num_toks + 1);
1380 //create query for /feed/(feed|atom|rss|rss2|rdf)
1381 $feedmatch = $match . $feedregex;
1382 $feedquery = $feedindex . '?' . $query . '&feed=' . $this->preg_index($num_toks + 1);
1384 //create query for /(feed|atom|rss|rss2|rdf) (see comment near creation of $feedregex)
1385 $feedmatch2 = $match . $feedregex2;
1386 $feedquery2 = $feedindex . '?' . $query . '&feed=' . $this->preg_index($num_toks + 1);
1388 //if asked to, turn the feed queries into comment feed ones
1389 if ( $forcomments ) {
1390 $feedquery .= '&withcomments=1';
1391 $feedquery2 .= '&withcomments=1';
1394 //start creating the array of rewrites for this dir
1396 if ( $feed ) //...adding on /feed/ regexes => queries
1397 $rewrite = array($feedmatch => $feedquery, $feedmatch2 => $feedquery2);
1398 if ( $paged ) //...and /page/xx ones
1399 $rewrite = array_merge($rewrite, array($pagematch => $pagequery));
1401 //only on pages with comments add ../comment-page-xx/
1402 if ( EP_PAGES & $ep_mask || EP_PERMALINK & $ep_mask )
1403 $rewrite = array_merge($rewrite, array($commentmatch => $commentquery));
1404 else if ( EP_ROOT & $ep_mask && get_option('page_on_front') )
1405 $rewrite = array_merge($rewrite, array($rootcommentmatch => $rootcommentquery));
1409 foreach ( (array) $ep_query_append as $regex => $ep) {
1410 //add the endpoints on if the mask fits
1411 if ( $ep[0] & $ep_mask || $ep[0] & $ep_mask_specific )
1412 $rewrite[$match . $regex] = $index . '?' . $query . $ep[1] . $this->preg_index($num_toks + 2);
1416 //if we've got some tags in this dir
1421 //check to see if this dir is permalink-level: i.e. the structure specifies an
1422 //individual post. Do this by checking it contains at least one of 1) post name,
1423 //2) post ID, 3) page name, 4) timestamp (year, month, day, hour, second and
1424 //minute all present). Set these flags now as we need them for the endpoints.
1425 if ( strpos($struct, '%postname%') !== false
1426 || strpos($struct, '%post_id%') !== false
1427 || strpos($struct, '%pagename%') !== false
1428 || (strpos($struct, '%year%') !== false && strpos($struct, '%monthnum%') !== false && strpos($struct, '%day%') !== false && strpos($struct, '%hour%') !== false && strpos($struct, '%minute%') !== false && strpos($struct, '%second%') !== false)
1431 if ( strpos($struct, '%pagename%') !== false )
1436 // For custom post types, we need to add on endpoints as well.
1437 foreach ( get_post_types( array('_builtin' => false ) ) as $ptype ) {
1438 if ( strpos($struct, "%$ptype%") !== false ) {
1440 $page = is_post_type_hierarchical( $ptype ); // This is for page style attachment url's
1446 //if we're creating rules for a permalink, do all the endpoints like attachments etc
1448 //create query and regex for trackback
1449 $trackbackmatch = $match . $trackbackregex;
1450 $trackbackquery = $trackbackindex . '?' . $query . '&tb=1';
1451 //trim slashes from the end of the regex for this dir
1452 $match = rtrim($match, '/');
1453 //get rid of brackets
1454 $submatchbase = str_replace( array('(', ')'), '', $match);
1456 //add a rule for at attachments, which take the form of <permalink>/some-text
1457 $sub1 = $submatchbase . '/([^/]+)/';
1458 $sub1tb = $sub1 . $trackbackregex; //add trackback regex <permalink>/trackback/...
1459 $sub1feed = $sub1 . $feedregex; //and <permalink>/feed/(atom|...)
1460 $sub1feed2 = $sub1 . $feedregex2; //and <permalink>/(feed|atom...)
1461 $sub1comment = $sub1 . $commentregex; //and <permalink>/comment-page-xx
1462 //add an ? as we don't have to match that last slash, and finally a $ so we
1463 //match to the end of the URL
1465 //add another rule to match attachments in the explicit form:
1466 //<permalink>/attachment/some-text
1467 $sub2 = $submatchbase . '/attachment/([^/]+)/';
1468 $sub2tb = $sub2 . $trackbackregex; //and add trackbacks <permalink>/attachment/trackback
1469 $sub2feed = $sub2 . $feedregex; //feeds, <permalink>/attachment/feed/(atom|...)
1470 $sub2feed2 = $sub2 . $feedregex2; //and feeds again on to this <permalink>/attachment/(feed|atom...)
1471 $sub2comment = $sub2 . $commentregex; //and <permalink>/comment-page-xx
1473 //create queries for these extra tag-ons we've just dealt with
1474 $subquery = $index . '?attachment=' . $this->preg_index(1);
1475 $subtbquery = $subquery . '&tb=1';
1476 $subfeedquery = $subquery . '&feed=' . $this->preg_index(2);
1477 $subcommentquery = $subquery . '&cpage=' . $this->preg_index(2);
1479 //do endpoints for attachments
1480 if ( !empty($endpoints) ) {
1481 foreach ( (array) $ep_query_append as $regex => $ep ) {
1482 if ( $ep[0] & EP_ATTACHMENT ) {
1483 $rewrite[$sub1 . $regex] = $subquery . $ep[1] . $this->preg_index(2);
1484 $rewrite[$sub2 . $regex] = $subquery . $ep[1] . $this->preg_index(2);
1489 //now we've finished with endpoints, finish off the $sub1 and $sub2 matches
1493 //allow URLs like <permalink>/2 for <permalink>/page/2
1494 $match = $match . '(/[0-9]+)?/?$';
1495 $query = $index . '?' . $query . '&page=' . $this->preg_index($num_toks + 1);
1496 } else { //not matching a permalink so this is a lot simpler
1497 //close the match and finalise the query
1499 $query = $index . '?' . $query;
1502 //create the final array for this dir by joining the $rewrite array (which currently
1503 //only contains rules/queries for trackback, pages etc) to the main regex/query for
1505 $rewrite = array_merge($rewrite, array($match => $query));
1507 //if we're matching a permalink, add those extras (attachments etc) on
1510 $rewrite = array_merge(array($trackbackmatch => $trackbackquery), $rewrite);
1512 //add regexes/queries for attachments, attachment trackbacks and so on
1513 if ( ! $page ) //require <permalink>/attachment/stuff form for pages because of confusion with subpages
1514 $rewrite = array_merge($rewrite, array($sub1 => $subquery, $sub1tb => $subtbquery, $sub1feed => $subfeedquery, $sub1feed2 => $subfeedquery, $sub1comment => $subcommentquery));
1515 $rewrite = array_merge(array($sub2 => $subquery, $sub2tb => $subtbquery, $sub2feed => $subfeedquery, $sub2feed2 => $subfeedquery, $sub2comment => $subcommentquery), $rewrite);
1518 //add the rules for this dir to the accumulating $post_rewrite
1519 $post_rewrite = array_merge($rewrite, $post_rewrite);
1521 return $post_rewrite; //the finished rules. phew!
1525 * Generate Rewrite rules with permalink structure and walking directory only.
1527 * Shorten version of {@link WP_Rewrite::generate_rewrite_rules()} that
1528 * allows for shorter list of parameters. See the method for longer
1529 * description of what generating rewrite rules does.
1531 * @uses WP_Rewrite::generate_rewrite_rules() See for long description and rest of parameters.
1535 * @param string $permalink_structure The permalink structure to generate rules.
1536 * @param bool $walk_dirs Optional, default is false. Whether to create list of directories to walk over.
1539 function generate_rewrite_rule($permalink_structure, $walk_dirs = false) {
1540 return $this->generate_rewrite_rules($permalink_structure, EP_NONE, false, false, false, $walk_dirs);
1544 * Construct rewrite matches and queries from permalink structure.
1546 * Runs the action 'generate_rewrite_rules' with the parameter that is an
1547 * reference to the current WP_Rewrite instance to further manipulate the
1548 * permalink structures and rewrite rules. Runs the 'rewrite_rules_array'
1549 * filter on the full rewrite rule array.
1551 * There are two ways to manipulate the rewrite rules, one by hooking into
1552 * the 'generate_rewrite_rules' action and gaining full control of the
1553 * object or just manipulating the rewrite rule array before it is passed
1554 * from the function.
1559 * @return array An associate array of matches and queries.
1561 function rewrite_rules() {
1564 if ( empty($this->permalink_structure) )
1567 // robots.txt -only if installed at the root
1568 $home_path = parse_url( home_url() );
1569 $robots_rewrite = ( empty( $home_path['path'] ) || '/' == $home_path['path'] ) ? array( 'robots\.txt$' => $this->index . '?robots=1' ) : array();
1571 // Default Feed rules - These are require to allow for the direct access files to work with permalink structure starting with %category%
1572 $default_feeds = array( '.*wp-atom.php$' => $this->index . '?feed=atom',
1573 '.*wp-rdf.php$' => $this->index . '?feed=rdf',
1574 '.*wp-rss.php$' => $this->index . '?feed=rss',
1575 '.*wp-rss2.php$' => $this->index . '?feed=rss2',
1576 '.*wp-feed.php$' => $this->index . '?feed=feed',
1577 '.*wp-commentsrss2.php$' => $this->index . '?feed=rss2&withcomments=1');
1579 // Registration rules
1580 $registration_pages = array();
1581 if ( is_multisite() && is_main_site() ) {
1582 $registration_pages['.*wp-signup.php$'] = $this->index . '?signup=true';
1583 $registration_pages['.*wp-activate.php$'] = $this->index . '?activate=true';
1587 $post_rewrite = $this->generate_rewrite_rules($this->permalink_structure, EP_PERMALINK);
1588 $post_rewrite = apply_filters('post_rewrite_rules', $post_rewrite);
1591 $date_rewrite = $this->generate_rewrite_rules($this->get_date_permastruct(), EP_DATE);
1592 $date_rewrite = apply_filters('date_rewrite_rules', $date_rewrite);
1595 $root_rewrite = $this->generate_rewrite_rules($this->root . '/', EP_ROOT);
1596 $root_rewrite = apply_filters('root_rewrite_rules', $root_rewrite);
1599 $comments_rewrite = $this->generate_rewrite_rules($this->root . $this->comments_base, EP_COMMENTS, true, true, true, false);
1600 $comments_rewrite = apply_filters('comments_rewrite_rules', $comments_rewrite);
1603 $search_structure = $this->get_search_permastruct();
1604 $search_rewrite = $this->generate_rewrite_rules($search_structure, EP_SEARCH);
1605 $search_rewrite = apply_filters('search_rewrite_rules', $search_rewrite);
1608 $category_rewrite = $this->generate_rewrite_rules($this->get_category_permastruct(), EP_CATEGORIES);
1609 $category_rewrite = apply_filters('category_rewrite_rules', $category_rewrite);
1612 $tag_rewrite = $this->generate_rewrite_rules($this->get_tag_permastruct(), EP_TAGS);
1613 $tag_rewrite = apply_filters('tag_rewrite_rules', $tag_rewrite);
1616 $author_rewrite = $this->generate_rewrite_rules($this->get_author_permastruct(), EP_AUTHORS);
1617 $author_rewrite = apply_filters('author_rewrite_rules', $author_rewrite);
1620 $page_rewrite = $this->page_rewrite_rules();
1621 $page_rewrite = apply_filters('page_rewrite_rules', $page_rewrite);
1623 // Extra permastructs
1624 foreach ( $this->extra_permastructs as $permastruct ) {
1625 if ( is_array($permastruct) )
1626 $this->extra_rules_top = array_merge($this->extra_rules_top, $this->generate_rewrite_rules($permastruct[0], $permastruct[1]));
1628 $this->extra_rules_top = array_merge($this->extra_rules_top, $this->generate_rewrite_rules($permastruct, EP_NONE));
1631 // Put them together.
1632 if ( $this->use_verbose_page_rules )
1633 $this->rules = array_merge($this->extra_rules_top, $robots_rewrite, $default_feeds, $registration_pages, $page_rewrite, $root_rewrite, $comments_rewrite, $search_rewrite, $category_rewrite, $tag_rewrite, $author_rewrite, $date_rewrite, $post_rewrite, $this->extra_rules);
1635 $this->rules = array_merge($this->extra_rules_top, $robots_rewrite, $default_feeds, $registration_pages, $root_rewrite, $comments_rewrite, $search_rewrite, $category_rewrite, $tag_rewrite, $author_rewrite, $date_rewrite, $post_rewrite, $page_rewrite, $this->extra_rules);
1637 do_action_ref_array('generate_rewrite_rules', array(&$this));
1638 $this->rules = apply_filters('rewrite_rules_array', $this->rules);
1640 return $this->rules;
1644 * Retrieve the rewrite rules.
1646 * The difference between this method and {@link
1647 * WP_Rewrite::rewrite_rules()} is that this method stores the rewrite rules
1648 * in the 'rewrite_rules' option and retrieves it. This prevents having to
1649 * process all of the permalinks to get the rewrite rules in the form of
1655 * @return array Rewrite rules.
1657 function wp_rewrite_rules() {
1658 $this->rules = get_option('rewrite_rules');
1659 if ( empty($this->rules) ) {
1660 $this->matches = 'matches';
1661 $this->rewrite_rules();
1662 update_option('rewrite_rules', $this->rules);
1665 return $this->rules;
1669 * Retrieve mod_rewrite formatted rewrite rules to write to .htaccess.
1671 * Does not actually write to the .htaccess file, but creates the rules for
1672 * the process that will.
1674 * Will add the non_wp_rules property rules to the .htaccess file before
1675 * the WordPress rewrite rules one.
1682 function mod_rewrite_rules() {
1683 if ( ! $this->using_permalinks() )
1686 $site_root = parse_url(get_option('siteurl'));
1687 if ( isset( $site_root['path'] ) )
1688 $site_root = trailingslashit($site_root['path']);
1690 $home_root = parse_url(home_url());
1691 if ( isset( $home_root['path'] ) )
1692 $home_root = trailingslashit($home_root['path']);
1696 $rules = "<IfModule mod_rewrite.c>\n";
1697 $rules .= "RewriteEngine On\n";
1698 $rules .= "RewriteBase $home_root\n";
1699 $rules .= "RewriteRule ^index\.php$ - [L]\n"; // Prevent -f checks on index.php.
1701 //add in the rules that don't redirect to WP's index.php (and thus shouldn't be handled by WP at all)
1702 foreach ( (array) $this->non_wp_rules as $match => $query) {
1703 // Apache 1.3 does not support the reluctant (non-greedy) modifier.
1704 $match = str_replace('.+?', '.+', $match);
1706 // If the match is unanchored and greedy, prepend rewrite conditions
1707 // to avoid infinite redirects and eclipsing of real files.
1708 //if ($match == '(.+)/?$' || $match == '([^/]+)/?$' ) {
1712 $rules .= 'RewriteRule ^' . $match . ' ' . $home_root . $query . " [QSA,L]\n";
1715 if ( $this->use_verbose_rules ) {
1716 $this->matches = '';
1717 $rewrite = $this->rewrite_rules();
1718 $num_rules = count($rewrite);
1719 $rules .= "RewriteCond %{REQUEST_FILENAME} -f [OR]\n" .
1720 "RewriteCond %{REQUEST_FILENAME} -d\n" .
1721 "RewriteRule ^.*$ - [S=$num_rules]\n";
1723 foreach ( (array) $rewrite as $match => $query) {
1724 // Apache 1.3 does not support the reluctant (non-greedy) modifier.
1725 $match = str_replace('.+?', '.+', $match);
1727 // If the match is unanchored and greedy, prepend rewrite conditions
1728 // to avoid infinite redirects and eclipsing of real files.
1729 //if ($match == '(.+)/?$' || $match == '([^/]+)/?$' ) {
1733 if ( strpos($query, $this->index) !== false )
1734 $rules .= 'RewriteRule ^' . $match . ' ' . $home_root . $query . " [QSA,L]\n";
1736 $rules .= 'RewriteRule ^' . $match . ' ' . $site_root . $query . " [QSA,L]\n";
1739 $rules .= "RewriteCond %{REQUEST_FILENAME} !-f\n" .
1740 "RewriteCond %{REQUEST_FILENAME} !-d\n" .
1741 "RewriteRule . {$home_root}{$this->index} [L]\n";
1744 $rules .= "</IfModule>\n";
1746 $rules = apply_filters('mod_rewrite_rules', $rules);
1747 $rules = apply_filters('rewrite_rules', $rules); // Deprecated
1753 * Retrieve IIS7 URL Rewrite formatted rewrite rules to write to web.config file.
1755 * Does not actually write to the web.config file, but creates the rules for
1756 * the process that will.
1763 function iis7_url_rewrite_rules( $add_parent_tags = false ) {
1765 if ( ! $this->using_permalinks() )
1768 if ( $add_parent_tags ) {
1769 $rules .= '<configuration>
1774 if ( !is_multisite() ) {
1776 <rule name="wordpress" patternSyntax="Wildcard">
1779 <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
1780 <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
1782 <action type="Rewrite" url="index.php" />
1785 if (is_subdomain_install()) {
1787 <rule name="wordpress - Rule 1" stopProcessing="true">
1788 <match url="^index\.php$" ignoreCase="false" />
1789 <action type="None" />
1791 <rule name="wordpress - Rule 2" stopProcessing="true">
1792 <match url="^files/(.+)" ignoreCase="false" />
1793 <action type="Rewrite" url="wp-includes/ms-files.php?file={R:1}" appendQueryString="false" />
1795 <rule name="wordpress - Rule 3" stopProcessing="true">
1796 <match url="^" ignoreCase="false" />
1797 <conditions logicalGrouping="MatchAny">
1798 <add input="{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="false" />
1799 <add input="{REQUEST_FILENAME}" matchType="IsDirectory" ignoreCase="false" />
1801 <action type="None" />
1803 <rule name="wordpress - Rule 4" stopProcessing="true">
1804 <match url="." ignoreCase="false" />
1805 <action type="Rewrite" url="index.php" />
1809 <rule name="wordpress - Rule 1" stopProcessing="true">
1810 <match url="^index\.php$" ignoreCase="false" />
1811 <action type="None" />
1813 <rule name="wordpress - Rule 2" stopProcessing="true">
1814 <match url="^([_0-9a-zA-Z-]+/)?files/(.+)" ignoreCase="false" />
1815 <action type="Rewrite" url="wp-includes/ms-files.php?file={R:2}" appendQueryString="false" />
1817 <rule name="wordpress - Rule 3" stopProcessing="true">
1818 <match url="^([_0-9a-zA-Z-]+/)?wp-admin$" ignoreCase="false" />
1819 <action type="Redirect" url="{R:1}wp-admin/" redirectType="Permanent" />
1821 <rule name="wordpress - Rule 4" stopProcessing="true">
1822 <match url="^" ignoreCase="false" />
1823 <conditions logicalGrouping="MatchAny">
1824 <add input="{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="false" />
1825 <add input="{REQUEST_FILENAME}" matchType="IsDirectory" ignoreCase="false" />
1827 <action type="None" />
1829 <rule name="wordpress - Rule 5" stopProcessing="true">
1830 <match url="^([_0-9a-zA-Z-]+/)?(wp-(content|admin|includes).*)" ignoreCase="false" />
1831 <action type="Rewrite" url="{R:2}" />
1833 <rule name="wordpress - Rule 6" stopProcessing="true">
1834 <match url="^([_0-9a-zA-Z-]+/)?(.*\.php)$" ignoreCase="false" />
1835 <action type="Rewrite" url="{R:2}" />
1837 <rule name="wordpress - Rule 7" stopProcessing="true">
1838 <match url="." ignoreCase="false" />
1839 <action type="Rewrite" url="index.php" />
1843 if ( $add_parent_tags ) {
1851 $rules = apply_filters('iis7_url_rewrite_rules', $rules);
1857 * Add a straight rewrite rule.
1859 * Any value in the $after parameter that isn't 'bottom' will be placed at
1860 * the top of the rules.
1865 * @param string $regex Regular expression to match against request.
1866 * @param string $redirect URL regex redirects to when regex matches request.
1867 * @param string $after Optional, default is bottom. Location to place rule.
1869 function add_rule($regex, $redirect, $after = 'bottom') {
1870 //get everything up to the first ?
1871 $index = (strpos($redirect, '?') == false ? strlen($redirect) : strpos($redirect, '?'));
1872 $front = substr($redirect, 0, $index);
1873 if ( $front != $this->index ) { //it doesn't redirect to WP's index.php
1874 $this->add_external_rule($regex, $redirect);
1876 if ( 'bottom' == $after)
1877 $this->extra_rules = array_merge($this->extra_rules, array($regex => $redirect));
1879 $this->extra_rules_top = array_merge($this->extra_rules_top, array($regex => $redirect));
1880 //$this->extra_rules[$regex] = $redirect;
1885 * Add a rule that doesn't redirect to index.php.
1887 * Can redirect to any place.
1892 * @param string $regex Regular expression to match against request.
1893 * @param string $redirect URL regex redirects to when regex matches request.
1895 function add_external_rule($regex, $redirect) {
1896 $this->non_wp_rules[$regex] = $redirect;
1900 * Add an endpoint, like /trackback/.
1902 * To be inserted after certain URL types (specified in $places).
1907 * @param string $name Name of endpoint.
1908 * @param array $places URL types that endpoint can be used.
1910 function add_endpoint($name, $places) {
1912 $this->endpoints[] = array ( $places, $name );
1913 $wp->add_query_var($name);
1917 * Add permalink structure.
1919 * These are added along with the extra rewrite rules that are merged to the
1925 * @param string $name Name for permalink structure.
1926 * @param string $struct Permalink structure.
1927 * @param bool $with_front Prepend front base to permalink structure.
1929 function add_permastruct($name, $struct, $with_front = true, $ep_mask = EP_NONE) {
1931 $struct = $this->front . $struct;
1932 $this->extra_permastructs[$name] = array($struct, $ep_mask);
1936 * Remove rewrite rules and then recreate rewrite rules.
1938 * Calls {@link WP_Rewrite::wp_rewrite_rules()} after removing the
1939 * 'rewrite_rules' option. If the function named 'save_mod_rewrite_rules'
1940 * exists, it will be called.
1944 * @param $hard bool Whether to update .htaccess (hard flush) or just update rewrite_rules option (soft flush). Default is true (hard).
1946 function flush_rules($hard = true) {
1947 delete_option('rewrite_rules');
1948 $this->wp_rewrite_rules();
1949 if ( $hard && function_exists('save_mod_rewrite_rules') )
1950 save_mod_rewrite_rules();
1951 if ( $hard && function_exists('iis7_save_url_rewrite_rules') )
1952 iis7_save_url_rewrite_rules();
1956 * Sets up the object's properties.
1958 * The 'use_verbose_page_rules' object property will be set to true if the
1959 * permalink structure begins with one of the following: '%postname%', '%category%',
1960 * '%tag%', or '%author%'.
1966 $this->extra_rules = $this->non_wp_rules = $this->endpoints = array();
1967 $this->permalink_structure = get_option('permalink_structure');
1968 $this->front = substr($this->permalink_structure, 0, strpos($this->permalink_structure, '%'));
1970 if ( $this->using_index_permalinks() )
1971 $this->root = $this->index . '/';
1972 $this->category_base = get_option( 'category_base' );
1973 $this->tag_base = get_option( 'tag_base' );
1974 unset($this->category_structure);
1975 unset($this->author_structure);
1976 unset($this->date_structure);
1977 unset($this->page_structure);
1978 unset($this->search_structure);
1979 unset($this->feed_structure);
1980 unset($this->comment_feed_structure);
1981 $this->use_trailing_slashes = ( '/' == substr($this->permalink_structure, -1, 1) );
1983 // Enable generic rules for pages if permalink structure doesn't begin with a wildcard.
1984 if ( preg_match("/^[^%]*%(?:postname|category|tag|author)%/", $this->permalink_structure) )
1985 $this->use_verbose_page_rules = true;
1987 $this->use_verbose_page_rules = false;
1991 * Set the main permalink structure for the blog.
1993 * Will update the 'permalink_structure' option, if there is a difference
1994 * between the current permalink structure and the parameter value. Calls
1995 * {@link WP_Rewrite::init()} after the option is updated.
1997 * Fires the 'permalink_structure_changed' action once the init call has
1998 * processed passing the old and new values
2003 * @param string $permalink_structure Permalink structure.
2005 function set_permalink_structure($permalink_structure) {
2006 if ( $permalink_structure != $this->permalink_structure ) {
2007 update_option('permalink_structure', $permalink_structure);
2009 do_action('permalink_structure_changed', $this->permalink_structure, $permalink_structure);
2014 * Set the category base for the category permalink.
2016 * Will update the 'category_base' option, if there is a difference between
2017 * the current category base and the parameter value. Calls
2018 * {@link WP_Rewrite::init()} after the option is updated.
2023 * @param string $category_base Category permalink structure base.
2025 function set_category_base($category_base) {
2026 if ( $category_base != $this->category_base ) {
2027 update_option('category_base', $category_base);
2033 * Set the tag base for the tag permalink.
2035 * Will update the 'tag_base' option, if there is a difference between the
2036 * current tag base and the parameter value. Calls
2037 * {@link WP_Rewrite::init()} after the option is updated.
2042 * @param string $tag_base Tag permalink structure base.
2044 function set_tag_base( $tag_base ) {
2045 if ( $tag_base != $this->tag_base ) {
2046 update_option( 'tag_base', $tag_base );
2052 * PHP4 Constructor - Calls init(), which runs setup.
2057 * @return WP_Rewrite
2059 function WP_Rewrite() {