Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function pm_detect_old_slugs() {
- global $wp_query, $wp, $wpdb;
- // Stop if any post or term was already found by Permalink Manager
- if(!empty($wp_query->query['do_not_redirect']) || !empty($wp_query->query_vars['do_not_redirect']) || (!is_404())) {
- return;
- }
- // Get the string from URL
- preg_match('/([^\/]+)(?:\/([\d]+))?/', $wp->request, $parts);
- if(!empty($parts[1])) {
- $post_id = $wpdb->get_var($wpdb->prepare("SELECT post_id from {$wpdb->postmeta} WHERE meta_key = '_wp_old_slug' AND meta_value LIKE '%%%s%%'", $parts[1]));
- // Redirect if post with old slug was found
- if(!empty($post_id)) {
- $permalink = get_permalink($post_id);
- $permalink = (!empty($parts[2])) ? sprintf('%s/%d', trim($permalink, '/'), $parts[2]) : '';
- wp_safe_redirect($permalink, 301);
- exit();
- }
- }
- }
- add_action('template_redirect', 'pm_detect_old_slugs', 0);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement