Skip to content

Instantly share code, notes, and snippets.

@mujahidi
Created October 16, 2024 19:47
Show Gist options
  • Save mujahidi/81ed3d5b8bf6a44ffcfdef5ee605820c to your computer and use it in GitHub Desktop.
Save mujahidi/81ed3d5b8bf6a44ffcfdef5ee605820c to your computer and use it in GitHub Desktop.
Redirect old /hello-world to /blog/hello-world URL
<?php
add_action('template_redirect', 'redirect_old_post_urls');
function redirect_old_post_urls() {
if (is_404()) {
global $wp;
$request = $wp->request;
$post = get_page_by_path($request, OBJECT, 'post');
if ($post) {
wp_redirect(home_url("/blog/{$request}/"), 301);
exit;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment