首先這裏是我電腦上打印出來的 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