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