Last active
November 15, 2017 15:12
-
-
Save codfish/7b8c8f4c4f085429f07c to your computer and use it in GitHub Desktop.
Wordpress short links don't automatically redirect to the post permalink, like some (me) might think they should. If you switch your permalink structure to something more SEO friendly, existing posts should automatically redirect to the proper format. However, future posts will not. If you want all "short links" (mywordpresssite.com/?p=123456) t…
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('init', 'redirect_short_links'); | |
| function redirect_short_links() | |
| { | |
| $id = $_GET['p']; | |
| // do not redirect post previews | |
| if (! $id || !preg_match('/^\d+$/', $id) || !empty($_GET['preview'])) { | |
| return; | |
| } | |
| $permalink = get_the_permalink($id); | |
| if ($permalink) { | |
| header('HTTP/1.1 301 Moved Permanently'); | |
| header("Location: {$permalink}"); | |
| exit(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment