Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Detect old category permalinks
- */
- function bis_detect_old_permalinks() {
- global $wp, $wpdb;
- preg_match('/index.php(.*)?\/([^\/]+)/', trim($_SERVER['REQUEST_URI'], '/'), $parts);
- if(!empty($parts[2])) {
- $slug = sanitize_text_field($parts[2]);
- // A. Check for term
- $term = $wpdb->get_row($wpdb->prepare("SELECT t.slug, tt.taxonomy FROM $wpdb->terms AS t LEFT JOIN $wpdb->term_taxonomy AS tt ON (t.term_id = tt.term_id) WHERE slug = %s", $slug));
- if(!empty($term->slug)) {
- $term_link = get_term_link($term->slug, $term->taxonomy);
- wp_safe_redirect($term_link, 301);
- exit();
- }
- // B. Check for post
- $post = $wpdb->get_row($wpdb->prepare("SELECT ID, post_date FROM $wpdb->posts WHERE post_name = %s AND post_status = 'publish' ORDER BY post_date DESC", $slug));
- if(!empty($post->ID)) {
- $post_link = get_permalink($post->ID);
- wp_safe_redirect($post_link, 301);
- exit();
- }
- }
- }
- add_action('template_redirect', 'bis_detect_old_permalinks', 0);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement