Skip to content

Commit

Permalink
mu-plugin: Redirect old content to the new URL structure.
Browse files Browse the repository at this point in the history
  • Loading branch information
ryelle committed Jan 18, 2023
1 parent b49c351 commit 564bb2e
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions source/wp-content/mu-plugins/site-documentation.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
namespace WordPressdotorg\Documentation_2022\MU_Plugin;

add_action( 'template_redirect', __NAMESPACE__ . '\redirect_to_google_search' );
add_action( 'template_redirect', __NAMESPACE__ . '\redirect_old_content', 9 ); // Before redirect_canonical();

/**
* Redirects search results to Google Custom Search.
Expand All @@ -26,3 +27,49 @@ function redirect_to_google_search() {
exit;
}
}

/**
* Redirects old content to the new URL structure.
*
* This does not include the article renaming since core will handle that.
*/
function redirect_old_content() {
$path_redirects = [
// Removed articles.
'/documentation/article/wordpress-features/' => 'https://wordpress.org/about/features/',
'/documentation/article/requirements/' => 'https://wordpress.org/about/requirements/',

// Renamed categories.
'/documentation/category/basic-administration/' => '/documentation/category/dashboard/',
'/documentation/category/troubleshooting/' => '/documentation/category/faqs/',
'/documentation/category/installation/' => '/documentation/category/installation/',
'/documentation/category/maintenance/' => '/documentation/category/maintenance/',
'/documentation/category/security/' => '/documentation/category/security/',
'/documentation/category/getting-started/' => '/documentation/category/where-to-start/',

// Top-level category landing pages.
'/documentation/category/customizing/' => '/documentation/customization/',
'/documentation/category/basic-usage/' => '/documentation/support-guides/',

// @todo When the Advanced Administration handbook is updated, add those redirects here.
];

$request_uri = $_SERVER['REQUEST_URI'] ?? '/documentation-test/'; // phpcs:ignore

foreach ( $path_redirects as $old_path => $new_url ) {
if ( str_starts_with( $request_uri, $old_path ) ) {
do_redirect_and_exit( $new_url );
}
}
}

/**
* Do the 301 redirect and exit the script.
*/
function do_redirect_and_exit( $location ) {
header_remove( 'expires' );
header_remove( 'cache-control' );

wp_safe_redirect( $location, 301 );
exit;
}

0 comments on commit 564bb2e

Please sign in to comment.