Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save mam4dali/68b8f2c2f4eccc54855eb4b539b487ae to your computer and use it in GitHub Desktop.
Save mam4dali/68b8f2c2f4eccc54855eb4b539b487ae to your computer and use it in GitHub Desktop.
wordpress redirect tiny post url to long post slug
<?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