| [ Index ] |
PHP Cross Reference of WordPress Trunk (Updated Daily) |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * Canonical API to handle WordPress Redirecting 4 * 5 * Based on "Permalink Redirect" from Scott Yang and "Enforce www. Preference" 6 * by Mark Jaquith 7 * 8 * @package WordPress 9 * @since 2.3.0 10 */ 11 12 /** 13 * Redirects incoming links to the proper URL based on the site url. 14 * 15 * Search engines consider www.somedomain.com and somedomain.com to be two 16 * different URLs when they both go to the same location. This SEO enhancement 17 * prevents penalty for duplicate content by redirecting all incoming links to 18 * one or the other. 19 * 20 * Prevents redirection for feeds, trackbacks, searches, and 21 * admin URLs. Does not redirect on non-pretty-permalink-supporting IIS 7+, 22 * page/post previews, WP admin, Trackbacks, robots.txt, favicon.ico, searches, 23 * or on POST requests. 24 * 25 * Will also attempt to find the correct link when a user enters a URL that does 26 * not exist based on exact WordPress query. Will instead try to parse the URL 27 * or query in an attempt to figure the correct page to go to. 28 * 29 * @since 2.3.0 30 * 31 * @global WP_Rewrite $wp_rewrite WordPress rewrite component. 32 * @global bool $is_IIS 33 * @global WP_Query $wp_query WordPress Query object. 34 * @global WP_Query $wp_the_query WordPress main Query object. 35 * @global wpdb $wpdb WordPress database abstraction object. 36 * @global WP $wp Current WordPress environment instance. 37 * 38 * @param string $requested_url Optional. The URL that was requested, used to 39 * figure if redirect is needed. 40 * @param bool $do_redirect Optional. Redirect to the new URL. 41 * @return string|null The string of the URL, if redirect needed. Never returns if a redirect occurs, depending on $do_redirect. 42 * @phpstan-return ( $do_redirect is true ? null : string|null ) 43 */ 44 function redirect_canonical( $requested_url = null, $do_redirect = true ) { 45 global $wp_rewrite, $is_IIS, $wp_query, $wp_the_query, $wpdb, $wp; 46 47 if ( isset( $_SERVER['REQUEST_METHOD'] ) && ! in_array( strtoupper( $_SERVER['REQUEST_METHOD'] ), array( 'GET', 'HEAD' ), true ) ) { 48 return null; 49 } 50 51 /* 52 * Random content requests with a selected post are redirected to that post by 53 * wp_random_content_redirect() later on the `template_redirect` action. A canonical 54 * redirect would redirect the request twice and may drop the `random` query variable. 55 */ 56 if ( $wp_the_query instanceof WP_Query 57 && wp_is_random_content_query( $wp_the_query ) 58 && $wp_the_query->post instanceof WP_Post 59 ) { 60 return null; 61 } 62 63 /* 64 * If we're not in wp-admin and the post has been published and preview nonce 65 * is non-existent or invalid then no need for preview in query. 66 */ 67 if ( is_preview() && get_query_var( 'p' ) && 'publish' === get_post_status( get_query_var( 'p' ) ) ) { 68 if ( ! isset( $_GET['preview_id'] ) 69 || ! isset( $_GET['preview_nonce'] ) 70 || ! wp_verify_nonce( $_GET['preview_nonce'], 'post_preview_' . (int) $_GET['preview_id'] ) 71 ) { 72 $wp_query->is_preview = false; 73 } 74 } 75 76 if ( is_admin() || is_search() || is_preview() || is_trackback() || is_favicon() 77 || ( $is_IIS && ! iis7_supports_permalinks() ) 78 ) { 79 return null; 80 } 81 82 if ( ! $requested_url && isset( $_SERVER['HTTP_HOST'] ) ) { 83 // Build the URL in the address bar. 84 $requested_url = is_ssl() ? 'https://' : 'http://'; 85 $requested_url .= $_SERVER['HTTP_HOST']; 86 $requested_url .= $_SERVER['REQUEST_URI']; 87 } 88 89 $original = parse_url( $requested_url ); 90 if ( false === $original ) { 91 return null; 92 } 93 94 // Notice fixing. 95 $original += array( 96 'host' => '', 97 'path' => '', 98 'query' => '', 99 'scheme' => '', 100 ); 101 102 $redirect = $original; 103 $redirect_url = false; 104 $redirect_obj = false; 105 106 /* 107 * If the original URL ended with non-breaking spaces, they were almost 108 * certainly inserted by accident. Let's remove them, so the reader doesn't 109 * see a 404 error with no obvious cause. 110 */ 111 $redirect['path'] = preg_replace( '|(%C2%A0)+$|i', '', $redirect['path'] ); 112 113 // It's not a preview, so remove it from URL. 114 if ( get_query_var( 'preview' ) ) { 115 $redirect['query'] = remove_query_arg( 'preview', $redirect['query'] ); 116 } 117 118 $post_id = get_query_var( 'p' ); 119 120 if ( is_feed() && $post_id ) { 121 $redirect_url = get_post_comments_feed_link( $post_id, get_query_var( 'feed' ) ); 122 $redirect_obj = get_post( $post_id ); 123 124 if ( $redirect_url ) { 125 $redirect['query'] = _remove_qs_args_if_not_in_url( 126 $redirect['query'], 127 array( 'p', 'page_id', 'attachment_id', 'pagename', 'name', 'post_type', 'feed' ), 128 $redirect_url 129 ); 130 131 $redirect['path'] = parse_url( $redirect_url, PHP_URL_PATH ); 132 } 133 } 134 135 if ( is_singular() && $wp_query->post_count < 1 && $post_id ) { 136 137 $vars = $wpdb->get_results( $wpdb->prepare( "SELECT post_type, post_parent FROM $wpdb->posts WHERE ID = %d", $post_id ) ); 138 139 if ( ! empty( $vars[0] ) ) { 140 $vars = $vars[0]; 141 142 if ( 'revision' === $vars->post_type && $vars->post_parent > 0 ) { 143 $post_id = $vars->post_parent; 144 } 145 146 $redirect_url = get_permalink( $post_id ); 147 $redirect_obj = get_post( $post_id ); 148 149 if ( $redirect_url ) { 150 $redirect['query'] = _remove_qs_args_if_not_in_url( 151 $redirect['query'], 152 array( 'p', 'page_id', 'attachment_id', 'pagename', 'name', 'post_type' ), 153 $redirect_url 154 ); 155 } 156 } 157 } 158 159 // These tests give us a WP-generated permalink. 160 if ( is_404() ) { 161 162 // Redirect ?page_id, ?p=, ?attachment_id= to their respective URLs. 163 $post_id = max( get_query_var( 'p' ), get_query_var( 'page_id' ), get_query_var( 'attachment_id' ) ); 164 165 $redirect_post = $post_id ? get_post( $post_id ) : false; 166 167 if ( $redirect_post ) { 168 $post_type_obj = get_post_type_object( $redirect_post->post_type ); 169 170 if ( $post_type_obj && $post_type_obj->public && 'auto-draft' !== $redirect_post->post_status ) { 171 $redirect_url = get_permalink( $redirect_post ); 172 $redirect_obj = get_post( $redirect_post ); 173 174 $redirect['query'] = _remove_qs_args_if_not_in_url( 175 $redirect['query'], 176 array( 'p', 'page_id', 'attachment_id', 'pagename', 'name', 'post_type' ), 177 $redirect_url 178 ); 179 } 180 } 181 182 $year = get_query_var( 'year' ); 183 $month = get_query_var( 'monthnum' ); 184 $day = get_query_var( 'day' ); 185 186 if ( $year && $month && $day ) { 187 $date = sprintf( '%04d-%02d-%02d', $year, $month, $day ); 188 189 if ( ! wp_checkdate( $month, $day, $year, $date ) ) { 190 $redirect_url = get_month_link( $year, $month ); 191 192 $redirect['query'] = _remove_qs_args_if_not_in_url( 193 $redirect['query'], 194 array( 'year', 'monthnum', 'day' ), 195 $redirect_url 196 ); 197 } 198 } elseif ( $year && $month && $month > 12 ) { 199 $redirect_url = get_year_link( $year ); 200 201 $redirect['query'] = _remove_qs_args_if_not_in_url( 202 $redirect['query'], 203 array( 'year', 'monthnum' ), 204 $redirect_url 205 ); 206 } 207 208 // Strip off non-existing <!--nextpage--> links from single posts or pages. 209 if ( get_query_var( 'page' ) ) { 210 $post_id = 0; 211 212 if ( $wp_query->queried_object instanceof WP_Post ) { 213 $post_id = $wp_query->queried_object->ID; 214 } elseif ( $wp_query->post ) { 215 $post_id = $wp_query->post->ID; 216 } 217 218 if ( $post_id ) { 219 $redirect_url = get_permalink( $post_id ); 220 $redirect_obj = get_post( $post_id ); 221 222 $redirect['path'] = rtrim( $redirect['path'], (int) get_query_var( 'page' ) . '/' ); 223 $redirect['query'] = remove_query_arg( 'page', $redirect['query'] ); 224 } 225 } 226 227 if ( ! $redirect_url ) { 228 $redirect_url = redirect_guess_404_permalink(); 229 230 if ( $redirect_url ) { 231 $redirect['query'] = _remove_qs_args_if_not_in_url( 232 $redirect['query'], 233 array( 'page', 'feed', 'p', 'page_id', 'attachment_id', 'pagename', 'name', 'post_type' ), 234 $redirect_url 235 ); 236 } 237 } 238 } elseif ( is_object( $wp_rewrite ) && $wp_rewrite->using_permalinks() ) { 239 240 // Rewriting of old ?p=X, ?m=2004, ?m=200401, ?m=20040101. 241 if ( is_attachment() 242 && ! array_diff( array_keys( $wp->query_vars ), array( 'attachment', 'attachment_id' ) ) 243 && ! $redirect_url 244 ) { 245 if ( ! empty( $_GET['attachment_id'] ) ) { 246 $redirect_url = get_attachment_link( get_query_var( 'attachment_id' ) ); 247 $redirect_obj = get_post( get_query_var( 'attachment_id' ) ); 248 249 if ( $redirect_url ) { 250 $redirect['query'] = remove_query_arg( 'attachment_id', $redirect['query'] ); 251 } 252 } else { 253 $redirect_url = get_attachment_link(); 254 $redirect_obj = get_post(); 255 } 256 } elseif ( is_single() && ! empty( $_GET['p'] ) && ! $redirect_url ) { 257 $redirect_url = get_permalink( get_query_var( 'p' ) ); 258 $redirect_obj = get_post( get_query_var( 'p' ) ); 259 260 if ( $redirect_url ) { 261 $redirect['query'] = remove_query_arg( array( 'p', 'post_type' ), $redirect['query'] ); 262 } 263 } elseif ( is_single() && ! empty( $_GET['name'] ) && ! $redirect_url ) { 264 $redirect_url = get_permalink( $wp_query->get_queried_object_id() ); 265 $redirect_obj = get_post( $wp_query->get_queried_object_id() ); 266 267 if ( $redirect_url ) { 268 $redirect['query'] = remove_query_arg( 'name', $redirect['query'] ); 269 } 270 } elseif ( is_page() && ! empty( $_GET['page_id'] ) && ! $redirect_url ) { 271 $redirect_url = get_permalink( get_query_var( 'page_id' ) ); 272 $redirect_obj = get_post( get_query_var( 'page_id' ) ); 273 274 if ( $redirect_url ) { 275 $redirect['query'] = remove_query_arg( 'page_id', $redirect['query'] ); 276 } 277 } elseif ( is_page() && ! is_feed() && ! $redirect_url 278 && 'page' === get_option( 'show_on_front' ) && get_queried_object_id() === (int) get_option( 'page_on_front' ) 279 ) { 280 $redirect_url = home_url( '/' ); 281 } elseif ( is_home() && ! empty( $_GET['page_id'] ) && ! $redirect_url 282 && 'page' === get_option( 'show_on_front' ) && get_query_var( 'page_id' ) === (int) get_option( 'page_for_posts' ) 283 ) { 284 $redirect_url = get_permalink( get_option( 'page_for_posts' ) ); 285 $redirect_obj = get_post( get_option( 'page_for_posts' ) ); 286 287 if ( $redirect_url ) { 288 $redirect['query'] = remove_query_arg( 'page_id', $redirect['query'] ); 289 } 290 } elseif ( ! empty( $_GET['m'] ) && ( is_year() || is_month() || is_day() ) ) { 291 $m = get_query_var( 'm' ); 292 293 switch ( strlen( $m ) ) { 294 case 4: // Yearly. 295 $redirect_url = get_year_link( $m ); 296 break; 297 case 6: // Monthly. 298 $redirect_url = get_month_link( substr( $m, 0, 4 ), substr( $m, 4, 2 ) ); 299 break; 300 case 8: // Daily. 301 $redirect_url = get_day_link( substr( $m, 0, 4 ), substr( $m, 4, 2 ), substr( $m, 6, 2 ) ); 302 break; 303 } 304 305 if ( $redirect_url ) { 306 $redirect['query'] = remove_query_arg( 'm', $redirect['query'] ); 307 } 308 // Now moving on to non ?m=X year/month/day links. 309 } elseif ( is_date() ) { 310 $year = get_query_var( 'year' ); 311 $month = get_query_var( 'monthnum' ); 312 $day = get_query_var( 'day' ); 313 314 if ( is_day() && $year && $month && ! empty( $_GET['day'] ) ) { 315 $redirect_url = get_day_link( $year, $month, $day ); 316 317 if ( $redirect_url ) { 318 $redirect['query'] = remove_query_arg( array( 'year', 'monthnum', 'day' ), $redirect['query'] ); 319 } 320 } elseif ( is_month() && $year && ! empty( $_GET['monthnum'] ) ) { 321 $redirect_url = get_month_link( $year, $month ); 322 323 if ( $redirect_url ) { 324 $redirect['query'] = remove_query_arg( array( 'year', 'monthnum' ), $redirect['query'] ); 325 } 326 } elseif ( is_year() && ! empty( $_GET['year'] ) ) { 327 $redirect_url = get_year_link( $year ); 328 329 if ( $redirect_url ) { 330 $redirect['query'] = remove_query_arg( 'year', $redirect['query'] ); 331 } 332 } 333 } elseif ( is_author() && ! empty( $_GET['author'] ) 334 && is_string( $_GET['author'] ) && preg_match( '|^[0-9]+$|', $_GET['author'] ) 335 ) { 336 $author = get_userdata( get_query_var( 'author' ) ); 337 338 if ( false !== $author 339 && $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE $wpdb->posts.post_author = %d AND $wpdb->posts.post_status = 'publish' LIMIT 1", $author->ID ) ) 340 ) { 341 $redirect_url = get_author_posts_url( $author->ID, $author->user_nicename ); 342 $redirect_obj = $author; 343 344 if ( $redirect_url ) { 345 $redirect['query'] = remove_query_arg( 'author', $redirect['query'] ); 346 } 347 } 348 } elseif ( is_category() || is_tag() || is_tax() ) { // Terms (tags/categories). 349 $term_count = 0; 350 351 foreach ( $wp_query->tax_query->queried_terms as $tax_query ) { 352 if ( isset( $tax_query['terms'] ) && is_countable( $tax_query['terms'] ) ) { 353 $term_count += count( $tax_query['terms'] ); 354 } 355 } 356 357 $obj = $wp_query->get_queried_object(); 358 359 if ( $term_count <= 1 && ! empty( $obj->term_id ) ) { 360 $tax_url = get_term_link( (int) $obj->term_id, $obj->taxonomy ); 361 362 if ( $tax_url && ! is_wp_error( $tax_url ) ) { 363 if ( ! empty( $redirect['query'] ) ) { 364 // Strip taxonomy query vars off the URL. 365 $qv_remove = array( 'term', 'taxonomy' ); 366 367 if ( is_category() ) { 368 $qv_remove[] = 'category_name'; 369 $qv_remove[] = 'cat'; 370 } elseif ( is_tag() ) { 371 $qv_remove[] = 'tag'; 372 $qv_remove[] = 'tag_id'; 373 } else { 374 // Custom taxonomies will have a custom query var, remove those too. 375 $tax_obj = get_taxonomy( $obj->taxonomy ); 376 if ( false !== $tax_obj->query_var ) { 377 $qv_remove[] = $tax_obj->query_var; 378 } 379 } 380 381 $rewrite_vars = array_diff( array_keys( $wp_query->query ), array_keys( $_GET ) ); 382 383 // Check to see if all the query vars are coming from the rewrite, none are set via $_GET. 384 if ( ! array_diff( $rewrite_vars, array_keys( $_GET ) ) ) { 385 // Remove all of the per-tax query vars. 386 $redirect['query'] = remove_query_arg( $qv_remove, $redirect['query'] ); 387 388 // Create the destination URL for this taxonomy. 389 $tax_url = parse_url( $tax_url ); 390 391 if ( ! empty( $tax_url['query'] ) ) { 392 // Taxonomy accessible via ?taxonomy=...&term=... or any custom query var. 393 parse_str( $tax_url['query'], $query_vars ); 394 $redirect['query'] = add_query_arg( $query_vars, $redirect['query'] ); 395 } else { 396 // Taxonomy is accessible via a "pretty URL". 397 $redirect['path'] = $tax_url['path']; 398 } 399 } else { 400 // Some query vars are set via $_GET. Unset those from $_GET that exist via the rewrite. 401 foreach ( $qv_remove as $_qv ) { 402 if ( isset( $rewrite_vars[ $_qv ] ) ) { 403 $redirect['query'] = remove_query_arg( $_qv, $redirect['query'] ); 404 } 405 } 406 } 407 } 408 } 409 } 410 } elseif ( is_single() && str_contains( $wp_rewrite->permalink_structure, '%category%' ) ) { 411 $category_name = get_query_var( 'category_name' ); 412 413 if ( $category_name ) { 414 $category = get_category_by_path( $category_name ); 415 416 if ( ! $category || is_wp_error( $category ) 417 || ! has_term( $category->term_id, 'category', $wp_query->get_queried_object_id() ) 418 ) { 419 $redirect_url = get_permalink( $wp_query->get_queried_object_id() ); 420 $redirect_obj = get_post( $wp_query->get_queried_object_id() ); 421 } 422 } 423 } 424 425 // Post paging. 426 if ( is_singular() && get_query_var( 'page' ) ) { 427 $page = get_query_var( 'page' ); 428 429 if ( ! $redirect_url ) { 430 $redirect_url = get_permalink( get_queried_object_id() ); 431 $redirect_obj = get_post( get_queried_object_id() ); 432 } 433 434 if ( $page > 1 ) { 435 $redirect_url = trailingslashit( $redirect_url ); 436 437 if ( is_front_page() ) { 438 $redirect_url .= user_trailingslashit( "$wp_rewrite->pagination_base/$page", 'paged' ); 439 } else { 440 $redirect_url .= user_trailingslashit( $page, 'single_paged' ); 441 } 442 } 443 444 $redirect['query'] = remove_query_arg( 'page', $redirect['query'] ); 445 } 446 447 if ( get_query_var( 'sitemap' ) ) { 448 $redirect_url = get_sitemap_url( get_query_var( 'sitemap' ), get_query_var( 'sitemap-subtype' ), get_query_var( 'paged' ) ); 449 $redirect['query'] = remove_query_arg( array( 'sitemap', 'sitemap-subtype', 'paged' ), $redirect['query'] ); 450 } elseif ( get_query_var( 'paged' ) || is_feed() || get_query_var( 'cpage' ) ) { 451 // Paging and feeds. 452 $paged = get_query_var( 'paged' ); 453 $feed = get_query_var( 'feed' ); 454 $cpage = get_query_var( 'cpage' ); 455 456 while ( preg_match( "#/$wp_rewrite->pagination_base/?[0-9]+?(/+)?$#", $redirect['path'] ) 457 || preg_match( '#/(comments/?)?(feed|rss2?|rdf|atom)(/+)?$#', $redirect['path'] ) 458 || preg_match( "#/{$wp_rewrite->comments_pagination_base}-[0-9]+(/+)?$#", $redirect['path'] ) 459 ) { 460 // Strip off any existing paging. 461 $redirect['path'] = preg_replace( "#/$wp_rewrite->pagination_base/?[0-9]+?(/+)?$#", '/', $redirect['path'] ); 462 // Strip off feed endings. 463 $redirect['path'] = preg_replace( '#/(comments/?)?(feed|rss2?|rdf|atom)(/+|$)#', '/', $redirect['path'] ); 464 // Strip off any existing comment paging. 465 $redirect['path'] = preg_replace( "#/{$wp_rewrite->comments_pagination_base}-[0-9]+?(/+)?$#", '/', $redirect['path'] ); 466 } 467 468 $addl_path = ''; 469 $default_feed = get_default_feed(); 470 471 if ( is_feed() && in_array( $feed, $wp_rewrite->feeds, true ) ) { 472 if ( ! is_singular() && get_query_var( 'withcomments' ) ) { 473 $addl_path .= 'comments/'; 474 } 475 476 if ( ( 'rss' === $default_feed && 'feed' === $feed ) || 'rss' === $feed ) { 477 $format = ( 'rss2' === $default_feed ) ? '' : 'rss2'; 478 } else { 479 $format = ( $default_feed === $feed || 'feed' === $feed ) ? '' : $feed; 480 } 481 482 $addl_path .= user_trailingslashit( 'feed/' . $format, 'feed' ); 483 484 $redirect['query'] = remove_query_arg( 'feed', $redirect['query'] ); 485 } elseif ( is_feed() && 'old' === $feed ) { 486 $old_feed_files = array( 487 'wp-atom.php' => 'atom', 488 'wp-commentsrss2.php' => 'comments_rss2', 489 'wp-feed.php' => $default_feed, 490 'wp-rdf.php' => 'rdf', 491 'wp-rss.php' => 'rss2', 492 'wp-rss2.php' => 'rss2', 493 ); 494 495 if ( isset( $old_feed_files[ basename( $redirect['path'] ) ] ) ) { 496 $redirect_url = get_feed_link( $old_feed_files[ basename( $redirect['path'] ) ] ); 497 498 wp_redirect( $redirect_url, 301 ); 499 die(); 500 } 501 } 502 503 if ( $paged > 0 ) { 504 $redirect['query'] = remove_query_arg( 'paged', $redirect['query'] ); 505 506 if ( ! is_feed() ) { 507 if ( ! is_single() ) { 508 $addl_path = ! empty( $addl_path ) ? trailingslashit( $addl_path ) : ''; 509 510 if ( $paged > 1 ) { 511 $addl_path .= user_trailingslashit( "$wp_rewrite->pagination_base/$paged", 'paged' ); 512 } 513 } 514 } elseif ( $paged > 1 ) { 515 $redirect['query'] = add_query_arg( 'paged', $paged, $redirect['query'] ); 516 } 517 } 518 519 $default_comments_page = get_option( 'default_comments_page' ); 520 521 if ( get_option( 'page_comments' ) 522 && ( 'newest' === $default_comments_page && $cpage > 0 523 || 'newest' !== $default_comments_page && $cpage > 1 ) 524 ) { 525 $addl_path = ( ! empty( $addl_path ) ? trailingslashit( $addl_path ) : '' ); 526 $addl_path .= user_trailingslashit( $wp_rewrite->comments_pagination_base . '-' . $cpage, 'commentpaged' ); 527 528 $redirect['query'] = remove_query_arg( 'cpage', $redirect['query'] ); 529 } 530 531 // Strip off trailing /index.php/. 532 $redirect['path'] = preg_replace( '|/' . preg_quote( $wp_rewrite->index, '|' ) . '/?$|', '/', $redirect['path'] ); 533 $redirect['path'] = user_trailingslashit( $redirect['path'] ); 534 535 if ( ! empty( $addl_path ) 536 && $wp_rewrite->using_index_permalinks() 537 && ! str_contains( $redirect['path'], '/' . $wp_rewrite->index . '/' ) 538 ) { 539 $redirect['path'] = trailingslashit( $redirect['path'] ) . $wp_rewrite->index . '/'; 540 } 541 542 if ( ! empty( $addl_path ) ) { 543 $redirect['path'] = trailingslashit( $redirect['path'] ) . $addl_path; 544 } 545 546 $redirect_url = $redirect['scheme'] . '://' . $redirect['host'] . $redirect['path']; 547 } 548 549 if ( 'wp-register.php' === basename( $redirect['path'] ) ) { 550 if ( is_multisite() ) { 551 /** This filter is documented in wp-login.php */ 552 $redirect_url = apply_filters( 'wp_signup_location', network_site_url( 'wp-signup.php' ) ); 553 } else { 554 $redirect_url = wp_registration_url(); 555 } 556 557 wp_redirect( $redirect_url, 301 ); 558 die(); 559 } 560 } 561 562 $is_attachment_redirect = false; 563 564 if ( is_attachment() && ! get_option( 'wp_attachment_pages_enabled' ) ) { 565 $attachment_id = get_query_var( 'attachment_id' ); 566 $attachment_post = get_post( $attachment_id ); 567 $attachment_parent_id = $attachment_post ? $attachment_post->post_parent : 0; 568 $attachment_url = wp_get_attachment_url( $attachment_id ); 569 570 if ( $attachment_url !== $redirect_url ) { 571 /* 572 * If an attachment is attached to a post, it inherits the parent post's status. 573 * Fetch the parent post to check its status later. 574 */ 575 if ( $attachment_parent_id ) { 576 $redirect_obj = get_post( $attachment_parent_id ); 577 } 578 579 $redirect_url = $attachment_url; 580 } 581 582 $is_attachment_redirect = true; 583 } 584 585 $redirect['query'] = preg_replace( '#^\??&*?#', '', $redirect['query'] ); 586 587 // Tack on any additional query vars. 588 if ( $redirect_url && ! empty( $redirect['query'] ) ) { 589 parse_str( $redirect['query'], $_parsed_query ); 590 $redirect = parse_url( $redirect_url ); 591 592 if ( ! empty( $_parsed_query['name'] ) && ! empty( $redirect['query'] ) ) { 593 parse_str( $redirect['query'], $_parsed_redirect_query ); 594 595 if ( empty( $_parsed_redirect_query['name'] ) ) { 596 unset( $_parsed_query['name'] ); 597 } 598 } 599 600 $_parsed_query = array_combine( 601 rawurlencode_deep( array_keys( $_parsed_query ) ), 602 rawurlencode_deep( array_values( $_parsed_query ) ) 603 ); 604 605 $redirect_url = add_query_arg( $_parsed_query, $redirect_url ); 606 } 607 608 if ( $redirect_url ) { 609 $redirect = parse_url( $redirect_url ); 610 } 611 612 // www.example.com vs. example.com 613 $user_home = parse_url( home_url() ); 614 615 if ( ! empty( $user_home['host'] ) ) { 616 $redirect['host'] = $user_home['host']; 617 } 618 619 if ( empty( $user_home['path'] ) ) { 620 $user_home['path'] = '/'; 621 } 622 623 // Handle ports. 624 if ( ! empty( $user_home['port'] ) ) { 625 $redirect['port'] = $user_home['port']; 626 } else { 627 unset( $redirect['port'] ); 628 } 629 630 // Notice prevention after new parse_url( $redirect_url ) calls 631 $redirect += array( 632 'host' => '', 633 'path' => '', 634 'query' => '', 635 'scheme' => '', 636 ); 637 638 // Trailing /index.php. 639 $redirect['path'] = preg_replace( '|/' . preg_quote( $wp_rewrite->index, '|' ) . '/*?$|', '/', $redirect['path'] ); 640 641 $punctuation_pattern = implode( 642 '|', 643 array_map( 644 'preg_quote', 645 array( 646 ' ', 647 '%20', // Space. 648 '!', 649 '%21', // Exclamation mark. 650 '"', 651 '%22', // Double quote. 652 "'", 653 '%27', // Single quote. 654 '(', 655 '%28', // Opening bracket. 656 ')', 657 '%29', // Closing bracket. 658 ',', 659 '%2C', // Comma. 660 '.', 661 '%2E', // Period. 662 ';', 663 '%3B', // Semicolon. 664 '{', 665 '%7B', // Opening curly bracket. 666 '}', 667 '%7D', // Closing curly bracket. 668 '%E2%80%9C', // Opening curly quote. 669 '%E2%80%9D', // Closing curly quote. 670 ) 671 ) 672 ); 673 674 // Remove trailing spaces and end punctuation from the path. 675 $redirect['path'] = preg_replace( "#($punctuation_pattern)+$#", '', $redirect['path'] ); 676 677 if ( ! empty( $redirect['query'] ) ) { 678 // Remove trailing spaces and end punctuation from certain terminating query string args. 679 $redirect['query'] = preg_replace( "#((^|&)(p|page_id|cat|tag)=[^&]*?)($punctuation_pattern)+$#", '$1', $redirect['query'] ); 680 681 // Clean up empty query strings. 682 $redirect['query'] = trim( preg_replace( '#(^|&)(p|page_id|cat|tag)=?(&|$)#', '&', $redirect['query'] ), '&' ); 683 684 // Redirect obsolete feeds. 685 $redirect['query'] = preg_replace( '#(^|&)feed=rss(&|$)#', '$1feed=rss2$2', $redirect['query'] ); 686 687 // Remove redundant leading ampersands. 688 $redirect['query'] = preg_replace( '#^\??&*?#', '', $redirect['query'] ); 689 } 690 691 // Strip /index.php/ when we're not using PATHINFO permalinks. 692 if ( ! $wp_rewrite->using_index_permalinks() ) { 693 $redirect['path'] = str_replace( '/' . $wp_rewrite->index . '/', '/', $redirect['path'] ); 694 } 695 696 // Trailing slashes. 697 if ( is_object( $wp_rewrite ) && $wp_rewrite->using_permalinks() 698 && ! $is_attachment_redirect 699 && ! is_404() && ( ! is_front_page() || is_front_page() && get_query_var( 'paged' ) > 1 ) 700 ) { 701 $user_ts_type = ''; 702 703 if ( get_query_var( 'paged' ) > 0 ) { 704 $user_ts_type = 'paged'; 705 } else { 706 foreach ( array( 'single', 'category', 'page', 'day', 'month', 'year', 'home' ) as $type ) { 707 $func = 'is_' . $type; 708 if ( call_user_func( $func ) ) { 709 $user_ts_type = $type; 710 break; 711 } 712 } 713 } 714 715 $redirect['path'] = user_trailingslashit( $redirect['path'], $user_ts_type ); 716 } elseif ( is_front_page() ) { 717 $redirect['path'] = trailingslashit( $redirect['path'] ); 718 } 719 720 // Remove trailing slash for robots.txt or sitemap requests. 721 if ( is_robots() || ! empty( get_query_var( 'sitemap' ) ) ) { 722 $redirect['path'] = untrailingslashit( $redirect['path'] ); 723 } 724 725 // Strip multiple slashes out of the URL. 726 if ( str_contains( $redirect['path'], '//' ) ) { 727 $redirect['path'] = preg_replace( '|/+|', '/', $redirect['path'] ); 728 } 729 730 // Always trailing slash the Front Page URL. 731 if ( trailingslashit( $redirect['path'] ) === trailingslashit( $user_home['path'] ) ) { 732 $redirect['path'] = trailingslashit( $redirect['path'] ); 733 } 734 735 $original_host_low = strtolower( $original['host'] ); 736 $redirect_host_low = strtolower( $redirect['host'] ); 737 738 /* 739 * Ignore differences in host capitalization, as this can lead to infinite redirects. 740 * Only redirect no-www <=> yes-www. 741 */ 742 if ( $original_host_low === $redirect_host_low 743 || ( 'www.' . $original_host_low !== $redirect_host_low 744 && 'www.' . $redirect_host_low !== $original_host_low ) 745 ) { 746 $redirect['host'] = $original['host']; 747 } 748 749 $compare_original = array( $original['host'], $original['path'] ); 750 751 if ( ! empty( $original['port'] ) ) { 752 $compare_original[] = $original['port']; 753 } 754 755 if ( ! empty( $original['query'] ) ) { 756 $compare_original[] = $original['query']; 757 } 758 759 $compare_redirect = array( $redirect['host'], $redirect['path'] ); 760 761 if ( ! empty( $redirect['port'] ) ) { 762 $compare_redirect[] = $redirect['port']; 763 } 764 765 if ( ! empty( $redirect['query'] ) ) { 766 $compare_redirect[] = $redirect['query']; 767 } 768 769 if ( $compare_original !== $compare_redirect ) { 770 $redirect_url = $redirect['scheme'] . '://' . $redirect['host']; 771 772 if ( ! empty( $redirect['port'] ) ) { 773 $redirect_url .= ':' . $redirect['port']; 774 } 775 776 $redirect_url .= $redirect['path']; 777 778 if ( ! empty( $redirect['query'] ) ) { 779 $redirect_url .= '?' . $redirect['query']; 780 } 781 } 782 783 if ( ! $redirect_url || $redirect_url === $requested_url ) { 784 return null; 785 } 786 787 // Hex-encoded octets are case-insensitive. 788 if ( str_contains( $requested_url, '%' ) ) { 789 if ( ! function_exists( 'lowercase_octets' ) ) { 790 /** 791 * Converts the first hex-encoded octet match to lowercase. 792 * 793 * @since 3.1.0 794 * @ignore 795 * 796 * @param array $matches Hex-encoded octet matches for the requested URL. 797 * @return string Lowercased version of the first match. 798 */ 799 function lowercase_octets( $matches ) { 800 return strtolower( $matches[0] ); 801 } 802 } 803 804 $requested_url = preg_replace_callback( '|%[a-fA-F0-9][a-fA-F0-9]|', 'lowercase_octets', $requested_url ); 805 } 806 807 if ( $redirect_obj instanceof WP_Post ) { 808 $post_status_obj = get_post_status_object( get_post_status( $redirect_obj ) ); 809 /* 810 * Unset the redirect object and URL if they are not readable by the user. 811 * This condition is a little confusing as the condition needs to pass if 812 * the post is not readable by the user. That's why there are ! (not) conditions 813 * throughout. 814 */ 815 if ( 816 // Private post statuses only redirect if the user can read them. 817 ! ( 818 $post_status_obj->private && 819 current_user_can( 'read_post', $redirect_obj->ID ) 820 ) && 821 // For other posts, only redirect if publicly viewable. 822 ! is_post_publicly_viewable( $redirect_obj ) 823 ) { 824 $redirect_obj = false; 825 $redirect_url = false; 826 } 827 } 828 829 /** 830 * Filters the canonical redirect URL. 831 * 832 * Returning false to this filter will cancel the redirect. 833 * 834 * @since 2.3.0 835 * 836 * @param string $redirect_url The redirect URL. 837 * @param string $requested_url The requested URL. 838 */ 839 $redirect_url = apply_filters( 'redirect_canonical', $redirect_url, $requested_url ); 840 841 // Yes, again -- in case the filter aborted the request. 842 if ( ! $redirect_url || strip_fragment_from_url( $redirect_url ) === strip_fragment_from_url( $requested_url ) ) { 843 return null; 844 } 845 846 if ( $do_redirect ) { 847 // Protect against chained redirects. 848 if ( ! redirect_canonical( $redirect_url, false ) ) { 849 wp_redirect( $redirect_url, 301 ); 850 exit; 851 } else { 852 // Debug. 853 // die("1: $redirect_url<br />2: " . redirect_canonical( $redirect_url, false ) ); 854 return null; 855 } 856 } else { 857 return $redirect_url; 858 } 859 } 860 861 /** 862 * Removes arguments from a query string if they are not present in a URL 863 * DO NOT use this in plugin code. 864 * 865 * @since 3.4.0 866 * @access private 867 * 868 * @param string $query_string 869 * @param array $args_to_check 870 * @param string $url 871 * @return string The altered query string 872 */ 873 function _remove_qs_args_if_not_in_url( $query_string, array $args_to_check, $url ) { 874 $parsed_url = parse_url( $url ); 875 876 if ( ! empty( $parsed_url['query'] ) ) { 877 parse_str( $parsed_url['query'], $parsed_query ); 878 879 foreach ( $args_to_check as $qv ) { 880 if ( ! isset( $parsed_query[ $qv ] ) ) { 881 $query_string = remove_query_arg( $qv, $query_string ); 882 } 883 } 884 } else { 885 $query_string = remove_query_arg( $args_to_check, $query_string ); 886 } 887 888 return $query_string; 889 } 890 891 /** 892 * Strips the #fragment from a URL, if one is present. 893 * 894 * @since 4.4.0 895 * 896 * @param string $url The URL to strip. 897 * @return string The altered URL. 898 */ 899 function strip_fragment_from_url( $url ) { 900 $parsed_url = wp_parse_url( $url ); 901 902 if ( ! empty( $parsed_url['host'] ) ) { 903 $url = ''; 904 905 if ( ! empty( $parsed_url['scheme'] ) ) { 906 $url = $parsed_url['scheme'] . ':'; 907 } 908 909 $url .= '//' . $parsed_url['host']; 910 911 if ( ! empty( $parsed_url['port'] ) ) { 912 $url .= ':' . $parsed_url['port']; 913 } 914 915 if ( ! empty( $parsed_url['path'] ) ) { 916 $url .= $parsed_url['path']; 917 } 918 919 if ( ! empty( $parsed_url['query'] ) ) { 920 $url .= '?' . $parsed_url['query']; 921 } 922 } 923 924 return $url; 925 } 926 927 /** 928 * Attempts to guess the correct URL for a 404 request based on query vars. 929 * 930 * @since 2.3.0 931 * 932 * @global wpdb $wpdb WordPress database abstraction object. 933 * 934 * @return string|false The correct URL if one is found. False on failure. 935 */ 936 function redirect_guess_404_permalink() { 937 global $wpdb; 938 939 /** 940 * Filters whether to attempt to guess a redirect URL for a 404 request. 941 * 942 * Returning a false value from the filter will disable the URL guessing 943 * and return early without performing a redirect. 944 * 945 * @since 5.5.0 946 * 947 * @param bool $do_redirect_guess Whether to attempt to guess a redirect URL 948 * for a 404 request. Default true. 949 */ 950 if ( false === apply_filters( 'do_redirect_guess_404_permalink', true ) ) { 951 return false; 952 } 953 954 /** 955 * Short-circuits the redirect URL guessing for 404 requests. 956 * 957 * Returning a non-null value from the filter will effectively short-circuit 958 * the URL guessing, returning the passed value instead. 959 * 960 * @since 5.5.0 961 * 962 * @param null|string|false $pre Whether to short-circuit guessing the redirect for a 404. 963 * Default null to continue with the URL guessing. 964 */ 965 $pre = apply_filters( 'pre_redirect_guess_404_permalink', null ); 966 if ( null !== $pre ) { 967 return $pre; 968 } 969 970 if ( get_query_var( 'name' ) ) { 971 $publicly_viewable_statuses = array_filter( get_post_stati(), 'is_post_status_viewable' ); 972 $publicly_viewable_post_types = array_filter( get_post_types( array( 'exclude_from_search' => false ) ), 'is_post_type_viewable' ); 973 974 /** 975 * Filters whether to perform a strict guess for a 404 redirect. 976 * 977 * Returning a truthy value from the filter will redirect only exact post_name matches. 978 * 979 * @since 5.5.0 980 * 981 * @param bool $strict_guess Whether to perform a strict guess. Default false (loose guess). 982 */ 983 $strict_guess = apply_filters( 'strict_redirect_guess_404_permalink', false ); 984 985 if ( $strict_guess ) { 986 $where = $wpdb->prepare( 'post_name = %s', get_query_var( 'name' ) ); 987 } else { 988 $where = $wpdb->prepare( 'post_name LIKE %s', $wpdb->esc_like( get_query_var( 'name' ) ) . '%' ); 989 } 990 991 // If any of post_type, year, monthnum, or day are set, use them to refine the query. 992 if ( get_query_var( 'post_type' ) ) { 993 if ( is_array( get_query_var( 'post_type' ) ) ) { 994 $post_types = array_intersect( get_query_var( 'post_type' ), $publicly_viewable_post_types ); 995 if ( empty( $post_types ) ) { 996 return false; 997 } 998 $where .= " AND post_type IN ('" . implode( "', '", esc_sql( $post_types ) ) . "')"; 999 } else { 1000 if ( ! in_array( get_query_var( 'post_type' ), $publicly_viewable_post_types, true ) ) { 1001 return false; 1002 } 1003 $where .= $wpdb->prepare( ' AND post_type = %s', get_query_var( 'post_type' ) ); 1004 } 1005 } else { 1006 $where .= " AND post_type IN ('" . implode( "', '", esc_sql( $publicly_viewable_post_types ) ) . "')"; 1007 } 1008 1009 if ( get_query_var( 'year' ) ) { 1010 $where .= $wpdb->prepare( ' AND YEAR(post_date) = %d', get_query_var( 'year' ) ); 1011 } 1012 if ( get_query_var( 'monthnum' ) ) { 1013 $where .= $wpdb->prepare( ' AND MONTH(post_date) = %d', get_query_var( 'monthnum' ) ); 1014 } 1015 if ( get_query_var( 'day' ) ) { 1016 $where .= $wpdb->prepare( ' AND DAYOFMONTH(post_date) = %d', get_query_var( 'day' ) ); 1017 } 1018 1019 $query = "SELECT ID FROM $wpdb->posts WHERE $where AND post_status IN ('" . implode( "', '", esc_sql( $publicly_viewable_statuses ) ) . "')"; 1020 1021 // Remove the per-request placeholder escape so the cache key is stable across requests. 1022 $key = md5( $wpdb->remove_placeholder_escape( $query ) ); 1023 $last_changed = wp_cache_get_last_changed( 'posts' ); 1024 $cache_key = "redirect_guess_404_permalink:$key"; 1025 $cache = wp_cache_get_salted( $cache_key, 'post-queries', $last_changed ); 1026 1027 if ( false !== $cache ) { 1028 $post_id = $cache; 1029 } else { 1030 // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared 1031 $post_id = (int) $wpdb->get_var( $query ); 1032 1033 // Cache misses as well as hits. 1034 wp_cache_set_salted( $cache_key, $post_id, 'post-queries', $last_changed ); 1035 } 1036 1037 if ( ! $post_id ) { 1038 return false; 1039 } 1040 1041 if ( get_query_var( 'feed' ) ) { 1042 return get_post_comments_feed_link( $post_id, get_query_var( 'feed' ) ); 1043 } elseif ( get_query_var( 'page' ) > 1 ) { 1044 return trailingslashit( get_permalink( $post_id ) ) . user_trailingslashit( get_query_var( 'page' ), 'single_paged' ); 1045 } else { 1046 return get_permalink( $post_id ); 1047 } 1048 } 1049 1050 return false; 1051 } 1052 1053 /** 1054 * Redirects a variety of shorthand URLs to the admin. 1055 * 1056 * If a user visits example.com/admin, they'll be redirected to /wp-admin. 1057 * Visiting /login redirects to /wp-login.php, and so on. 1058 * 1059 * @since 3.4.0 1060 * 1061 * @global WP_Rewrite $wp_rewrite WordPress rewrite component. 1062 */ 1063 function wp_redirect_admin_locations() { 1064 global $wp_rewrite; 1065 1066 if ( ! ( is_404() && $wp_rewrite->using_permalinks() ) ) { 1067 return; 1068 } 1069 1070 $admins = array( 1071 home_url( 'wp-admin', 'relative' ), 1072 home_url( 'dashboard', 'relative' ), 1073 home_url( 'admin', 'relative' ), 1074 site_url( 'dashboard', 'relative' ), 1075 site_url( 'admin', 'relative' ), 1076 ); 1077 1078 if ( in_array( untrailingslashit( $_SERVER['REQUEST_URI'] ), $admins, true ) ) { 1079 wp_redirect( admin_url() ); 1080 exit; 1081 } 1082 1083 $logins = array( 1084 home_url( 'wp-login.php', 'relative' ), 1085 home_url( 'login.php', 'relative' ), 1086 home_url( 'login', 'relative' ), 1087 site_url( 'login', 'relative' ), 1088 ); 1089 1090 if ( in_array( untrailingslashit( $_SERVER['REQUEST_URI'] ), $logins, true ) ) { 1091 wp_redirect( wp_login_url() ); 1092 exit; 1093 } 1094 1095 /** 1096 * Filters the well-known change password URL (/.well-known/change-password). 1097 * 1098 * By default visiting the well-known change password URL will redirect to 1099 * the user's profile page when requested by a browser or password manager. 1100 * 1101 * Sites with a custom URL structure for modifying the passwords can modify 1102 * the destination with this filter. 1103 * 1104 * The resulting URL is redirected using {@see wp_safe_redirect}, if the 1105 * filtered URL is on an external host, that host will need to be included 1106 * as an allowed host via the {@see 'allowed_redirect_hosts'} filter. 1107 * 1108 * @since 7.2.0 1109 * 1110 * @param string $change_password_url The URL to redirect to. Default is the user profile page. 1111 */ 1112 $change_password_url = apply_filters( 'wp_change_password_url', admin_url( 'profile.php' ) ); 1113 1114 /* 1115 * The well-known change-password URL is always at the root of the origin 1116 * per the spec (https://w3c.github.io/webappsec-change-password-url/), so 1117 * only the root-relative path is matched here, regardless of whether 1118 * WordPress is installed in a subdirectory. 1119 */ 1120 if ( 1121 '/.well-known/change-password' === untrailingslashit( $_SERVER['REQUEST_URI'] ) 1122 && $change_password_url 1123 && wp_safe_redirect( $change_password_url ) 1124 ) { 1125 exit; 1126 } 1127 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated : Mon Nov 23 08:20:01 2026 | Cross-referenced by PHPXref |