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 rewrite tag (like %postname%).
27
* The $query parameter is optional. If it is omitted you must ensure that
28
* you call this on, or before, the 'init' hook. This is because $query defaults
29
* to "$tag=", and for this to work a new query var has to be added.
31
* @see WP_Rewrite::add_rewrite_tag()
34
* @param string $tag Name of the new rewrite tag.
35
* @param string $regex Regular expression to substitute the tag for in rewrite rules.
36
* @param string $query String to append to the rewritten query. Must end in '='. Optional.
38
function add_rewrite_tag( $tag, $regex, $query = '' ) {
39
// validate the tag's name
40
if ( strlen( $tag ) < 3 || $tag[0] != '%' || $tag[ strlen($tag) - 1 ] != '%' )
43
global $wp_rewrite, $wp;
45
if ( empty( $query ) ) {
46
$qv = trim( $tag, '%' );
47
$wp->add_query_var( $qv );
51
$wp_rewrite->add_rewrite_tag( $tag, $regex, $query );
55
* Add permalink structure.
57
* @see WP_Rewrite::add_permastruct()
60
* @param string $name Name for permalink structure.
61
* @param string $struct Permalink structure.
62
* @param array $args Optional configuration for building the rules from the permalink structure,
63
* see {@link WP_Rewrite::add_permastruct()} for full details.
65
function add_permastruct( $name, $struct, $args = array() ) {
68
// backwards compatibility for the old parameters: $with_front and $ep_mask
69
if ( ! is_array( $args ) )
70
$args = array( 'with_front' => $args );
71
if ( func_num_args() == 4 )
72
$args['ep_mask'] = func_get_arg( 3 );
74
return $wp_rewrite->add_permastruct( $name, $struct, $args );
78
* Add a new feed type like /atom1/.
82
* @param string $feedname
83
* @param callback $function Callback to run on feed display.
84
* @return string Feed action name.
86
function add_feed($feedname, $function) {
88
if ( ! in_array($feedname, $wp_rewrite->feeds) ) //override the file if it is
89
$wp_rewrite->feeds[] = $feedname;
90
$hook = 'do_feed_' . $feedname;
91
// Remove default function hook
92
remove_action($hook, $hook);
93
add_action($hook, $function, 10, 1);
98
* Remove rewrite rules and then recreate rewrite rules.
100
* @see WP_Rewrite::flush_rules()
103
* @param bool $hard Whether to update .htaccess (hard flush) or just update
104
* rewrite_rules transient (soft flush). Default is true (hard).
106
function flush_rewrite_rules( $hard = true ) {
108
$wp_rewrite->flush_rules( $hard );
112
* Endpoint Mask for default, which is nothing.
116
define('EP_NONE', 0);
119
* Endpoint Mask for Permalink.
123
define('EP_PERMALINK', 1);
126
* Endpoint Mask for Attachment.
130
define('EP_ATTACHMENT', 2);
133
* Endpoint Mask for date.
137
define('EP_DATE', 4);
140
* Endpoint Mask for year
144
define('EP_YEAR', 8);
147
* Endpoint Mask for month.
151
define('EP_MONTH', 16);
154
* Endpoint Mask for day.
158
define('EP_DAY', 32);
161
* Endpoint Mask for root.
165
define('EP_ROOT', 64);
168
* Endpoint Mask for comments.
172
define('EP_COMMENTS', 128);
175
* Endpoint Mask for searches.
179
define('EP_SEARCH', 256);
182
* Endpoint Mask for categories.
186
define('EP_CATEGORIES', 512);
189
* Endpoint Mask for tags.
193
define('EP_TAGS', 1024);
196
* Endpoint Mask for authors.
200
define('EP_AUTHORS', 2048);
203
* Endpoint Mask for pages.
207
define('EP_PAGES', 4096);
210
* Endpoint Mask for all archive views.
214
define( 'EP_ALL_ARCHIVES', EP_DATE | EP_YEAR | EP_MONTH | EP_DAY | EP_CATEGORIES | EP_TAGS | EP_AUTHORS );
217
* Endpoint Mask for everything.
221
define( 'EP_ALL', EP_PERMALINK | EP_ATTACHMENT | EP_ROOT | EP_COMMENTS | EP_SEARCH | EP_PAGES | EP_ALL_ARCHIVES );
224
* Add an endpoint, like /trackback/.
226
* Adding an endpoint creates extra rewrite rules for each of the matching
227
* places specified by the provided bitmask. For example:
230
* add_rewrite_endpoint( 'json', EP_PERMALINK | EP_PAGES );
233
* will add a new rewrite rule ending with "json(/(.*))?/?$" for every permastruct
234
* that describes a permalink (post) or page. This is rewritten to "json=$match"
235
* where $match is the part of the URL matched by the endpoint regex (e.g. "foo" in
236
* "<permalink>/json/foo/").
238
* A new query var with the same name as the endpoint will also be created.
240
* When specifying $places ensure that you are using the EP_* constants (or a
241
* combination of them using the bitwise OR operator) as their values are not
242
* guaranteed to remain static (especially EP_ALL).
244
* Be sure to flush the rewrite rules - flush_rewrite_rules() - when your plugin gets
245
* activated and deactivated.
248
* @see WP_Rewrite::add_endpoint()
249
* @global object $wp_rewrite
251
* @param string $name Name of the endpoint.
252
* @param int $places Endpoint mask describing the places the endpoint should be added.
253
* @param string $query_var Name of the corresponding query variable. Defaults to $name.
255
function add_rewrite_endpoint( $name, $places, $query_var = null ) {
257
$wp_rewrite->add_endpoint( $name, $places, $query_var );
261
* Filter the URL base for taxonomies.
263
* To remove any manually prepended /index.php/.
268
* @param string $base The taxonomy base that we're going to filter
271
function _wp_filter_taxonomy_base( $base ) {
272
if ( !empty( $base ) ) {
273
$base = preg_replace( '|^/index\.php/|', '', $base );
274
$base = trim( $base, '/' );
280
* Examine a url and try to determine the post ID it represents.
282
* Checks are supposedly from the hosted site blog.
286
* @param string $url Permalink to check.
287
* @return int Post ID, or 0 on failure.
289
function url_to_postid($url) {
293
* Filter the URL to derive the post ID from.
297
* @param string $url The URL to derive the post ID from.
299
$url = apply_filters( 'url_to_postid', $url );
301
// First, check to see if there is a 'p=N' or 'page_id=N' to match against
302
if ( preg_match('#[?&](p|page_id|attachment_id)=(\d+)#', $url, $values) ) {
303
$id = absint($values[2]);
308
// Check to see if we are using rewrite rules
309
$rewrite = $wp_rewrite->wp_rewrite_rules();
311
// Not using rewrite rules, and 'p=N' and 'page_id=N' methods failed, so we're out of options
312
if ( empty($rewrite) )
315
// Get rid of the #anchor
316
$url_split = explode('#', $url);
317
$url = $url_split[0];
319
// Get rid of URL ?query=string
320
$url_split = explode('?', $url);
321
$url = $url_split[0];
323
// Add 'www.' if it is absent and should be there
324
if ( false !== strpos(home_url(), '://www.') && false === strpos($url, '://www.') )
325
$url = str_replace('://', '://www.', $url);
327
// Strip 'www.' if it is present and shouldn't be
328
if ( false === strpos(home_url(), '://www.') )
329
$url = str_replace('://www.', '://', $url);
331
// Strip 'index.php/' if we're not using path info permalinks
332
if ( !$wp_rewrite->using_index_permalinks() )
333
$url = str_replace( $wp_rewrite->index . '/', '', $url );
335
if ( false !== strpos( trailingslashit( $url ), home_url( '/' ) ) ) {
336
// Chop off http://domain.com/[path]
337
$url = str_replace(home_url(), '', $url);
339
// Chop off /path/to/blog
340
$home_path = parse_url( home_url( '/' ) );
341
$home_path = isset( $home_path['path'] ) ? $home_path['path'] : '' ;
342
$url = preg_replace( sprintf( '#^%s#', preg_quote( $home_path ) ), '', trailingslashit( $url ) );
345
// Trim leading and lagging slashes
346
$url = trim($url, '/');
349
$post_type_query_vars = array();
351
foreach ( get_post_types( array() , 'objects' ) as $post_type => $t ) {
352
if ( ! empty( $t->query_var ) )
353
$post_type_query_vars[ $t->query_var ] = $post_type;
357
$request_match = $request;
358
foreach ( (array)$rewrite as $match => $query) {
360
// If the requesting file is the anchor of the match, prepend it
362
if ( !empty($url) && ($url != $request) && (strpos($match, $url) === 0) )
363
$request_match = $url . '/' . $request;
365
if ( preg_match("#^$match#", $request_match, $matches) ) {
367
if ( $wp_rewrite->use_verbose_page_rules && preg_match( '/pagename=\$matches\[([0-9]+)\]/', $query, $varmatch ) ) {
368
// this is a verbose page match, lets check to be sure about it
369
if ( ! get_page_by_path( $matches[ $varmatch[1] ] ) )
374
// Trim the query of everything up to the '?'.
375
$query = preg_replace("!^.+\?!", '', $query);
377
// Substitute the substring matches into the query.
378
$query = addslashes(WP_MatchesMapRegex::apply($query, $matches));
380
// Filter out non-public query vars
382
parse_str( $query, $query_vars );
384
foreach ( (array) $query_vars as $key => $value ) {
385
if ( in_array( $key, $wp->public_query_vars ) ){
386
$query[$key] = $value;
387
if ( isset( $post_type_query_vars[$key] ) ) {
388
$query['post_type'] = $post_type_query_vars[$key];
389
$query['name'] = $value;
395
$query = new WP_Query( $query );
396
if ( ! empty( $query->posts ) && $query->is_singular )
397
return $query->post->ID;
406
* WordPress Rewrite Component.
408
* The WordPress Rewrite class writes the rewrite module rules to the .htaccess
409
* file. It also handles parsing the request to get the correct setup for the
410
* WordPress Query class.
412
* The Rewrite along with WP class function as a front controller for WordPress.
413
* You can add rules to trigger your page view and processing using this
414
* component. The full functionality of a front controller does not exist,
415
* meaning you can't define how the template files load based on the rewrite
422
* Permalink structure for posts.
428
var $permalink_structure;
431
* Whether to add trailing slashes.
437
var $use_trailing_slashes;
440
* Base for the author permalink structure (example.com/$author_base/authorname).
446
var $author_base = 'author';
449
* Permalink structure for author archives.
455
var $author_structure;
458
* Permalink structure for date archives.
467
* Permalink structure for pages.
476
* Base of the search permalink structure (example.com/$search_base/query).
482
var $search_base = 'search';
485
* Permalink structure for searches.
491
var $search_structure;
494
* Comments permalink base.
500
var $comments_base = 'comments';
503
* Pagination permalink base.
509
var $pagination_base = 'page';
512
* Feed permalink base.
518
var $feed_base = 'feed';
521
* Comments feed permalink structure.
527
var $comments_feed_structure;
530
* Feed request permalink structure.
539
* The static portion of the post permalink structure.
541
* If the permalink structure is "/archive/%post_id%" then the front
542
* is "/archive/". If the permalink structure is "/%year%/%postname%/"
543
* then the front is "/".
545
* @see WP_Rewrite::init()
553
* The prefix for all permalink structures.
555
* If PATHINFO/index permalinks are in use then the root is the value of
556
* {@link WP_Rewrite::$index} with a trailing slash appended. Otherwise
557
* the root will be empty.
559
* @see WP_Rewrite::init()
560
* @see WP_Rewrite::using_index_permalinks()
568
* The name of the index file which is the entry point to all requests.
574
public $index = 'index.php';
577
* Variable name to use for regex matches in the rewritten query.
586
* Rewrite rules to match against the request to find the redirect or query.
595
* Additional rules added external to the rewrite class.
597
* Those not generated by the class, see add_rewrite_rule().
603
var $extra_rules = array();
606
* Additional rules that belong at the beginning to match first.
608
* Those not generated by the class, see add_rewrite_rule().
614
var $extra_rules_top = array();
617
* Rules that don't redirect to WordPress' index.php.
619
* These rules are written to the mod_rewrite portion of the .htaccess,
620
* and are added by {@link add_external_rule()}.
626
var $non_wp_rules = array();
629
* Extra permalink structures, e.g. categories, added by {@link add_permastruct()}.
635
var $extra_permastructs = array();
638
* Endpoints (like /trackback/) added by {@link add_rewrite_endpoint()}.
647
* Whether to write every mod_rewrite rule for WordPress into the .htaccess file.
649
* This is off by default, turning it on might print a lot of rewrite rules
650
* to the .htaccess file.
652
* @see WP_Rewrite::mod_rewrite_rules()
657
public $use_verbose_rules = false;
660
* Could post permalinks be confused with those of pages?
662
* If the first rewrite tag in the post permalink structure is one that could
663
* also match a page name (e.g. %postname% or %author%) then this flag is
664
* set to true. Prior to WordPress 3.3 this flag indicated that every page
665
* would have a set of rules added to the top of the rewrite rules array.
666
* Now it tells {@link WP::parse_request()} to check if a URL matching the
667
* page permastruct is actually a page before accepting it.
669
* @link http://core.trac.wordpress.org/ticket/16687
670
* @see WP_Rewrite::init()
675
public $use_verbose_page_rules = true;
678
* Rewrite tags that can be used in permalink structures.
680
* These are translated into the regular expressions stored in
681
* {@link WP_Rewrite::$rewritereplace} and are rewritten to the
682
* query variables listed in {@link WP_Rewrite::$queryreplace}.
684
* Additional tags can be added with {@link add_rewrite_tag()}.
690
var $rewritecode = array(
705
* Regular expressions to be substituted into rewrite rules in place
706
* of rewrite tags, see {@link WP_Rewrite::$rewritecode}.
712
var $rewritereplace = array(
727
* Query variables that rewrite tags map to, see {@link WP_Rewrite::$rewritecode}.
733
var $queryreplace = array(
748
* Supported default feeds.
754
var $feeds = array( 'feed', 'rdf', 'rss', 'rss2', 'atom' );
757
* Whether permalinks are being used.
759
* This can be either rewrite module or permalink in the HTTP query string.
764
* @return bool True, if permalinks are enabled.
766
public function using_permalinks() {
767
return ! empty($this->permalink_structure);
771
* Whether permalinks are being used and rewrite module is not enabled.
773
* Means that permalink links are enabled and index.php is in the URL.
780
public function using_index_permalinks() {
781
if ( empty($this->permalink_structure) )
784
// If the index is not in the permalink, we're using mod_rewrite.
785
if ( preg_match('#^/*' . $this->index . '#', $this->permalink_structure) )
792
* Whether permalinks are being used and rewrite module is enabled.
794
* Using permalinks and index.php is not in the URL.
801
public function using_mod_rewrite_permalinks() {
802
if ( $this->using_permalinks() && ! $this->using_index_permalinks() )
809
* Index for matches for usage in preg_*() functions.
811
* The format of the string is, with empty matches property value, '$NUM'.
812
* The 'NUM' will be replaced with the value in the $number parameter. With
813
* the matches property not empty, the value of the returned string will
814
* contain that value of the matches property. The format then will be
815
* '$MATCHES[NUM]', with MATCHES as the value in the property and NUM the
816
* value of the $number parameter.
821
* @param int $number Index number.
824
public function preg_index($number) {
828
if ( ! empty($this->matches) ) {
829
$match_prefix = '$' . $this->matches . '[';
833
return "$match_prefix$number$match_suffix";
837
* Retrieve all page and attachments for pages URIs.
839
* The attachments are for those that have pages as parents and will be
845
* @return array Array of page URIs as first element and attachment URIs as second element.
847
public function page_uri_index() {
850
//get pages in order of hierarchy, i.e. children after parents
851
$pages = $wpdb->get_results("SELECT ID, post_name, post_parent FROM $wpdb->posts WHERE post_type = 'page' AND post_status != 'auto-draft'");
852
$posts = get_page_hierarchy( $pages );
854
// If we have no pages get out quick
856
return array( array(), array() );
858
//now reverse it, because we need parents after children for rewrite rules to work properly
859
$posts = array_reverse($posts, true);
861
$page_uris = array();
862
$page_attachment_uris = array();
864
foreach ( $posts as $id => $post ) {
866
$uri = get_page_uri($id);
867
$attachments = $wpdb->get_results( $wpdb->prepare( "SELECT ID, post_name, post_parent FROM $wpdb->posts WHERE post_type = 'attachment' AND post_parent = %d", $id ));
868
if ( !empty($attachments) ) {
869
foreach ( $attachments as $attachment ) {
870
$attach_uri = get_page_uri($attachment->ID);
871
$page_attachment_uris[$attach_uri] = $attachment->ID;
875
$page_uris[$uri] = $id;
878
return array( $page_uris, $page_attachment_uris );
882
* Retrieve all of the rewrite rules for pages.
889
public function page_rewrite_rules() {
890
// the extra .? at the beginning prevents clashes with other regular expressions in the rules array
891
$this->add_rewrite_tag( '%pagename%', '(.?.+?)', 'pagename=' );
893
return $this->generate_rewrite_rules( $this->get_page_permastruct(), EP_PAGES, true, true, false, false );
897
* Retrieve date permalink structure, with year, month, and day.
899
* The permalink structure for the date, if not set already depends on the
900
* permalink structure. It can be one of three formats. The first is year,
901
* month, day; the second is day, month, year; and the last format is month,
902
* day, year. These are matched against the permalink structure for which
903
* one is used. If none matches, then the default will be used, which is
906
* Prevents post ID and date permalinks from overlapping. In the case of
907
* post_id, the date permalink will be prepended with front permalink with
908
* 'date/' before the actual permalink to form the complete date permalink
914
* @return bool|string False on no permalink structure. Date permalink structure.
916
public function get_date_permastruct() {
917
if ( isset($this->date_structure) )
918
return $this->date_structure;
920
if ( empty($this->permalink_structure) ) {
921
$this->date_structure = '';
925
// The date permalink must have year, month, and day separated by slashes.
926
$endians = array('%year%/%monthnum%/%day%', '%day%/%monthnum%/%year%', '%monthnum%/%day%/%year%');
928
$this->date_structure = '';
931
foreach ( $endians as $endian ) {
932
if ( false !== strpos($this->permalink_structure, $endian) ) {
933
$date_endian= $endian;
938
if ( empty($date_endian) )
939
$date_endian = '%year%/%monthnum%/%day%';
941
// Do not allow the date tags and %post_id% to overlap in the permalink
942
// structure. If they do, move the date tags to $front/date/.
943
$front = $this->front;
944
preg_match_all('/%.+?%/', $this->permalink_structure, $tokens);
946
foreach ( (array) $tokens[0] as $token) {
947
if ( '%post_id%' == $token && ($tok_index <= 3) ) {
948
$front = $front . 'date/';
954
$this->date_structure = $front . $date_endian;
956
return $this->date_structure;
960
* Retrieve the year permalink structure without month and day.
962
* Gets the date permalink structure and strips out the month and day
963
* permalink structures.
968
* @return bool|string False on failure. Year structure on success.
970
public function get_year_permastruct() {
971
$structure = $this->get_date_permastruct();
973
if ( empty($structure) )
976
$structure = str_replace('%monthnum%', '', $structure);
977
$structure = str_replace('%day%', '', $structure);
979
$structure = preg_replace('#/+#', '/', $structure);
985
* Retrieve the month permalink structure without day and with year.
987
* Gets the date permalink structure and strips out the day permalink
988
* structures. Keeps the year permalink structure.
993
* @return bool|string False on failure. Year/Month structure on success.
995
public function get_month_permastruct() {
996
$structure = $this->get_date_permastruct();
998
if ( empty($structure) )
1001
$structure = str_replace('%day%', '', $structure);
1003
$structure = preg_replace('#/+#', '/', $structure);
1009
* Retrieve the day permalink structure with month and year.
1011
* Keeps date permalink structure with all year, month, and day.
1016
* @return bool|string False on failure. Year/Month/Day structure on success.
1018
public function get_day_permastruct() {
1019
return $this->get_date_permastruct();
1023
* Retrieve the permalink structure for categories.
1025
* If the category_base property has no value, then the category structure
1026
* will have the front property value, followed by 'category', and finally
1027
* '%category%'. If it does, then the root property will be used, along with
1028
* the category_base property value.
1033
* @return bool|string False on failure. Category permalink structure.
1035
public function get_category_permastruct() {
1036
return $this->get_extra_permastruct('category');
1040
* Retrieve the permalink structure for tags.
1042
* If the tag_base property has no value, then the tag structure will have
1043
* the front property value, followed by 'tag', and finally '%tag%'. If it
1044
* does, then the root property will be used, along with the tag_base
1050
* @return bool|string False on failure. Tag permalink structure.
1052
public function get_tag_permastruct() {
1053
return $this->get_extra_permastruct('post_tag');
1057
* Retrieve extra permalink structure by name.
1062
* @param string $name Permalink structure name.
1063
* @return string|bool False if not found. Permalink structure string.
1065
public function get_extra_permastruct($name) {
1066
if ( empty($this->permalink_structure) )
1069
if ( isset($this->extra_permastructs[$name]) )
1070
return $this->extra_permastructs[$name]['struct'];
1076
* Retrieve the author permalink structure.
1078
* The permalink structure is front property, author base, and finally
1079
* '/%author%'. Will set the author_structure property and then return it
1080
* without attempting to set the value again.
1085
* @return string|bool False if not found. Permalink structure string.
1087
public function get_author_permastruct() {
1088
if ( isset($this->author_structure) )
1089
return $this->author_structure;
1091
if ( empty($this->permalink_structure) ) {
1092
$this->author_structure = '';
1096
$this->author_structure = $this->front . $this->author_base . '/%author%';
1098
return $this->author_structure;
1102
* Retrieve the search permalink structure.
1104
* The permalink structure is root property, search base, and finally
1105
* '/%search%'. Will set the search_structure property and then return it
1106
* without attempting to set the value again.
1111
* @return string|bool False if not found. Permalink structure string.
1113
public function get_search_permastruct() {
1114
if ( isset($this->search_structure) )
1115
return $this->search_structure;
1117
if ( empty($this->permalink_structure) ) {
1118
$this->search_structure = '';
1122
$this->search_structure = $this->root . $this->search_base . '/%search%';
1124
return $this->search_structure;
1128
* Retrieve the page permalink structure.
1130
* The permalink structure is root property, and '%pagename%'. Will set the
1131
* page_structure property and then return it without attempting to set the
1137
* @return string|bool False if not found. Permalink structure string.
1139
public function get_page_permastruct() {
1140
if ( isset($this->page_structure) )
1141
return $this->page_structure;
1143
if (empty($this->permalink_structure)) {
1144
$this->page_structure = '';
1148
$this->page_structure = $this->root . '%pagename%';
1150
return $this->page_structure;
1154
* Retrieve the feed permalink structure.
1156
* The permalink structure is root property, feed base, and finally
1157
* '/%feed%'. Will set the feed_structure property and then return it
1158
* without attempting to set the value again.
1163
* @return string|bool False if not found. Permalink structure string.
1165
public function get_feed_permastruct() {
1166
if ( isset($this->feed_structure) )
1167
return $this->feed_structure;
1169
if ( empty($this->permalink_structure) ) {
1170
$this->feed_structure = '';
1174
$this->feed_structure = $this->root . $this->feed_base . '/%feed%';
1176
return $this->feed_structure;
1180
* Retrieve the comment feed permalink structure.
1182
* The permalink structure is root property, comment base property, feed
1183
* base and finally '/%feed%'. Will set the comment_feed_structure property
1184
* and then return it without attempting to set the value again.
1189
* @return string|bool False if not found. Permalink structure string.
1191
public function get_comment_feed_permastruct() {
1192
if ( isset($this->comment_feed_structure) )
1193
return $this->comment_feed_structure;
1195
if (empty($this->permalink_structure)) {
1196
$this->comment_feed_structure = '';
1200
$this->comment_feed_structure = $this->root . $this->comments_base . '/' . $this->feed_base . '/%feed%';
1202
return $this->comment_feed_structure;
1206
* Add or update existing rewrite tags (e.g. %postname%).
1208
* If the tag already exists, replace the existing pattern and query for
1209
* that tag, otherwise add the new tag.
1211
* @see WP_Rewrite::$rewritecode
1212
* @see WP_Rewrite::$rewritereplace
1213
* @see WP_Rewrite::$queryreplace
1217
* @param string $tag Name of the rewrite tag to add or update.
1218
* @param string $regex Regular expression to substitute the tag for in rewrite rules.
1219
* @param string $query String to append to the rewritten query. Must end in '='.
1221
public function add_rewrite_tag( $tag, $regex, $query ) {
1222
$position = array_search( $tag, $this->rewritecode );
1223
if ( false !== $position && null !== $position ) {
1224
$this->rewritereplace[ $position ] = $regex;
1225
$this->queryreplace[ $position ] = $query;
1227
$this->rewritecode[] = $tag;
1228
$this->rewritereplace[] = $regex;
1229
$this->queryreplace[] = $query;
1234
* Generate rewrite rules from a permalink structure.
1236
* The main WP_Rewrite function for building the rewrite rule list. The
1237
* contents of the function is a mix of black magic and regular expressions,
1238
* so best just ignore the contents and move to the parameters.
1243
* @param string $permalink_structure The permalink structure.
1244
* @param int $ep_mask Endpoint mask defining what endpoints are added to the structure. Default is EP_NONE.
1245
* @param bool $paged Should archive pagination rules be added for the structure? Default is true.
1246
* @param bool $feed Should feed rewrite rules be added for the structure? Default is true.
1247
* @param bool $forcomments Should the feed rules be a query for a comments feed? Default is false.
1248
* @param bool $walk_dirs Should the 'directories' making up the structure be walked over and rewrite rules
1249
* built for each in turn? Default is true.
1250
* @param bool $endpoints Should endpoints be applied to the generated rewrite rules? Default is true.
1251
* @return array Rewrite rule list.
1253
public function generate_rewrite_rules($permalink_structure, $ep_mask = EP_NONE, $paged = true, $feed = true, $forcomments = false, $walk_dirs = true, $endpoints = true) {
1254
//build a regex to match the feed section of URLs, something like (feed|atom|rss|rss2)/?
1256
foreach ( (array) $this->feeds as $feed_name)
1257
$feedregex2 .= $feed_name . '|';
1258
$feedregex2 = '(' . trim($feedregex2, '|') . ')/?$';
1260
//$feedregex is identical but with /feed/ added on as well, so URLs like <permalink>/feed/atom
1261
//and <permalink>/atom are both possible
1262
$feedregex = $this->feed_base . '/' . $feedregex2;
1264
//build a regex to match the trackback and page/xx parts of URLs
1265
$trackbackregex = 'trackback/?$';
1266
$pageregex = $this->pagination_base . '/?([0-9]{1,})/?$';
1267
$commentregex = 'comment-page-([0-9]{1,})/?$';
1269
//build up an array of endpoint regexes to append => queries to append
1271
$ep_query_append = array ();
1272
foreach ( (array) $this->endpoints as $endpoint) {
1273
//match everything after the endpoint name, but allow for nothing to appear there
1274
$epmatch = $endpoint[1] . '(/(.*))?/?$';
1275
//this will be appended on to the rest of the query for each dir
1276
$epquery = '&' . $endpoint[2] . '=';
1277
$ep_query_append[$epmatch] = array ( $endpoint[0], $epquery );
1281
//get everything up to the first rewrite tag
1282
$front = substr($permalink_structure, 0, strpos($permalink_structure, '%'));
1283
//build an array of the tags (note that said array ends up being in $tokens[0])
1284
preg_match_all('/%.+?%/', $permalink_structure, $tokens);
1286
$num_tokens = count($tokens[0]);
1288
$index = $this->index; //probably 'index.php'
1289
$feedindex = $index;
1290
$trackbackindex = $index;
1291
//build a list from the rewritecode and queryreplace arrays, that will look something like
1292
//tagname=$matches[i] where i is the current $i
1293
for ( $i = 0; $i < $num_tokens; ++$i ) {
1295
$queries[$i] = $queries[$i - 1] . '&';
1299
$query_token = str_replace($this->rewritecode, $this->queryreplace, $tokens[0][$i]) . $this->preg_index($i+1);
1300
$queries[$i] .= $query_token;
1303
//get the structure, minus any cruft (stuff that isn't tags) at the front
1304
$structure = $permalink_structure;
1305
if ( $front != '/' )
1306
$structure = str_replace($front, '', $structure);
1308
//create a list of dirs to walk over, making rewrite rules for each level
1309
//so for example, a $structure of /%year%/%monthnum%/%postname% would create
1310
//rewrite rules for /%year%/, /%year%/%monthnum%/ and /%year%/%monthnum%/%postname%
1311
$structure = trim($structure, '/');
1312
$dirs = $walk_dirs ? explode('/', $structure) : array( $structure );
1313
$num_dirs = count($dirs);
1315
//strip slashes from the front of $front
1316
$front = preg_replace('|^/+|', '', $front);
1318
//the main workhorse loop
1319
$post_rewrite = array();
1321
for ( $j = 0; $j < $num_dirs; ++$j ) {
1322
//get the struct for this dir, and trim slashes off the front
1323
$struct .= $dirs[$j] . '/'; //accumulate. see comment near explode('/', $structure) above
1324
$struct = ltrim($struct, '/');
1326
//replace tags with regexes
1327
$match = str_replace($this->rewritecode, $this->rewritereplace, $struct);
1329
//make a list of tags, and store how many there are in $num_toks
1330
$num_toks = preg_match_all('/%.+?%/', $struct, $toks);
1332
//get the 'tagname=$matches[i]'
1333
$query = ( isset($queries) && is_array($queries) && !empty($num_toks) ) ? $queries[$num_toks - 1] : '';
1335
//set up $ep_mask_specific which is used to match more specific URL types
1336
switch ( $dirs[$j] ) {
1338
$ep_mask_specific = EP_YEAR;
1341
$ep_mask_specific = EP_MONTH;
1344
$ep_mask_specific = EP_DAY;
1347
$ep_mask_specific = EP_NONE;
1350
//create query for /page/xx
1351
$pagematch = $match . $pageregex;
1352
$pagequery = $index . '?' . $query . '&paged=' . $this->preg_index($num_toks + 1);
1354
//create query for /comment-page-xx
1355
$commentmatch = $match . $commentregex;
1356
$commentquery = $index . '?' . $query . '&cpage=' . $this->preg_index($num_toks + 1);
1358
if ( get_option('page_on_front') ) {
1359
//create query for Root /comment-page-xx
1360
$rootcommentmatch = $match . $commentregex;
1361
$rootcommentquery = $index . '?' . $query . '&page_id=' . get_option('page_on_front') . '&cpage=' . $this->preg_index($num_toks + 1);
1364
//create query for /feed/(feed|atom|rss|rss2|rdf)
1365
$feedmatch = $match . $feedregex;
1366
$feedquery = $feedindex . '?' . $query . '&feed=' . $this->preg_index($num_toks + 1);
1368
//create query for /(feed|atom|rss|rss2|rdf) (see comment near creation of $feedregex)
1369
$feedmatch2 = $match . $feedregex2;
1370
$feedquery2 = $feedindex . '?' . $query . '&feed=' . $this->preg_index($num_toks + 1);
1372
//if asked to, turn the feed queries into comment feed ones
1373
if ( $forcomments ) {
1374
$feedquery .= '&withcomments=1';
1375
$feedquery2 .= '&withcomments=1';
1378
//start creating the array of rewrites for this dir
1380
if ( $feed ) //...adding on /feed/ regexes => queries
1381
$rewrite = array($feedmatch => $feedquery, $feedmatch2 => $feedquery2);
1382
if ( $paged ) //...and /page/xx ones
1383
$rewrite = array_merge($rewrite, array($pagematch => $pagequery));
1385
//only on pages with comments add ../comment-page-xx/
1386
if ( EP_PAGES & $ep_mask || EP_PERMALINK & $ep_mask )
1387
$rewrite = array_merge($rewrite, array($commentmatch => $commentquery));
1388
else if ( EP_ROOT & $ep_mask && get_option('page_on_front') )
1389
$rewrite = array_merge($rewrite, array($rootcommentmatch => $rootcommentquery));
1393
foreach ( (array) $ep_query_append as $regex => $ep) {
1394
//add the endpoints on if the mask fits
1395
if ( $ep[0] & $ep_mask || $ep[0] & $ep_mask_specific )
1396
$rewrite[$match . $regex] = $index . '?' . $query . $ep[1] . $this->preg_index($num_toks + 2);
1400
//if we've got some tags in this dir
1405
//check to see if this dir is permalink-level: i.e. the structure specifies an
1406
//individual post. Do this by checking it contains at least one of 1) post name,
1407
//2) post ID, 3) page name, 4) timestamp (year, month, day, hour, second and
1408
//minute all present). Set these flags now as we need them for the endpoints.
1409
if ( strpos($struct, '%postname%') !== false
1410
|| strpos($struct, '%post_id%') !== false
1411
|| strpos($struct, '%pagename%') !== false
1412
|| (strpos($struct, '%year%') !== false && strpos($struct, '%monthnum%') !== false && strpos($struct, '%day%') !== false && strpos($struct, '%hour%') !== false && strpos($struct, '%minute%') !== false && strpos($struct, '%second%') !== false)
1415
if ( strpos($struct, '%pagename%') !== false )
1420
// For custom post types, we need to add on endpoints as well.
1421
foreach ( get_post_types( array('_builtin' => false ) ) as $ptype ) {
1422
if ( strpos($struct, "%$ptype%") !== false ) {
1424
$page = is_post_type_hierarchical( $ptype ); // This is for page style attachment url's
1430
//if we're creating rules for a permalink, do all the endpoints like attachments etc
1432
//create query and regex for trackback
1433
$trackbackmatch = $match . $trackbackregex;
1434
$trackbackquery = $trackbackindex . '?' . $query . '&tb=1';
1435
//trim slashes from the end of the regex for this dir
1436
$match = rtrim($match, '/');
1437
//get rid of brackets
1438
$submatchbase = str_replace( array('(', ')'), '', $match);
1440
//add a rule for at attachments, which take the form of <permalink>/some-text
1441
$sub1 = $submatchbase . '/([^/]+)/';
1442
$sub1tb = $sub1 . $trackbackregex; //add trackback regex <permalink>/trackback/...
1443
$sub1feed = $sub1 . $feedregex; //and <permalink>/feed/(atom|...)
1444
$sub1feed2 = $sub1 . $feedregex2; //and <permalink>/(feed|atom...)
1445
$sub1comment = $sub1 . $commentregex; //and <permalink>/comment-page-xx
1447
//add another rule to match attachments in the explicit form:
1448
//<permalink>/attachment/some-text
1449
$sub2 = $submatchbase . '/attachment/([^/]+)/';
1450
$sub2tb = $sub2 . $trackbackregex; //and add trackbacks <permalink>/attachment/trackback
1451
$sub2feed = $sub2 . $feedregex; //feeds, <permalink>/attachment/feed/(atom|...)
1452
$sub2feed2 = $sub2 . $feedregex2; //and feeds again on to this <permalink>/attachment/(feed|atom...)
1453
$sub2comment = $sub2 . $commentregex; //and <permalink>/comment-page-xx
1455
//create queries for these extra tag-ons we've just dealt with
1456
$subquery = $index . '?attachment=' . $this->preg_index(1);
1457
$subtbquery = $subquery . '&tb=1';
1458
$subfeedquery = $subquery . '&feed=' . $this->preg_index(2);
1459
$subcommentquery = $subquery . '&cpage=' . $this->preg_index(2);
1461
//do endpoints for attachments
1462
if ( !empty($endpoints) ) {
1463
foreach ( (array) $ep_query_append as $regex => $ep ) {
1464
if ( $ep[0] & EP_ATTACHMENT ) {
1465
$rewrite[$sub1 . $regex] = $subquery . $ep[1] . $this->preg_index(3);
1466
$rewrite[$sub2 . $regex] = $subquery . $ep[1] . $this->preg_index(3);
1471
//now we've finished with endpoints, finish off the $sub1 and $sub2 matches
1472
//add a ? as we don't have to match that last slash, and finally a $ so we
1473
//match to the end of the URL
1477
//post pagination, e.g. <permalink>/2/
1478
$match = $match . '(/[0-9]+)?/?$';
1479
$query = $index . '?' . $query . '&page=' . $this->preg_index($num_toks + 1);
1480
} else { //not matching a permalink so this is a lot simpler
1481
//close the match and finalise the query
1483
$query = $index . '?' . $query;
1486
//create the final array for this dir by joining the $rewrite array (which currently
1487
//only contains rules/queries for trackback, pages etc) to the main regex/query for
1489
$rewrite = array_merge($rewrite, array($match => $query));
1491
//if we're matching a permalink, add those extras (attachments etc) on
1494
$rewrite = array_merge(array($trackbackmatch => $trackbackquery), $rewrite);
1496
//add regexes/queries for attachments, attachment trackbacks and so on
1497
if ( ! $page ) //require <permalink>/attachment/stuff form for pages because of confusion with subpages
1498
$rewrite = array_merge($rewrite, array($sub1 => $subquery, $sub1tb => $subtbquery, $sub1feed => $subfeedquery, $sub1feed2 => $subfeedquery, $sub1comment => $subcommentquery));
1499
$rewrite = array_merge(array($sub2 => $subquery, $sub2tb => $subtbquery, $sub2feed => $subfeedquery, $sub2feed2 => $subfeedquery, $sub2comment => $subcommentquery), $rewrite);
1502
//add the rules for this dir to the accumulating $post_rewrite
1503
$post_rewrite = array_merge($rewrite, $post_rewrite);
1505
return $post_rewrite; //the finished rules. phew!
1509
* Generate Rewrite rules with permalink structure and walking directory only.
1511
* Shorten version of {@link WP_Rewrite::generate_rewrite_rules()} that
1512
* allows for shorter list of parameters. See the method for longer
1513
* description of what generating rewrite rules does.
1515
* @uses WP_Rewrite::generate_rewrite_rules() See for long description and rest of parameters.
1519
* @param string $permalink_structure The permalink structure to generate rules.
1520
* @param bool $walk_dirs Optional, default is false. Whether to create list of directories to walk over.
1523
public function generate_rewrite_rule($permalink_structure, $walk_dirs = false) {
1524
return $this->generate_rewrite_rules($permalink_structure, EP_NONE, false, false, false, $walk_dirs);
1528
* Construct rewrite matches and queries from permalink structure.
1530
* Runs the action 'generate_rewrite_rules' with the parameter that is an
1531
* reference to the current WP_Rewrite instance to further manipulate the
1532
* permalink structures and rewrite rules. Runs the 'rewrite_rules_array'
1533
* filter on the full rewrite rule array.
1535
* There are two ways to manipulate the rewrite rules, one by hooking into
1536
* the 'generate_rewrite_rules' action and gaining full control of the
1537
* object or just manipulating the rewrite rule array before it is passed
1538
* from the function.
1543
* @return array An associate array of matches and queries.
1545
public function rewrite_rules() {
1548
if ( empty($this->permalink_structure) )
1551
// robots.txt -only if installed at the root
1552
$home_path = parse_url( home_url() );
1553
$robots_rewrite = ( empty( $home_path['path'] ) || '/' == $home_path['path'] ) ? array( 'robots\.txt$' => $this->index . '?robots=1' ) : array();
1555
// Old feed and service files
1556
$deprecated_files = array(
1557
'.*wp-(atom|rdf|rss|rss2|feed|commentsrss2)\.php$' => $this->index . '?feed=old',
1558
'.*wp-app\.php(/.*)?$' => $this->index . '?error=403',
1561
// Registration rules
1562
$registration_pages = array();
1563
if ( is_multisite() && is_main_site() ) {
1564
$registration_pages['.*wp-signup.php$'] = $this->index . '?signup=true';
1565
$registration_pages['.*wp-activate.php$'] = $this->index . '?activate=true';
1567
$registration_pages['.*wp-register.php$'] = $this->index . '?register=true'; // Deprecated
1569
// Post rewrite rules.
1570
$post_rewrite = $this->generate_rewrite_rules( $this->permalink_structure, EP_PERMALINK );
1573
* Filter rewrite rules used for "post" archives.
1577
* @param array $post_rewrite The rewrite rules for posts.
1579
$post_rewrite = apply_filters( 'post_rewrite_rules', $post_rewrite );
1581
// Date rewrite rules.
1582
$date_rewrite = $this->generate_rewrite_rules($this->get_date_permastruct(), EP_DATE);
1585
* Filter rewrite rules used for date archives.
1587
* Likely date archives would include /yyyy/, /yyyy/mm/, and /yyyy/mm/dd/.
1591
* @param array $date_rewrite The rewrite rules for date archives.
1593
$date_rewrite = apply_filters( 'date_rewrite_rules', $date_rewrite );
1595
// Root-level rewrite rules.
1596
$root_rewrite = $this->generate_rewrite_rules($this->root . '/', EP_ROOT);
1599
* Filter rewrite rules used for root-level archives.
1601
* Likely root-level archives would include pagination rules for the homepage
1602
* as well as site-wide post feeds (e.g. /feed/, and /feed/atom/).
1606
* @param array $root_rewrite The root-level rewrite rules.
1608
$root_rewrite = apply_filters( 'root_rewrite_rules', $root_rewrite );
1610
// Comments rewrite rules.
1611
$comments_rewrite = $this->generate_rewrite_rules($this->root . $this->comments_base, EP_COMMENTS, false, true, true, false);
1614
* Filter rewrite rules used for comment feed archives.
1616
* Likely comments feed archives include /comments/feed/, and /comments/feed/atom/.
1620
* @param array $comments_rewrite The rewrite rules for the site-wide comments feeds.
1622
$comments_rewrite = apply_filters( 'comments_rewrite_rules', $comments_rewrite );
1624
// Search rewrite rules.
1625
$search_structure = $this->get_search_permastruct();
1626
$search_rewrite = $this->generate_rewrite_rules($search_structure, EP_SEARCH);
1629
* Filter rewrite rules used for search archives.
1631
* Likely search-related archives include /search/search+query/ as well as
1632
* pagination and feed paths for a search.
1636
* @param array $search_rewrite The rewrite rules for search queries.
1638
$search_rewrite = apply_filters( 'search_rewrite_rules', $search_rewrite );
1640
// Author rewrite rules.
1641
$author_rewrite = $this->generate_rewrite_rules($this->get_author_permastruct(), EP_AUTHORS);
1644
* Filter rewrite rules used for author archives.
1646
* Likely author archives would include /author/author-name/, as well as
1647
* pagination and feed paths for author archives.
1651
* @param array $author_rewrite The rewrite rules for author archives.
1653
$author_rewrite = apply_filters( 'author_rewrite_rules', $author_rewrite );
1655
// Pages rewrite rules.
1656
$page_rewrite = $this->page_rewrite_rules();
1659
* Filter rewrite rules used for "page" post type archives.
1663
* @param array $page_rewrite The rewrite rules for the "page" post type.
1665
$page_rewrite = apply_filters( 'page_rewrite_rules', $page_rewrite );
1667
// Extra permastructs.
1668
foreach ( $this->extra_permastructs as $permastructname => $struct ) {
1669
if ( is_array( $struct ) ) {
1670
if ( count( $struct ) == 2 )
1671
$rules = $this->generate_rewrite_rules( $struct[0], $struct[1] );
1673
$rules = $this->generate_rewrite_rules( $struct['struct'], $struct['ep_mask'], $struct['paged'], $struct['feed'], $struct['forcomments'], $struct['walk_dirs'], $struct['endpoints'] );
1675
$rules = $this->generate_rewrite_rules( $struct );
1679
* Filter rewrite rules used for individual permastructs.
1681
* The dynamic portion of the hook name, $permastructname, refers
1682
* to the name of the registered permastruct, e.g. 'post_tag' (tags),
1683
* 'category' (categories), etc.
1687
* @param array $rules The rewrite rules generated for the current permastruct.
1689
$rules = apply_filters( $permastructname . '_rewrite_rules', $rules );
1690
if ( 'post_tag' == $permastructname ) {
1693
* Filter rewrite rules used specifically for Tags.
1696
* @deprecated 3.1.0 Use 'post_tag_rewrite_rules' instead
1698
* @param array $rules The rewrite rules generated for tags.
1700
$rules = apply_filters( 'tag_rewrite_rules', $rules );
1703
$this->extra_rules_top = array_merge($this->extra_rules_top, $rules);
1706
// Put them together.
1707
if ( $this->use_verbose_page_rules )
1708
$this->rules = array_merge($this->extra_rules_top, $robots_rewrite, $deprecated_files, $registration_pages, $root_rewrite, $comments_rewrite, $search_rewrite, $author_rewrite, $date_rewrite, $page_rewrite, $post_rewrite, $this->extra_rules);
1710
$this->rules = array_merge($this->extra_rules_top, $robots_rewrite, $deprecated_files, $registration_pages, $root_rewrite, $comments_rewrite, $search_rewrite, $author_rewrite, $date_rewrite, $post_rewrite, $page_rewrite, $this->extra_rules);
1713
* Fires after the rewrite rules are generated.
1717
* @param WP_Rewrite $this Current WP_Rewrite instance, passed by reference.
1719
do_action_ref_array( 'generate_rewrite_rules', array( &$this ) );
1722
* Filter the full set of generated rewrite rules.
1726
* @param array $this->rules The compiled array of rewrite rules.
1728
$this->rules = apply_filters( 'rewrite_rules_array', $this->rules );
1730
return $this->rules;
1734
* Retrieve the rewrite rules.
1736
* The difference between this method and {@link
1737
* WP_Rewrite::rewrite_rules()} is that this method stores the rewrite rules
1738
* in the 'rewrite_rules' option and retrieves it. This prevents having to
1739
* process all of the permalinks to get the rewrite rules in the form of
1745
* @return array Rewrite rules.
1747
public function wp_rewrite_rules() {
1748
$this->rules = get_option('rewrite_rules');
1749
if ( empty($this->rules) ) {
1750
$this->matches = 'matches';
1751
$this->rewrite_rules();
1752
update_option('rewrite_rules', $this->rules);
1755
return $this->rules;
1759
* Retrieve mod_rewrite formatted rewrite rules to write to .htaccess.
1761
* Does not actually write to the .htaccess file, but creates the rules for
1762
* the process that will.
1764
* Will add the non_wp_rules property rules to the .htaccess file before
1765
* the WordPress rewrite rules one.
1772
public function mod_rewrite_rules() {
1773
if ( ! $this->using_permalinks() )
1776
$site_root = parse_url( site_url() );
1777
if ( isset( $site_root['path'] ) )
1778
$site_root = trailingslashit($site_root['path']);
1780
$home_root = parse_url(home_url());
1781
if ( isset( $home_root['path'] ) )
1782
$home_root = trailingslashit($home_root['path']);
1786
$rules = "<IfModule mod_rewrite.c>\n";
1787
$rules .= "RewriteEngine On\n";
1788
$rules .= "RewriteBase $home_root\n";
1789
$rules .= "RewriteRule ^index\.php$ - [L]\n"; // Prevent -f checks on index.php.
1791
//add in the rules that don't redirect to WP's index.php (and thus shouldn't be handled by WP at all)
1792
foreach ( (array) $this->non_wp_rules as $match => $query) {
1793
// Apache 1.3 does not support the reluctant (non-greedy) modifier.
1794
$match = str_replace('.+?', '.+', $match);
1796
// If the match is unanchored and greedy, prepend rewrite conditions
1797
// to avoid infinite redirects and eclipsing of real files.
1798
//if ($match == '(.+)/?$' || $match == '([^/]+)/?$' ) {
1802
$rules .= 'RewriteRule ^' . $match . ' ' . $home_root . $query . " [QSA,L]\n";
1805
if ( $this->use_verbose_rules ) {
1806
$this->matches = '';
1807
$rewrite = $this->rewrite_rules();
1808
$num_rules = count($rewrite);
1809
$rules .= "RewriteCond %{REQUEST_FILENAME} -f [OR]\n" .
1810
"RewriteCond %{REQUEST_FILENAME} -d\n" .
1811
"RewriteRule ^.*$ - [S=$num_rules]\n";
1813
foreach ( (array) $rewrite as $match => $query) {
1814
// Apache 1.3 does not support the reluctant (non-greedy) modifier.
1815
$match = str_replace('.+?', '.+', $match);
1817
// If the match is unanchored and greedy, prepend rewrite conditions
1818
// to avoid infinite redirects and eclipsing of real files.
1819
//if ($match == '(.+)/?$' || $match == '([^/]+)/?$' ) {
1823
if ( strpos($query, $this->index) !== false )
1824
$rules .= 'RewriteRule ^' . $match . ' ' . $home_root . $query . " [QSA,L]\n";
1826
$rules .= 'RewriteRule ^' . $match . ' ' . $site_root . $query . " [QSA,L]\n";
1829
$rules .= "RewriteCond %{REQUEST_FILENAME} !-f\n" .
1830
"RewriteCond %{REQUEST_FILENAME} !-d\n" .
1831
"RewriteRule . {$home_root}{$this->index} [L]\n";
1834
$rules .= "</IfModule>\n";
1838
* Filter the list of rewrite rules formatted for output to an .htaccess file.
1842
* @param string $rules mod_rewrite Rewrite rules formatted for .htaccess.
1844
$rules = apply_filters( 'mod_rewrite_rules', $rules );
1847
* Filter the list of rewrite rules formatted for output to an .htaccess file.
1850
* @deprecated 1.5.0 Use the mod_rewrite_rules filter instead.
1852
* @param string $rules mod_rewrite Rewrite rules formatted for .htaccess.
1854
$rules = apply_filters( 'rewrite_rules', $rules ); // Deprecated
1860
* Retrieve IIS7 URL Rewrite formatted rewrite rules to write to web.config file.
1862
* Does not actually write to the web.config file, but creates the rules for
1863
* the process that will.
1870
public function iis7_url_rewrite_rules( $add_parent_tags = false ) {
1872
if ( ! $this->using_permalinks() )
1875
if ( $add_parent_tags ) {
1876
$rules .= '<configuration>
1883
<rule name="wordpress" patternSyntax="Wildcard">
1886
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
1887
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
1889
<action type="Rewrite" url="index.php" />
1892
if ( $add_parent_tags ) {
1901
* Filter the list of rewrite rules formatted for output to a web.config.
1905
* @param string $rules Rewrite rules formatted for IIS web.config.
1907
$rules = apply_filters( 'iis7_url_rewrite_rules', $rules );
1913
* Add a straight rewrite rule.
1915
* Any value in the $after parameter that isn't 'bottom' will be placed at
1916
* the top of the rules.
1921
* @param string $regex Regular expression to match against request.
1922
* @param string $redirect URL regex redirects to when regex matches request.
1923
* @param string $after Optional, default is bottom. Location to place rule.
1925
public function add_rule($regex, $redirect, $after = 'bottom') {
1926
//get everything up to the first ?
1927
$index = (strpos($redirect, '?') == false ? strlen($redirect) : strpos($redirect, '?'));
1928
$front = substr($redirect, 0, $index);
1929
if ( $front != $this->index ) { //it doesn't redirect to WP's index.php
1930
$this->add_external_rule($regex, $redirect);
1932
if ( 'bottom' == $after)
1933
$this->extra_rules = array_merge($this->extra_rules, array($regex => $redirect));
1935
$this->extra_rules_top = array_merge($this->extra_rules_top, array($regex => $redirect));
1936
//$this->extra_rules[$regex] = $redirect;
1941
* Add a rule that doesn't redirect to index.php.
1943
* Can redirect to any place.
1948
* @param string $regex Regular expression to match against request.
1949
* @param string $redirect URL regex redirects to when regex matches request.
1951
public function add_external_rule($regex, $redirect) {
1952
$this->non_wp_rules[$regex] = $redirect;
1956
* Add an endpoint, like /trackback/.
1959
* @since 3.9.0 $query_var parameter added.
1962
* @see add_rewrite_endpoint() for full documentation.
1963
* @uses WP::add_query_var()
1965
* @param string $name Name of the endpoint.
1966
* @param int $places Endpoint mask describing the places the endpoint should be added.
1967
* @param string $query_var Name of the corresponding query variable. Default is value of $name.
1969
public function add_endpoint( $name, $places, $query_var = null ) {
1971
if ( null === $query_var ) {
1974
$this->endpoints[] = array( $places, $name, $query_var );
1975
$wp->add_query_var( $query_var );
1979
* Add a new permalink structure.
1981
* A permalink structure (permastruct) is an abstract definition of a set of rewrite rules; it
1982
* is an easy way of expressing a set of regular expressions that rewrite to a set of query strings.
1983
* The new permastruct is added to the {@link WP_Rewrite::$extra_permastructs} array. When the
1984
* rewrite rules are built by {@link WP_Rewrite::rewrite_rules()} all of these extra permastructs
1985
* are passed to {@link WP_Rewrite::generate_rewrite_rules()} which transforms them into the
1986
* regular expressions that many love to hate.
1988
* The $args parameter gives you control over how {@link WP_Rewrite::generate_rewrite_rules()}
1989
* works on the new permastruct.
1994
* @param string $name Name for permalink structure.
1995
* @param string $struct Permalink structure (e.g. category/%category%)
1996
* @param array $args Optional configuration for building the rules from the permalink structure:
1997
* - with_front (bool) - Should the structure be prepended with WP_Rewrite::$front? Default is true.
1998
* - ep_mask (int) - Endpoint mask defining what endpoints are added to the structure. Default is EP_NONE.
1999
* - paged (bool) - Should archive pagination rules be added for the structure? Default is true.
2000
* - feed (bool) - Should feed rewrite rules be added for the structure? Default is true.
2001
* - forcomments (bool) - Should the feed rules be a query for a comments feed? Default is false.
2002
* - walk_dirs (bool) - Should the 'directories' making up the structure be walked over and rewrite
2003
* rules built for each in turn? Default is true.
2004
* - endpoints (bool) - Should endpoints be applied to the generated rewrite rules? Default is true.
2006
public function add_permastruct( $name, $struct, $args = array() ) {
2007
// backwards compatibility for the old parameters: $with_front and $ep_mask
2008
if ( ! is_array( $args ) )
2009
$args = array( 'with_front' => $args );
2010
if ( func_num_args() == 4 )
2011
$args['ep_mask'] = func_get_arg( 3 );
2014
'with_front' => true,
2015
'ep_mask' => EP_NONE,
2018
'forcomments' => false,
2019
'walk_dirs' => true,
2020
'endpoints' => true,
2022
$args = array_intersect_key( $args, $defaults );
2023
$args = wp_parse_args( $args, $defaults );
2025
if ( $args['with_front'] )
2026
$struct = $this->front . $struct;
2028
$struct = $this->root . $struct;
2029
$args['struct'] = $struct;
2031
$this->extra_permastructs[ $name ] = $args;
2035
* Remove rewrite rules and then recreate rewrite rules.
2037
* Calls {@link WP_Rewrite::wp_rewrite_rules()} after removing the
2038
* 'rewrite_rules' option. If the function named 'save_mod_rewrite_rules'
2039
* exists, it will be called.
2043
* @param bool $hard Whether to update .htaccess (hard flush) or just update rewrite_rules option (soft flush). Default is true (hard).
2045
public function flush_rules($hard = true) {
2046
delete_option('rewrite_rules');
2047
$this->wp_rewrite_rules();
2049
* Filter whether a "hard" rewrite rule flush should be performed when requested.
2051
* A "hard" flush updates .htaccess (Apache) or web.config (IIS).
2055
* @param bool $hard Whether to flush rewrite rules "hard". Default true.
2057
if ( ! $hard || ! apply_filters( 'flush_rewrite_rules_hard', true ) ) {
2060
if ( function_exists( 'save_mod_rewrite_rules' ) )
2061
save_mod_rewrite_rules();
2062
if ( function_exists( 'iis7_save_url_rewrite_rules' ) )
2063
iis7_save_url_rewrite_rules();
2067
* Sets up the object's properties.
2069
* The 'use_verbose_page_rules' object property will be set to true if the
2070
* permalink structure begins with one of the following: '%postname%', '%category%',
2071
* '%tag%', or '%author%'.
2076
public function init() {
2077
$this->extra_rules = $this->non_wp_rules = $this->endpoints = array();
2078
$this->permalink_structure = get_option('permalink_structure');
2079
$this->front = substr($this->permalink_structure, 0, strpos($this->permalink_structure, '%'));
2081
if ( $this->using_index_permalinks() )
2082
$this->root = $this->index . '/';
2083
unset($this->author_structure);
2084
unset($this->date_structure);
2085
unset($this->page_structure);
2086
unset($this->search_structure);
2087
unset($this->feed_structure);
2088
unset($this->comment_feed_structure);
2089
$this->use_trailing_slashes = ( '/' == substr($this->permalink_structure, -1, 1) );
2091
// Enable generic rules for pages if permalink structure doesn't begin with a wildcard.
2092
if ( preg_match("/^[^%]*%(?:postname|category|tag|author)%/", $this->permalink_structure) )
2093
$this->use_verbose_page_rules = true;
2095
$this->use_verbose_page_rules = false;
2099
* Set the main permalink structure for the blog.
2101
* Will update the 'permalink_structure' option, if there is a difference
2102
* between the current permalink structure and the parameter value. Calls
2103
* {@link WP_Rewrite::init()} after the option is updated.
2105
* Fires the 'permalink_structure_changed' action once the init call has
2106
* processed passing the old and new values
2111
* @param string $permalink_structure Permalink structure.
2113
public function set_permalink_structure($permalink_structure) {
2114
if ( $permalink_structure != $this->permalink_structure ) {
2115
$old_permalink_structure = $this->permalink_structure;
2116
update_option('permalink_structure', $permalink_structure);
2120
* Fires after the permalink structure is updated.
2124
* @param string $old_permalink_structure The previous permalink structure.
2125
* @param string $permalink_structure The new permalink structure.
2127
do_action( 'permalink_structure_changed', $old_permalink_structure, $permalink_structure );
2132
* Set the category base for the category permalink.
2134
* Will update the 'category_base' option, if there is a difference between
2135
* the current category base and the parameter value. Calls
2136
* {@link WP_Rewrite::init()} after the option is updated.
2141
* @param string $category_base Category permalink structure base.
2143
public function set_category_base($category_base) {
2144
if ( $category_base != get_option('category_base') ) {
2145
update_option('category_base', $category_base);
2151
* Set the tag base for the tag permalink.
2153
* Will update the 'tag_base' option, if there is a difference between the
2154
* current tag base and the parameter value. Calls
2155
* {@link WP_Rewrite::init()} after the option is updated.
2160
* @param string $tag_base Tag permalink structure base.
2162
public function set_tag_base( $tag_base ) {
2163
if ( $tag_base != get_option( 'tag_base') ) {
2164
update_option( 'tag_base', $tag_base );
2170
* Constructor - Calls init(), which runs setup.
2175
* @return WP_Rewrite
2177
public function __construct() {