首先这里是我电脑上打印出来的 Rewrite 实例。有部分规则是我安装的插件生成的。php
在插件中添加下面代码html
1 add_action('get_footer',"temp_output"); 2 function temp_output(){ 3 global $wp_rewrite; 4 $temp = $wp_rewrite; 5 echo "#kais7mg#"; 6 var_dump($temp); 7 }
而后随便查看一篇文章就能够看到 本身WordPress中的 WP_Rewrite 实例。web
1 object(WP_Rewrite)[1095] 2 public 'permalink_structure' => string '/%year%/%monthnum%/%day%/%post_id%/' (length=35) 3 public 'use_trailing_slashes' => boolean true 4 public 'author_base' => string 'author' (length=6) 5 public 'search_base' => string 'search' (length=6) 6 public 'comments_base' => string 'comments' (length=8) 7 public 'pagination_base' => string 'page' (length=4) 8 public 'comments_pagination_base' => string 'comment-page' (length=12) 9 public 'feed_base' => string 'feed' (length=4) 10 public 'front' => string '/' (length=1) 11 public 'root' => string '' (length=0) 12 public 'index' => string 'index.php' (length=9) 13 public 'matches' => string '' (length=0) 14 public 'rules' => 15 array 16 '^wp-json/?$' => string 'index.php?rest_route=/' (length=22) 17 '^wp-json/(.*)?' => string 'index.php?rest_route=/$matches[1]' (length=33) 18 '^index.php/wp-json/?$' => string 'index.php?rest_route=/' (length=22) 19 '^index.php/wp-json/(.*)?' => string 'index.php?rest_route=/$matches[1]' (length=33) 20 'rooms/?$' => string 'index.php?post_type=hb_room' (length=27) 21 'rooms/feed/(feed|rdf|rss|rss2|atom)/?$' => string 'index.php?post_type=hb_room&feed=$matches[1]' (length=44) 22 'rooms/(feed|rdf|rss|rss2|atom)/?$' => string 'index.php?post_type=hb_room&feed=$matches[1]' (length=44) 23 'rooms/page/([0-9]{1,})/?$' => string 'index.php?post_type=hb_room&paged=$matches[1]' (length=45) 24 'hb_extra_room/?$' => string 'index.php?post_type=hb_extra_room' (length=33) 25 'hb_extra_room/feed/(feed|rdf|rss|rss2|atom)/?$' => string 'index.php?post_type=hb_extra_room&feed=$matches[1]' (length=50) 26 'hb_extra_room/(feed|rdf|rss|rss2|atom)/?$' => string 'index.php?post_type=hb_extra_room&feed=$matches[1]' (length=50) 27 'hb_extra_room/page/([0-9]{1,})/?$' => string 'index.php?post_type=hb_extra_room&paged=$matches[1]' (length=51) 28 'category/(.+?)/feed/(feed|rdf|rss|rss2|atom)/?$' => string 'index.php?category_name=$matches[1]&feed=$matches[2]' (length=52) 29 'category/(.+?)/(feed|rdf|rss|rss2|atom)/?$' => string 'index.php?category_name=$matches[1]&feed=$matches[2]' (length=52) 30 'category/(.+?)/embed/?$' => string 'index.php?category_name=$matches[1]&embed=true' (length=46) 31 'category/(.+?)/page/?([0-9]{1,})/?$' => string 'index.php?category_name=$matches[1]&paged=$matches[2]' (length=53) 32 'category/(.+?)/?$' => string 'index.php?category_name=$matches[1]' (length=35) 33 'tag/([^/]+)/feed/(feed|rdf|rss|rss2|atom)/?$' => string 'index.php?tag=$matches[1]&feed=$matches[2]' (length=42) 34 'tag/([^/]+)/(feed|rdf|rss|rss2|atom)/?$' => string 'index.php?tag=$matches[1]&feed=$matches[2]' (length=42) 35 'tag/([^/]+)/embed/?$' => string 'index.php?tag=$matches[1]&embed=true' (length=36) 36 'tag/([^/]+)/page/?([0-9]{1,})/?$' => string 'index.php?tag=$matches[1]&paged=$matches[2]' (length=43) 37 'tag/([^/]+)/?$' => string 'index.php?tag=$matches[1]' (length=25) 38 'type/([^/]+)/feed/(feed|rdf|rss|rss2|atom)/?$' => string 'index.php?post_format=$matches[1]&feed=$matches[2]' (length=50) 39 'type/([^/]+)/(feed|rdf|rss|rss2|atom)/?$' => string 'index.php?post_format=$matches[1]&feed=$matches[2]' (length=50) 40 'type/([^/]+)/embed/?$' => string 'index.php?post_format=$matches[1]&embed=true' (length=44) 41 'type/([^/]+)/page/?([0-9]{1,})/?$' => string 'index.php?post_format=$matches[1]&paged=$matches[2]' (length=51) 42 'type/([^/]+)/?$' => string 'index.php?post_format=$matches[1]' (length=33) 43 'rooms/[^/]+/attachment/([^/]+)/?$' => string 'index.php?attachment=$matches[1]' (length=32) 44 'rooms/[^/]+/attachment/([^/]+)/trackback/?$' => string 'index.php?attachment=$matches[1]&tb=1' (length=37) 45 'rooms/[^/]+/attachment/([^/]+)/feed/(feed|rdf|rss|rss2|atom)/?$' => string 'index.php?attachment=$matches[1]&feed=$matches[2]' (length=49) 46 'rooms/[^/]+/attachment/([^/]+)/(feed|rdf|rss|rss2|atom)/?$' => string 'index.php?attachment=$matches[1]&feed=$matches[2]' (length=49) 47 'rooms/[^/]+/attachment/([^/]+)/comment-page-([0-9]{1,})/?$' => string 'index.php?attachment=$matches[1]&cpage=$matches[2]' (length=50) 48 'rooms/[^/]+/attachment/([^/]+)/embed/?$' => string 'index.php?attachment=$matches[1]&embed=true' (length=43) 49 'rooms/([^/]+)/embed/?$' => string 'index.php?hb_room=$matches[1]&embed=true' (length=40) 50 'rooms/([^/]+)/trackback/?$' => string 'index.php?hb_room=$matches[1]&tb=1' (length=34) 51 'rooms/([^/]+)/feed/(feed|rdf|rss|rss2|atom)/?$' => string 'index.php?hb_room=$matches[1]&feed=$matches[2]' (length=46) 52 'rooms/([^/]+)/(feed|rdf|rss|rss2|atom)/?$' => string 'index.php?hb_room=$matches[1]&feed=$matches[2]' (length=46) 53 'rooms/([^/]+)/page/?([0-9]{1,})/?$' => string 'index.php?hb_room=$matches[1]&paged=$matches[2]' (length=47) 54 'rooms/([^/]+)/comment-page-([0-9]{1,})/?$' => string 'index.php?hb_room=$matches[1]&cpage=$matches[2]' (length=47) 55 'rooms/([^/]+)/thank-you(/(.*))?/?$' => string 'index.php?hb_room=$matches[1]&thank-you=$matches[3]' (length=51) 56 'rooms/([^/]+)(?:/([0-9]+))?/?$' => string 'index.php?hb_room=$matches[1]&page=$matches[2]' (length=46) 57 'rooms/[^/]+/([^/]+)/?$' => string 'index.php?attachment=$matches[1]' (length=32) 58 'rooms/[^/]+/([^/]+)/trackback/?$' => string 'index.php?attachment=$matches[1]&tb=1' (length=37) 59 'rooms/[^/]+/([^/]+)/feed/(feed|rdf|rss|rss2|atom)/?$' => string 'index.php?attachment=$matches[1]&feed=$matches[2]' (length=49) 60 'rooms/[^/]+/([^/]+)/(feed|rdf|rss|rss2|atom)/?$' => string 'index.php?attachment=$matches[1]&feed=$matches[2]' (length=49) 61 'rooms/[^/]+/([^/]+)/comment-page-([0-9]{1,})/?$' => string 'index.php?attachment=$matches[1]&cpage=$matches[2]' (length=50) 62 'rooms/[^/]+/([^/]+)/embed/?$' => string 'index.php?attachment=$matches[1]&embed=true' (length=43) 63 'hb_booking/[^/]+/attachment/([^/]+)/?$' => string 'index.php?attachment=$matches[1]' (length=32) 64 'hb_booking/[^/]+/attachment/([^/]+)/trackback/?$' => string 'index.php?attachment=$matches[1]&tb=1' (length=37) 65 'hb_booking/[^/]+/attachment/([^/]+)/feed/(feed|rdf|rss|rss2|atom)/?$' => string 'index.php?attachment=$matches[1]&feed=$matches[2]' (length=49) 66 'hb_booking/[^/]+/attachment/([^/]+)/(feed|rdf|rss|rss2|atom)/?$' => string 'index.php?attachment=$matches[1]&feed=$matches[2]' (length=49) 67 'hb_booking/[^/]+/attachment/([^/]+)/comment-page-([0-9]{1,})/?$' => string 'index.php?attachment=$matches[1]&cpage=$matches[2]' (length=50) 68 'hb_booking/[^/]+/attachment/([^/]+)/embed/?$' => string 'index.php?attachment=$matches[1]&embed=true' (length=43) 69 'hb_booking/([^/]+)/embed/?$' => string 'index.php?hb_booking=$matches[1]&embed=true' (length=43) 70 'hb_booking/([^/]+)/trackback/?$' => string 'index.php?hb_booking=$matches[1]&tb=1' (length=37) 71 'hb_booking/([^/]+)/page/?([0-9]{1,})/?$' => string 'index.php?hb_booking=$matches[1]&paged=$matches[2]' (length=50) 72 'hb_booking/([^/]+)/comment-page-([0-9]{1,})/?$' => string 'index.php?hb_booking=$matches[1]&cpage=$matches[2]' (length=50) 73 'hb_booking/([^/]+)/thank-you(/(.*))?/?$' => string 'index.php?hb_booking=$matches[1]&thank-you=$matches[3]' (length=54) 74 'hb_booking/([^/]+)(?:/([0-9]+))?/?$' => string 'index.php?hb_booking=$matches[1]&page=$matches[2]' (length=49) 75 'hb_booking/[^/]+/([^/]+)/?$' => string 'index.php?attachment=$matches[1]' (length=32) 76 'hb_booking/[^/]+/([^/]+)/trackback/?$' => string 'index.php?attachment=$matches[1]&tb=1' (length=37) 77 'hb_booking/[^/]+/([^/]+)/feed/(feed|rdf|rss|rss2|atom)/?$' => string 'index.php?attachment=$matches[1]&feed=$matches[2]' (length=49) 78 'hb_booking/[^/]+/([^/]+)/(feed|rdf|rss|rss2|atom)/?$' => string 'index.php?attachment=$matches[1]&feed=$matches[2]' (length=49) 79 'hb_booking/[^/]+/([^/]+)/comment-page-([0-9]{1,})/?$' => string 'index.php?attachment=$matches[1]&cpage=$matches[2]' (length=50) 80 'hb_booking/[^/]+/([^/]+)/embed/?$' => string 'index.php?attachment=$matches[1]&embed=true' (length=43) 81 'room-type/([^/]+)/feed/(feed|rdf|rss|rss2|atom)/?$' => string 'index.php?hb_room_type=$matches[1]&feed=$matches[2]' (length=51) 82 'room-type/([^/]+)/(feed|rdf|rss|rss2|atom)/?$' => string 'index.php?hb_room_type=$matches[1]&feed=$matches[2]' (length=51) 83 'room-type/([^/]+)/embed/?$' => string 'index.php?hb_room_type=$matches[1]&embed=true' (length=45) 84 'room-type/([^/]+)/page/?([0-9]{1,})/?$' => string 'index.php?hb_room_type=$matches[1]&paged=$matches[2]' (length=52) 85 'room-type/([^/]+)/?$' => string 'index.php?hb_room_type=$matches[1]' (length=34) 86 'room-capacity/([^/]+)/feed/(feed|rdf|rss|rss2|atom)/?$' => string 'index.php?hb_room_capacity=$matches[1]&feed=$matches[2]' (length=55) 87 'room-capacity/([^/]+)/(feed|rdf|rss|rss2|atom)/?$' => string 'index.php?hb_room_capacity=$matches[1]&feed=$matches[2]' (length=55) 88 'room-capacity/([^/]+)/embed/?$' => string 'index.php?hb_room_capacity=$matches[1]&embed=true' (length=49) 89 'room-capacity/([^/]+)/page/?([0-9]{1,})/?$' => string 'index.php?hb_room_capacity=$matches[1]&paged=$matches[2]' (length=56) 90 'room-capacity/([^/]+)/?$' => string 'index.php?hb_room_capacity=$matches[1]' (length=38) 91 'hb_extra_room/[^/]+/attachment/([^/]+)/?$' => string 'index.php?attachment=$matches[1]' (length=32) 92 'hb_extra_room/[^/]+/attachment/([^/]+)/trackback/?$' => string 'index.php?attachment=$matches[1]&tb=1' (length=37) 93 'hb_extra_room/[^/]+/attachment/([^/]+)/feed/(feed|rdf|rss|rss2|atom)/?$' => string 'index.php?attachment=$matches[1]&feed=$matches[2]' (length=49) 94 'hb_extra_room/[^/]+/attachment/([^/]+)/(feed|rdf|rss|rss2|atom)/?$' => string 'index.php?attachment=$matches[1]&feed=$matches[2]' (length=49) 95 'hb_extra_room/[^/]+/attachment/([^/]+)/comment-page-([0-9]{1,})/?$' => string 'index.php?attachment=$matches[1]&cpage=$matches[2]' (length=50) 96 'hb_extra_room/[^/]+/attachment/([^/]+)/embed/?$' => string 'index.php?attachment=$matches[1]&embed=true' (length=43) 97 'hb_extra_room/([^/]+)/embed/?$' => string 'index.php?hb_extra_room=$matches[1]&embed=true' (length=46) 98 'hb_extra_room/([^/]+)/trackback/?$' => string 'index.php?hb_extra_room=$matches[1]&tb=1' (length=40) 99 'hb_extra_room/([^/]+)/feed/(feed|rdf|rss|rss2|atom)/?$' => string 'index.php?hb_extra_room=$matches[1]&feed=$matches[2]' (length=52) 100 'hb_extra_room/([^/]+)/(feed|rdf|rss|rss2|atom)/?$' => string 'index.php?hb_extra_room=$matches[1]&feed=$matches[2]' (length=52) 101 'hb_extra_room/([^/]+)/page/?([0-9]{1,})/?$' => string 'index.php?hb_extra_room=$matches[1]&paged=$matches[2]' (length=53) 102 'hb_extra_room/([^/]+)/comment-page-([0-9]{1,})/?$' => string 'index.php?hb_extra_room=$matches[1]&cpage=$matches[2]' (length=53) 103 'hb_extra_room/([^/]+)/thank-you(/(.*))?/?$' => string 'index.php?hb_extra_room=$matches[1]&thank-you=$matches[3]' (length=57) 104 'hb_extra_room/([^/]+)(?:/([0-9]+))?/?$' => string 'index.php?hb_extra_room=$matches[1]&page=$matches[2]' (length=52) 105 'hb_extra_room/[^/]+/([^/]+)/?$' => string 'index.php?attachment=$matches[1]' (length=32) 106 'hb_extra_room/[^/]+/([^/]+)/trackback/?$' => string 'index.php?attachment=$matches[1]&tb=1' (length=37) 107 'hb_extra_room/[^/]+/([^/]+)/feed/(feed|rdf|rss|rss2|atom)/?$' => string 'index.php?attachment=$matches[1]&feed=$matches[2]' (length=49) 108 'hb_extra_room/[^/]+/([^/]+)/(feed|rdf|rss|rss2|atom)/?$' => string 'index.php?attachment=$matches[1]&feed=$matches[2]' (length=49) 109 'hb_extra_room/[^/]+/([^/]+)/comment-page-([0-9]{1,})/?$' => string 'index.php?attachment=$matches[1]&cpage=$matches[2]' (length=50) 110 'hb_extra_room/[^/]+/([^/]+)/embed/?$' => string 'index.php?attachment=$matches[1]&embed=true' (length=43) 111 '.*wp-(atom|rdf|rss|rss2|feed|commentsrss2)\.php$' => string 'index.php?feed=old' (length=18) 112 '.*wp-app\.php(/.*)?$' => string 'index.php?error=403' (length=19) 113 '.*wp-register.php$' => string 'index.php?register=true' (length=23) 114 'feed/(feed|rdf|rss|rss2|atom)/?$' => string 'index.php?&feed=$matches[1]' (length=27) 115 '(feed|rdf|rss|rss2|atom)/?$' => string 'index.php?&feed=$matches[1]' (length=27) 116 'embed/?$' => string 'index.php?&embed=true' (length=21) 117 'page/?([0-9]{1,})/?$' => string 'index.php?&paged=$matches[1]' (length=28) 118 'comments/feed/(feed|rdf|rss|rss2|atom)/?$' => string 'index.php?&feed=$matches[1]&withcomments=1' (length=42) 119 'comments/(feed|rdf|rss|rss2|atom)/?$' => string 'index.php?&feed=$matches[1]&withcomments=1' (length=42) 120 'comments/embed/?$' => string 'index.php?&embed=true' (length=21) 121 'search/(.+)/feed/(feed|rdf|rss|rss2|atom)/?$' => string 'index.php?s=$matches[1]&feed=$matches[2]' (length=40) 122 'search/(.+)/(feed|rdf|rss|rss2|atom)/?$' => string 'index.php?s=$matches[1]&feed=$matches[2]' (length=40) 123 'search/(.+)/embed/?$' => string 'index.php?s=$matches[1]&embed=true' (length=34) 124 'search/(.+)/page/?([0-9]{1,})/?$' => string 'index.php?s=$matches[1]&paged=$matches[2]' (length=41) 125 'search/(.+)/?$' => string 'index.php?s=$matches[1]' (length=23) 126 'author/([^/]+)/feed/(feed|rdf|rss|rss2|atom)/?$' => string 'index.php?author_name=$matches[1]&feed=$matches[2]' (length=50) 127 'author/([^/]+)/(feed|rdf|rss|rss2|atom)/?$' => string 'index.php?author_name=$matches[1]&feed=$matches[2]' (length=50) 128 'author/([^/]+)/embed/?$' => string 'index.php?author_name=$matches[1]&embed=true' (length=44) 129 'author/([^/]+)/page/?([0-9]{1,})/?$' => string 'index.php?author_name=$matches[1]&paged=$matches[2]' (length=51) 130 'author/([^/]+)/?$' => string 'index.php?author_name=$matches[1]' (length=33) 131 '([0-9]{4})/([0-9]{1,2})/([0-9]{1,2})/feed/(feed|rdf|rss|rss2|atom)/?$' => string 'index.php?year=$matches[1]&monthnum=$matches[2]&day=$matches[3]&feed=$matches[4]' (length=80) 132 '([0-9]{4})/([0-9]{1,2})/([0-9]{1,2})/(feed|rdf|rss|rss2|atom)/?$' => string 'index.php?year=$matches[1]&monthnum=$matches[2]&day=$matches[3]&feed=$matches[4]' (length=80) 133 '([0-9]{4})/([0-9]{1,2})/([0-9]{1,2})/embed/?$' => string 'index.php?year=$matches[1]&monthnum=$matches[2]&day=$matches[3]&embed=true' (length=74) 134 '([0-9]{4})/([0-9]{1,2})/([0-9]{1,2})/page/?([0-9]{1,})/?$' => string 'index.php?year=$matches[1]&monthnum=$matches[2]&day=$matches[3]&paged=$matches[4]' (length=81) 135 '([0-9]{4})/([0-9]{1,2})/([0-9]{1,2})/?$' => string 'index.php?year=$matches[1]&monthnum=$matches[2]&day=$matches[3]' (length=63) 136 '([0-9]{4})/([0-9]{1,2})/feed/(feed|rdf|rss|rss2|atom)/?$' => string 'index.php?year=$matches[1]&monthnum=$matches[2]&feed=$matches[3]' (length=64) 137 '([0-9]{4})/([0-9]{1,2})/(feed|rdf|rss|rss2|atom)/?$' => string 'index.php?year=$matches[1]&monthnum=$matches[2]&feed=$matches[3]' (length=64) 138 '([0-9]{4})/([0-9]{1,2})/embed/?$' => string 'index.php?year=$matches[1]&monthnum=$matches[2]&embed=true' (length=58) 139 '([0-9]{4})/([0-9]{1,2})/page/?([0-9]{1,})/?$' => string 'index.php?year=$matches[1]&monthnum=$matches[2]&paged=$matches[3]' (length=65) 140 '([0-9]{4})/([0-9]{1,2})/?$' => string 'index.php?year=$matches[1]&monthnum=$matches[2]' (length=47) 141 '([0-9]{4})/feed/(feed|rdf|rss|rss2|atom)/?$' => string 'index.php?year=$matches[1]&feed=$matches[2]' (length=43) 142 '([0-9]{4})/(feed|rdf|rss|rss2|atom)/?$' => string 'index.php?year=$matches[1]&feed=$matches[2]' (length=43) 143 '([0-9]{4})/embed/?$' => string 'index.php?year=$matches[1]&embed=true' (length=37) 144 more elements... 145 public 'extra_rules' => 146 array 147 empty 148 public 'extra_rules_top' => 149 array 150 '^wp-json/?$' => string 'index.php?rest_route=/' (length=22) 151 '^wp-json/(.*)?' => string 'index.php?rest_route=/$matches[1]' (length=33) 152 '^index.php/wp-json/?$' => string 'index.php?rest_route=/' (length=22) 153 '^index.php/wp-json/(.*)?' => string 'index.php?rest_route=/$matches[1]' (length=33) 154 public 'non_wp_rules' => 155 array 156 empty 157 public 'extra_permastructs' => 158 array 159 'category' => 160 array 161 'with_front' => boolean true 162 'ep_mask' => int 512 163 'paged' => boolean true 164 'feed' => boolean true 165 'forcomments' => boolean false 166 'walk_dirs' => boolean true 167 'endpoints' => boolean true 168 'struct' => string '/category/%category%' (length=20) 169 'post_tag' => 170 array 171 'with_front' => boolean true 172 'ep_mask' => int 1024 173 'paged' => boolean true 174 'feed' => boolean true 175 'forcomments' => boolean false 176 'walk_dirs' => boolean true 177 'endpoints' => boolean true 178 'struct' => string '/tag/%post_tag%' (length=15) 179 'post_format' => 180 array 181 'with_front' => boolean true 182 'ep_mask' => int 0 183 'paged' => boolean true 184 'feed' => boolean true 185 'forcomments' => boolean false 186 'walk_dirs' => boolean true 187 'endpoints' => boolean true 188 'struct' => string '/type/%post_format%' (length=19) 189 public 'endpoints' => 190 array 191 empty 192 public 'use_verbose_rules' => boolean false 193 public 'use_verbose_page_rules' => boolean false 194 public 'rewritecode' => 195 array 196 0 => string '%year%' (length=6) 197 1 => string '%monthnum%' (length=10) 198 2 => string '%day%' (length=5) 199 3 => string '%hour%' (length=6) 200 4 => string '%minute%' (length=8) 201 5 => string '%second%' (length=8) 202 6 => string '%postname%' (length=10) 203 7 => string '%post_id%' (length=9) 204 8 => string '%author%' (length=8) 205 9 => string '%pagename%' (length=10) 206 10 => string '%search%' (length=8) 207 11 => string '%category%' (length=10) 208 12 => string '%post_tag%' (length=10) 209 13 => string '%post_format%' (length=13) 210 public 'rewritereplace' => 211 array 212 0 => string '([0-9]{4})' (length=10) 213 1 => string '([0-9]{1,2})' (length=12) 214 2 => string '([0-9]{1,2})' (length=12) 215 3 => string '([0-9]{1,2})' (length=12) 216 4 => string '([0-9]{1,2})' (length=12) 217 5 => string '([0-9]{1,2})' (length=12) 218 6 => string '([^/]+)' (length=7) 219 7 => string '([0-9]+)' (length=8) 220 8 => string '([^/]+)' (length=7) 221 9 => string '([^/]+?)' (length=8) 222 10 => string '(.+)' (length=4) 223 11 => string '(.+?)' (length=5) 224 12 => string '([^/]+)' (length=7) 225 13 => string '([^/]+)' (length=7) 226 public 'queryreplace' => 227 array 228 0 => string 'year=' (length=5) 229 1 => string 'monthnum=' (length=9) 230 2 => string 'day=' (length=4) 231 3 => string 'hour=' (length=5) 232 4 => string 'minute=' (length=7) 233 5 => string 'second=' (length=7) 234 6 => string 'name=' (length=5) 235 7 => string 'p=' (length=2) 236 8 => string 'author_name=' (length=12) 237 9 => string 'pagename=' (length=9) 238 10 => string 's=' (length=2) 239 11 => string 'category_name=' (length=14) 240 12 => string 'tag=' (length=4) 241 13 => string 'post_format=' (length=12) 242 public 'feeds' => 243 array 244 0 => string 'feed' (length=4) 245 1 => string 'rdf' (length=3) 246 2 => string 'rss' (length=3) 247 3 => string 'rss2' (length=4) 248 4 => string 'atom' (length=4) 249 public 'feed_structure' => string 'feed/%feed%' (length=11) 250 public 'comment_feed_structure' => string 'comments/feed/%feed%' (length=20) 251 public 'date_structure' => string '/%year%/%monthnum%/%day%' (length=24) 252 public 'author_structure' => string '/author/%author%' (length=16)
原文地址:https://codex.wordpress.org/Function_Reference/WP_Rewrite正则表达式
This document assumes familiarity with Apache's mod_rewrite. If you've never heard of this before, try reading Sitepoint's Beginner's Guide to URL Rewriting. Also see Otto's explanation of hierarchy of rewrite rules in the wp-hackers email list.数据库
本文档假定熟悉Apache的mod_rewrite模块。若是你历来没有据说过这个,试着读SitePoint的初学者指南URL重写。还看到Otto的解释层次结构的重写规则,在WP黑客电子邮件列表。express
WP_Rewrite is WordPress' class for managing the rewrite rules that allow you to use Pretty Permalinks feature. It has several methods that generate the rewrite rules from values in the database. It is used internally when updating the rewrite rules, and also to find the URL of a specific post, Page, category archive, etc.. It's defined in wp-includes/rewrite.php as a single instance global variable, $wp_rewrite, is initialised in wp-settings.php.apache
wp_rewrite是WordPress的类管理的重写规则,容许你使用更好的永久连接功能。它有几种方法从数据库中的值生成重写规则。它在更新重写规则时在内部用,也能够找到特定的文章、页面、分类归档等的URL。它定义 wp-includes/rewrite.php做为单实例的全局变量,$wp_rewrite, 初始化在 wp-settings.php.json
This is the formal documentation of WP_Rewrite. Try not to access or set the properties directly, instead use the methods to interact with the $wp_rewrite object. See also Rewrite_API.api
这是wp_rewrite正式文件。尽可能不要访问或直接设置属性,而使用$wp_rewrite对象的方法进行交互。又见rewrite_api(中文)。数组
$permalink_structure 固定连接结构
The permalink structure as in the database. This is what you set on the Permalink Options page, and includes 'tags' like %year%, %month% and %post_id%.
数据库中的固定连接结构。你能够在选项页面去设置,包括“标签”像 %year%, %month% and %post_id%。
public 'permalink_structure' => string '/%year%/%monthnum%/%day%/%post_id%/' (length=35)
$category_base 分类的基础路径
Anything to be inserted before category archive URLs. Defaults to 'category/'.
在类别归档URL以前插入的内容。默认是 'category/'。
$category_structure 分类归档的结构
Structure for category archive URLs. This is just the $category_base plus '%category%'.
类别归档URL的结构。这仅仅是 $category_base 加上 '%category%'。
$author_base 做者的基础路径
Anything to be inserted before author archive URLs. Defaults to 'author/'.
在做者归档URL以前插入的内容。默认是 'author/'。
public 'author_base' => string 'author' (length=6)
$author_structure 做者归档的结构
Structure for author archive URLs. This is just the $author_base plus '%author%'.
做者归档URL的结构。这仅仅是 $author_base 加上 '%author%' 。
public 'author_structure' => string '/author/%author%' (length=16)
$pagination_base 分页的基础路径
Anything to be inserted before pagination indices. Defaults to 'page/'.
在分页索引以前插入的内容. 默认是 'page/' 。
public 'pagination_base' => string 'page' (length=4)
$feeds 订阅
Supported feeds names (rdf, rss, atom) Use add_feed to override or add.
支持订阅的名字(rdf, rss, atom)使用add_feed重写或添加。
1 public 'feeds' => 2 array 3 0 => string 'feed' (length=4) 4 1 => string 'rdf' (length=3) 5 2 => string 'rss' (length=3) 6 3 => string 'rss2' (length=4) 7 4 => string 'atom' (length=4)
$feed_base 订阅的基础路径
Anything to be inserted before feed URLs. Defaults to 'feed/'.
在订阅URL以前插入的内容。默认是 'feed/'。
public 'feed_base' => string 'feed' (length=4)
$feed_structure 订阅的结构
订阅URL的结构. 这仅仅是 $feed_base 加上 '%feed%'.
public 'feed_structure' => string 'feed/%feed%' (length=11)
$search_base 搜索的基础路径
Anything to be inserted before searches. Defaults to 'search/'.
在搜索URL以前插入的内容. 默认是 'search/'。
public 'search_base' => string 'search' (length=6)
$search_structure 搜索的结构
Structure for search URLs. This is just the $search_base plus '%search%'.
搜索URL的结构. 这仅仅是 $search_base 加上 '%search%'.
$comments_base 评论的基础路径
Anything to be inserted just before the $feed_structure to get the latest comments feed. Defaults to 'comments'.
在最新评论订阅以前插入的内容。默认是'comments'。
public 'comments_base' => string 'comments' (length=8)
$comments_feed_structure 评论订阅的结构
The structure for the latest comments feed. This is just $comments_base plus $feed_base plus '%feed%'.
最新评论订阅的结构.。这仅仅是 $comments_base 加上 $feed_base plus '%feed%'。
public 'comment_feed_structure' => string 'comments/feed/%feed%' (length=20)
$date_structure 日期的结构
Structure for dated archive URLs. Tries to be '%year%/%monthnum%/%day%', '%day%/%monthnum%/%year%' or '%monthnum%/%day%/%year%', but if none of these are detected in your $permalink_structure, defaults to '%year%/%monthnum%/%day%'. Various functions use this structure to obtain less specific structures: for example, get_year_permastruct() simply removes the '%monthnum%' and '%day%' tags from $date_structure.
日期档案URL的结构。尝试 '%year%/%monthnum%/%day%','%day%/%monthnum%/%year%' or '%monthnum%/%day%/%year%', 可是,若是这些都不是你检测的$permalink_structure,默认是 '%year%/%monthnum%/%day%'。各类功能使用这些结构去获取更少的具体结构。例如,get_year_permastruct()简单的移除 %monthnum%' 和 '%day%' 标签,对于$date_structure。
public 'date_structure' => string '/%year%/%monthnum%/%day%' (length=24)
$page_structure 页面的结构(别名)
Structure for Pages. Just '%pagename%'.
页面的结构。仅仅是 '%pagename%'。
public 'page_structure' => string '%pagename%' (length=10)
$front
Anything up to the start of the first tag in your $permalink_structure.
在你的$permalink_structure第一个标签前插入的内容。
public 'front' => string '/' (length=1)
$root
The root of your WordPress install. Prepended to all structures.
WordPress安装的根目录。全部结构的前缀。
ublic 'root' => string '' (length=0)
$matches
Used internally when calculating back references for the redirect part of the rewrite rules.
在计算重写规则重定向部分的回引用时,使用在内部。
public 'matches' => string '' (length=0)
$rules
The rewrite rules. Set when rewrite_rules() is called.
重写规则。当rewrite_rules()调用时。
规则的一部分举例。实际的重写规则更多。
1 public 'rules' => 2 array 3 '^wp-json/?$' => string 'index.php?rest_route=/' (length=22) 4 '^wp-json/(.*)?' => string 'index.php?rest_route=/$matches[1]' (length=33) 5 '^index.php/wp-json/?$' => string 'index.php?rest_route=/' (length=22) 6 '^index.php/wp-json/(.*)?' => string 'index.php?rest_route=/$matches[1]' (length=33)
$non_wp_rules
Associative array of "rules that don't redirect to WP's index.php (and thus shouldn't be handled by WP at all)" roughly in the form 'Pattern' => 'Substitution'
(see below).
一个关联数组。"不要重定向到 WP的 index.php(所以不该该被WP处理。)" 。粗略的结构是 'Pattern' => 'Substitution'(见下文)。
public 'non_wp_rules' => array empty
$rewritecode
An array of all the tags available for the permalink structure. See Using Permalinks for a list.
一个数组,全部的标签可用于固定连接。看看使用Permalinks列表。
1 public 'rewritecode' => 2 array 3 0 => string '%year%' (length=6) 4 1 => string '%monthnum%' (length=10) 5 2 => string '%day%' (length=5) 6 3 => string '%hour%' (length=6) 7 4 => string '%minute%' (length=8) 8 5 => string '%second%' (length=8) 9 6 => string '%postname%' (length=10) 10 7 => string '%post_id%' (length=9) 11 8 => string '%author%' (length=8) 12 9 => string '%pagename%' (length=10) 13 10 => string '%search%' (length=8) 14 11 => string '%category%' (length=10) 15 12 => string '%post_tag%' (length=10) 16 13 => string '%post_format%' (length=13)
$rewritereplace 重写规则替换
What each tag will be replaced with for the regex part of the rewrite rule. The first element in $rewritereplace is the regex for the first element in $rewritecode, the second corresponds to the second, and so on.
每一个标签将取代的重写规则的正则表达式的一部分。在 $rewritereplace的第一元素是正则表达式,替换$rewritecode的第一元素。第二项对应于第二项。后边的都这样对应。
1 public 'rewritereplace' => 2 array 3 0 => string '([0-9]{4})' (length=10) 4 1 => string '([0-9]{1,2})' (length=12) 5 2 => string '([0-9]{1,2})' (length=12) 6 3 => string '([0-9]{1,2})' (length=12) 7 4 => string '([0-9]{1,2})' (length=12) 8 5 => string '([0-9]{1,2})' (length=12) 9 6 => string '([^/]+)' (length=7) 10 7 => string '([0-9]+)' (length=8) 11 8 => string '([^/]+)' (length=7) 12 9 => string '([^/]+?)' (length=8) 13 10 => string '(.+)' (length=4) 14 11 => string '(.+?)' (length=5) 15 12 => string '([^/]+)' (length=7) 16 13 => string '([^/]+)' (length=7)
$queryreplace
What each tag will be replaced with in the rewrite part of the rewrite rule. The same correspondance applies here as with $rewritereplace.
重写规则的重写部分中将替换每一个标记。一样的结果适用于这里,相似$rewritereplace
1 public 'queryreplace' => 2 array 3 0 => string 'year=' (length=5) 4 1 => string 'monthnum=' (length=9) 5 2 => string 'day=' (length=4) 6 3 => string 'hour=' (length=5) 7 4 => string 'minute=' (length=7) 8 5 => string 'second=' (length=7) 9 6 => string 'name=' (length=5) 10 7 => string 'p=' (length=2) 11 8 => string 'author_name=' (length=12) 12 9 => string 'pagename=' (length=9) 13 10 => string 's=' (length=2) 14 11 => string 'category_name=' (length=14) 15 12 => string 'tag=' (length=4) 16 13 => string 'post_format=' (length=12)
add_rewrite_tag($tag, $pattern, $query)
Add an element to the $rewritecode, $rewritereplace and $queryreplace arrays using each parameter respectively. If $tag already exists in $rewritecode, the existing value will be overwritten.
See also: add_rewrite_tag($tagname, $regex)
添加一个元素到 $rewritecode, $rewritereplace 和 $queryreplace,分别使用每一个参数。若是$tag在$tagrewritecode中已经存在的,现有的值将被覆盖。
查看: add_rewrite_tag($tag, $regex, $query) 函数。
这个函数其实也是调用$wp_rewrite->add_rewrite_tag(); 只是在调用的过程当中作了一些处理,能够查看函数的源码,这个函数代码只有几行。
/** 144 * Add a new rewrite tag (like %postname%). 145 * 146 * The $query parameter is optional. If it is omitted you must ensure that 147 * you call this on, or before, the {@see 'init'} hook. This is because $query defaults 148 * to "$tag=", and for this to work a new query var has to be added. 149 * 150 * @since 2.1.0 151 * 152 * @global WP_Rewrite $wp_rewrite 153 * @global WP $wp 154 * 155 * @param string $tag Name of the new rewrite tag. 156 * @param string $regex Regular expression to substitute the tag for in rewrite rules. 157 * @param string $query Optional. String to append to the rewritten query. Must end in '='. Default empty. 158 */ 159 function add_rewrite_tag( $tag, $regex, $query = '' ) { 160 // validate the tag's name 161 if ( strlen( $tag ) < 3 || $tag[0] != '%' || $tag[ strlen($tag) - 1 ] != '%' ) 162 return; 163 164 global $wp_rewrite, $wp; 165 166 if ( empty( $query ) ) { 167 $qv = trim( $tag, '%' ); 168 $wp->add_query_var( $qv ); 169 $query = $qv . '='; 170 } 171 172 $wp_rewrite->add_rewrite_tag( $tag, $regex, $query ); 173 }
add_feed($feedname, $function)
Add a new feed type like /atom1/ . Requires one-time use of flush_rules() to take effect.
添加一个新的订阅类型,例如 /atom1/ 。要求一次性使用 flush_rules() 生效。
#正确的来讲这只是一个函数,并非WP_Rewrite 的方法。
/** 234 * Add a new feed type like /atom1/. 235 * 236 * @since 2.1.0 237 * 238 * @global WP_Rewrite $wp_rewrite 239 * 240 * @param string $feedname Feed name. 241 * @param callable $function Callback to run on feed display. 242 * @return string Feed action name. 243 */ 244 function add_feed( $feedname, $function ) { 245 global $wp_rewrite; 246 247 if ( ! in_array( $feedname, $wp_rewrite->feeds ) ) { 248 $wp_rewrite->feeds[] = $feedname; 249 } 250 251 $hook = 'do_feed_' . $feedname; 252 253 // Remove default function hook 254 remove_action( $hook, $hook ); 255 256 add_action( $hook, $function, 10, 2 ); 257 258 return $hook; 259 }
Regenerate the rewrite rules and save them to the database
从新生成重写规则并保存到数据库中.
/** 1790 * Removes rewrite rules and then recreate rewrite rules. 1791 * 1792 * Calls WP_Rewrite::wp_rewrite_rules() after removing the 'rewrite_rules' option. 1793 * If the function named 'save_mod_rewrite_rules' exists, it will be called. 1794 * 1795 * @since 2.0.1 1796 * @access public 1797 * 1798 * @staticvar bool $do_hard_later 1799 * 1800 * @param bool $hard Whether to update .htaccess (hard flush) or just update rewrite_rules option (soft flush). Default is true (hard). 1801 */ 1802 public function flush_rules( $hard = true ) { 1803 static $do_hard_later = null; 1804 1805 // Prevent this action from running before everyone has registered their rewrites. 1806 if ( ! did_action( 'wp_loaded' ) ) { 1807 add_action( 'wp_loaded', array( $this, 'flush_rules' ) ); 1808 $do_hard_later = ( isset( $do_hard_later ) ) ? $do_hard_later || $hard : $hard; 1809 return; 1810 } 1811 1812 if ( isset( $do_hard_later ) ) { 1813 $hard = $do_hard_later; 1814 unset( $do_hard_later ); 1815 } 1816 1817 update_option( 'rewrite_rules', '' ); 1818 $this->wp_rewrite_rules(); 1819 1820 /** 1821 * Filters whether a "hard" rewrite rule flush should be performed when requested. 1822 * 1823 * A "hard" flush updates .htaccess (Apache) or web.config (IIS). 1824 * 1825 * @since 3.7.0 1826 * 1827 * @param bool $hard Whether to flush rewrite rules "hard". Default true. 1828 */ 1829 if ( ! $hard || ! apply_filters( 'flush_rewrite_rules_hard', true ) ) { 1830 return; 1831 } 1832 if ( function_exists( 'save_mod_rewrite_rules' ) ) 1833 save_mod_rewrite_rules(); 1834 if ( function_exists( 'iis7_save_url_rewrite_rules' ) ) 1835 iis7_save_url_rewrite_rules(); 1836 }
generate_rewrite_rule($permalink_structure, $walk_dirs = false)
Generates a no-frills rewrite rule from the permalink structure. No rules for extra pages or feeds will be created.
从固定连接结构生成一个不带装饰的重写规则。不存在规则的扩展页面或者订阅 ,将被建立。
generate_rewrite_rules($permalink_structure, $ep_mask = EP_NONE, $page = true, $feed = true, $forcomments= false, $walk_dirs = true)
A large function that generates the rewrite rules for a given structure, $permalink_structure. If $page is true, an extra rewrite rule will be generated for accessing different pages (e.g. /category/tech/page/2 points to the second page of the 'tech' category archive). If $feed is true, extra rewrite rules will be generated for obtaining a feed of the current page, and if $forcomments is true, this will be a comment feed. If $walk_dirs is true, then a rewrite rule will be generated for each directory of the structure provided, e.g. if you provide it with '/%year%/%month%/%day/', rewrite rules will be generated for '/%year%/', /%year%/%month%/' and '/%year%/%month%/%day%/'. This returns an associative array using the regex part of the rewrite rule as the keys and redirect part of the rewrite rule as the value.
为给定结构($permalink_structure)生成重写规则的强大函数。
若是$page为true。将生成一个额外的重写规则来访问不一样的页面。(e.g. /category/tech/page/2 指向 “tech” 类别存档的第二页)。
若是$feed为true,将 生成额外的重写规则 以获取当前页的订阅。
若是$forcomments为true,这将是一个评论订阅。
若是$walk_dirs为true,而后将为所提供的结构的每一个目录生成重写规则。例如你提供的是 '/%year%/%month%/%day/',从重写规则将生成 '/%year%/', '/%year%/%month%/' 和 '/%year%/%month%/%day%/'。
将返回一个关联数组,使用重写规则的正则部分做为键名,重写规则的重定向部分做为键值。
get_date_permastruct(), get_category_permastruct(), get_date_permastruct() etc.
Populates the corresponding property (e.g., $date_structure for get_date_permastruct()) if it's not already set and returns it. The functions get_month_permastruct() and get_year_permastruct() don't have a corresponding property: they work out the structure by taking the $date_structure and removing tags that are more specific than they need (i.e., get_month_permastruct() removes the '%day%' tag, as it only needs to specify the year and month).
填充相应的属性(例如 $date_structure 对应 get_date_permastruct())若是尚未的话,设置并返回该属性。函数 get_month_permastruct() 和 get_year_permastruct() 没有相对应的属性:他们输出结构到$date_structure 而且 移除标签,这须要具体的指定,当他们须要的时候(例如,get_month_permastruct() 移除 '%day%' 标签,由于它只须要指定年份和月份。)
init()
Set up the object, set $permalink_structure and $category_base from the database. Set $root to $index plus '/'. Set $frontto everything up to the start of the first tag in the permalink structure. Unset all other properties.
设置对象,读取数据库中的选项,而后设置 $permalink_structure 和 $category_base。
将$root 设置为 $index 加上 '/' 。在全部固定连接标签的前边添加 $front 。
取消全部的其余属性。
mod_rewrite_rules()
returns a string (not an array) of all the rules. They are wrapped in an Apache <IfModule> block, to ensure mod_rewrite is enabled.
返回一个全部规则的字符串(不是数组)。他们是包含在 Apache <IfModule>块内的,确保 mod_rewrite 是有效的。
#是建立了字符串,并无写入到文件。
page_rewrite_rules()
Returns the set of rules for any Pages you have created.
返回规则集合,对于您建立的任何页面的。
rewrite_rules()
populate and return the $rules variable with an associative array as in generate_rewrite_rules(). This is generated from the post, date, comment, search, category, authors and page structures.
填充并返回$rules变量,调用generate_rewrite_rules()生成的关联数组。这是从文章、日期、评论、搜索、类别、做者和页面结构中生成的。
#返回一个关联数组,并保存到$rules变量中。 这个关联数组是调用generate_rewrite_rules()函数生成的。
set_category_base($category_base)
Change the category base.
改变分类的基础路径(更新数据库中 wp_options 表的 category_base 记录)
/** 1907 * Sets the category base for the category permalink. 1908 * 1909 * Will update the 'category_base' option, if there is a difference between 1910 * the current category base and the parameter value. Calls WP_Rewrite::init() 1911 * after the option is updated. 1912 * 1913 * @since 1.5.0 1914 * @access public 1915 * 1916 * @param string $category_base Category permalink structure base. 1917 */ 1918 public function set_category_base($category_base) { 1919 if ( $category_base != get_option('category_base') ) { 1920 update_option('category_base', $category_base); 1921 $this->init(); 1922 } 1923 }
set_permalink_structure($permalink_structure)
Change the permalink structure.
改变固定连接结构(更新数据库中 wp_options 表的 permalink_structure 记录)
/** 1873 * Sets the main permalink structure for the site. 1874 * 1875 * Will update the 'permalink_structure' option, if there is a difference 1876 * between the current permalink structure and the parameter value. Calls 1877 * WP_Rewrite::init() after the option is updated. 1878 * 1879 * Fires the {@see 'permalink_structure_changed'} action once the init call has 1880 * processed passing the old and new values 1881 * 1882 * @since 1.5.0 1883 * @access public 1884 * 1885 * @param string $permalink_structure Permalink structure. 1886 */ 1887 public function set_permalink_structure($permalink_structure) { 1888 if ( $permalink_structure != $this->permalink_structure ) { 1889 $old_permalink_structure = $this->permalink_structure; 1890 update_option('permalink_structure', $permalink_structure); 1891 1892 $this->init(); 1893 1894 /** 1895 * Fires after the permalink structure is updated. 1896 * 1897 * @since 2.8.0 1898 * 1899 * @param string $old_permalink_structure The previous permalink structure. 1900 * @param string $permalink_structure The new permalink structure. 1901 */ 1902 do_action( 'permalink_structure_changed', $old_permalink_structure, $permalink_structure ); 1903 } 1904 }
using_index_permalinks()
Returns true if your blog is using PATHINFO permalinks.
若是你的博客使用的是永久连接返回true。
using_mod_rewrite_permalinks()
Returns true your blog is using "pretty" permalinks via mod_rewrite.
返回true,你的博客使用的是“更好”的永久连接,经过mod_rewrite。
using_permalinks()
Returns true if your blog is using any permalink structure (i.e. not the default query URIs ?p=n, ?cat=n).
返回true,若是你的博客是使用任何的永久连接结构 (例如, 不是默认的查询 URIs ?p=n, ?cat=n).
WP_Rewrite (constructor) 类的构造函数
Calls init().
调用 init().
wp_rewrite_rules()
returns the array of rewrite rules as in rewrite_rules(), but using $matches[xxx] in the (where xxx is a number) instead of the normal mod_rewrite backreferences, $xxx (where xxx is a number). This is useful when you're going to be using the rules inside PHP, rather than writing them out to a .htaccess file.
返回重写规则的数组,经过 rewrite_rules()函数,可是使用$matches[xxx](这里xxx是一个数字)替代常规的mod_rewrite的反向引用,$xxx (这里xxx是一个数字)。这是有用的,当你使用PHP的规则,而不是写出来一个.htaccess文件。
/** 1461 * Retrieves the rewrite rules. 1462 * 1463 * The difference between this method and WP_Rewrite::rewrite_rules() is that 1464 * this method stores the rewrite rules in the 'rewrite_rules' option and retrieves 1465 * it. This prevents having to process all of the permalinks to get the rewrite rules 1466 * in the form of caching. 1467 * 1468 * @since 1.5.0 1469 * @access public 1470 * 1471 * @return array Rewrite rules. 1472 */ 1473 public function wp_rewrite_rules() { 1474 $this->rules = get_option('rewrite_rules'); 1475 if ( empty($this->rules) ) { 1476 $this->matches = 'matches'; 1477 $this->rewrite_rules(); 1478 if ( ! did_action( 'wp_loaded' ) ) { 1479 add_action( 'wp_loaded', array( $this, 'flush_rules' ) ); 1480 return $this->rules; 1481 } 1482 update_option('rewrite_rules', $this->rules); 1483 } 1484 1485 return $this->rules; 1486 }
As the rewrite rules are a crucial part of your website's functionality, WordPress allows plugins to hook into the generation process at several points. rewrite_rules(), specifically, contains nine filters and one hook for really precise control over the rewrite rules process. Here's what you can filter in rewrite_rules():
因为重写规则是网站功能的一个重要部分,WordPress容许插件在几点中融入生成过程。rewrite_rules(),具体而言,包含九个滤波器,在重写规则的过程很精确地控制一个钩子。在这里你能够过滤rewrite_rules():
mod_rewrite_rules() is the function that takes the array generated by rewrite_rules() and actually turns it into a set of rewrite rules for the .htaccess file. This function also has a filter, mod_rewrite_rules, which will pass functions the string of all the rules to be written out to .htaccess, including the <IfModule> surrounding section. (Note: you may also see plugins using the rewrite_rules hook, but this is deprecated).
mod_rewrite_rules()是一个函数,数组的生成是经过rewrite_rules(),和实际上把它变成了一组重写规则,对于htaccess文件。这个函数也有一个过滤器,mod_rewrite_rules,做用是将全部的规则的字符串写入到 .htaccess文件,包括 <IfModule>环绕的部分。(注:你也能够看到插件使用rewrite_rules挂钩,但这是不推荐使用)。
(See also: Permalinks for Custom Archives) The most obvious thing a plugin would do with the $wp_rewrite object is add its own rewrite rules. This is remarkably simple. Filter the generic rewrite_rules_array.
最明显的事,一个插件将 $wp_rewrite对象添加本身的重写规则。这很是简单。过滤器是rewrite_rules_array。
A Quick and dirty example for rewriting http://mysite/project/1 into http://mysite/index.php?pagename=project&id=1:
一个快速和粗略的例子,重写 http://mysite/project/1 变成 http://mysite/index.php?pagename=project&id=1
1 add_filter( 'rewrite_rules_array','my_insert_rewrite_rules' ); 2 add_filter( 'query_vars','my_insert_query_vars' ); 3 add_action( 'wp_loaded','my_flush_rules' ); 4 5 // flush_rules() if our rules are not yet included 6 function my_flush_rules(){ 7 $rules = get_option( 'rewrite_rules' ); 8 9 if ( ! isset( $rules['(project)/(\d*)$'] ) ) { 10 global $wp_rewrite; 11 $wp_rewrite->flush_rules(); 12 } 13 } 14 15 // Adding a new rule 16 function my_insert_rewrite_rules( $rules ) 17 { 18 $newrules = array(); 19 $newrules['(project)/(\d*)$'] = 'index.php?pagename=$matches[1]&id=$matches[2]'; 20 return $newrules + $rules; 21 } 22 23 // Adding the id var so that WP recognizes it 24 function my_insert_query_vars( $vars ) 25 { 26 array_push($vars, 'id'); 27 return $vars; 28 }
Keep in mind that the flush_rules function is quite slow, so in practice you never want to call it from the wp_loaded action that gets executed on each page load. Instead, call this function only when the rewrite rules change. If the rules get set up and then never change, it is enough to flush in register_activation_hook of your plugin.
记住,flush_rules功能至关缓慢,所以在实践中你不想从wp_loaded行动获得在每一个页面加载执行调用它。相反,只有在重写规则更改时调用此函数。若是规则获得创建并不会改变,那么在你的插件register_activation_hook 刷新。
The Jerome's Keywords plugin does this to enable URLs like http://example.com/tag/sausages.
Jerome's Keywords 插件是否启用URL像http://example.com/tag/sausages。
1 function keywords_create_rewrite_rules( $rewrite ) { 2 global $wp_rewrite; 3 4 // add rewrite tokens 5 $keytag_token = '%tag%'; 6 $wp_rewrite->add_rewrite_tag( $keytag_token, '(.+)', 'tag=' ); 7 8 $keywords_structure = $wp_rewrite->root . "tag/$keytag_token"; 9 $keywords_rewrite = $wp_rewrite->generate_rewrite_rules( $keywords_structure ); 10 11 return ( $rewrite + $keywords_rewrite ); 12 }
Instead of inserting the rewrite rules into the $rewrite array itself, Jerome chose to create a second array, $keywords_rewrite, using the WP_Rewrite function generate_rewrite_rules(). Using that function means that the plugin doesn't have to create rewrite rules for extra pages (like page/2), or feeds (like feed/atom), etc. This array is then appended onto the $rewrite array and returned.
而不是将重写规则插入到$$rewrite数组,Jerome选择建立第二个数组,$keywords_rewrite,使用WP_Rewrite 的函数 generate_rewrite_rules()。使用这个函数意味着插件不能建立重写规则,对于扩展页面(例如 page/2),或订阅(例如 feed/atom),等等。而后将这个数组附加到 $rewrite数组并返回。
A simpler example of this is Ryan Boren's Feed Director plugin. This simply redirects URLs like http://example.com/feed.xmlto http://example.com/feed/rss2:
一个简单的例子,Ryan Boren的订阅目录插件。这个简单重定向URLs,相似 http://example.com/feed.xmlt 重定向到 http://example.com/feed/rss2:
1 function feed_dir_rewrite( $wp_rewrite ) { 2 $feed_rules = array( 3 'index.rdf' => 'index.php?feed=rdf', 4 'index.xml' => 'index.php?feed=rss2', 5 '(.+).xml' => 'index.php?feed=' . $wp_rewrite->preg_index(1) 6 ); 7 8 $wp_rewrite->rules = $feed_rules + $wp_rewrite->rules; 9 return $wp_rewrite->rules; 10 } 11 12 // Hook in. 13 add_filter( 'generate_rewrite_rules', 'feed_dir_rewrite' );
As the array is so simple here, there is no need to call generate_rewrite_rules(). Again, the plugin's rules are added to WordPress'. Notice that as this function filters generate_rewrite_rules, it accepts a reference to the entire $wp_rewrite object as a parameter, not just the rewrite rules.
这里的数组很简单,不须要调用generate_rewrite_rules()。再次,插件的规则添加到WordPress。注意,这个过滤器 generate_rewrite_rules,它接受一个$wp_rewrite对象做为参数。而不仅是重写规则。
Of course, as you're adding your rewrite rules to the array before WordPress does anything with them, your plugins rewrite rules will be included in anything WordPress does with the rewrite rules, like write them to the .htaccess file.
固然,当你添加你的重写规则到数组,在这以前,WordPress作什么与他们,你的插件的重写规则将被包含在WordPress的重写规则,像把他们写入到 .htaccess文件。
1 <?php 2 $wp_rewrite->non_wp_rules = array( 'Pattern' => 'Substitution' ); 3 print_r($wp_rewrite->mod_rewrite_rules()); 4 ?>
打印输出
<IfModule mod_rewrite.c> RewriteEngine On RewriteBase /wp_home/ RewriteRule ^Pattern /wp_home/Substitution [QSA,L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /wp_home/index.php [L] </IfModule>
where /wp_home/ is WordPress' home directory (or the root URL / if WordPress is installed in your web root.)
这里 /wp_home/ 是WordPress'主目录(或者根URL / ,若是WordPress是安装在你的web根目录)。
WP_Rewrite() is located in wp-includes/class-wp-rewrite.php