Advertisement
mbis

Redirect post/page IDs

Jan 28th, 2020
335
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.46 KB | None | 0 0
  1. function pm_deep_detect_permalinks() {
  2.     global $wp, $wp_query;
  3.  
  4.     // Works only if 404 error is set before when the WP query is parsed
  5.     if(empty($wp_query->query['do_not_redirect']) && is_numeric($wp->request)) {
  6.         $post_id = (int) $wp->request;
  7.  
  8.         // Redirect to new permalink
  9.         if($post_id) {
  10.             $correct_permalink = get_permalink($post_id);
  11.             wp_safe_redirect($correct_permalink, 301);
  12.         }
  13.     }
  14. }
  15. add_filter('template_redirect', 'pm_deep_detect_permalinks', 0);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement