Created
October 16, 2024 19:47
-
-
Save mujahidi/81ed3d5b8bf6a44ffcfdef5ee605820c to your computer and use it in GitHub Desktop.
Redirect old /hello-world to /blog/hello-world URL
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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