Created
July 17, 2018 13:39
-
-
Save mam4dali/68b8f2c2f4eccc54855eb4b539b487ae to your computer and use it in GitHub Desktop.
wordpress redirect tiny post url to long post slug
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 | |
/* | |
**** into functions.php **** | |
Example: | |
redirect tiny url: http://site.com/?p=1 | |
to long post slug: http://site.com/my-post/ | |
*/ | |
add_action('template_redirect', 'redirect_tiny_url_to_long'); | |
function redirect_tiny_url_to_long(){ | |
if(is_single()){ | |
if(isset($_GET['p']) && is_numeric($_GET['p']) && intval($_GET['p'])){ | |
$p_id = (int)intval($_GET['p']); | |
if($p_id != 0){ | |
$post_link = get_permalink($p_id); | |
if($post_link && !empty($post_link) && stripos($post_link, '?p='.$p_id) === false){ | |
wp_redirect( $post_link, 301 ); | |
exit(); | |
} | |
} | |
} | |
} | |
return; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment