WordPress.org

Make WordPress Core

Changeset 4556


Ignore:
Timestamp:
11/30/06 08:48:56 (7 years ago)
Author:
markjaquith
Message:

Remember old post slugs and automatically redirect to the new slug. fixes #3202

Location:
trunk
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-admin/admin-functions.php

    r4552 r4556  
    20702070} 
    20712071 
     2072 
     2073function wp_check_for_changed_slugs($post_id) { 
     2074    if ( !strlen($_POST['wp-old-slug']) ) 
     2075        return $post_id; 
     2076 
     2077    $post = &get_post($post_id); 
     2078 
     2079    // we're only concerned with published posts 
     2080    if ( $post->post_status != 'publish' || $post->post_type != 'post' ) 
     2081        return $post_id; 
     2082 
     2083    // only bother if the slug has changed 
     2084    if ( $post->post_name == $_POST['wp-old-slug'] ) 
     2085        return $post_id; 
     2086 
     2087    $old_slugs = get_post_meta($post_id, '_wp_old_slug'); 
     2088 
     2089    // if we haven't added this old slug before, add it now 
     2090    if ( !count($old_slugs) || !in_array($_POST['wp-old-slug'], $old_slugs) ) 
     2091        add_post_meta($post_id, '_wp_old_slug', $_POST['wp-old-slug']); 
     2092 
     2093    // if the new slug was used previously, delete it from the list 
     2094    if ( in_array($post->post_name, $old_slugs) ) 
     2095        delete_post_meta($post_id, '_wp_old_slug', $post->post_name); 
     2096 
     2097    return $post_id; 
     2098} 
     2099 
     2100 
     2101function wp_remember_old_slug() { 
     2102    global $post; 
     2103    $name = wp_specialchars($post->post_name); // just in case 
     2104    if ( strlen($name) ) 
     2105        echo '<input type="hidden" id="wp-old-slug" name="wp-old-slug" value="' . $name . '" />'; 
     2106} 
     2107 
     2108 
    20722109// If siteurl or home changed, reset cookies and flush rewrite rules. 
    20732110function update_home_siteurl( $old_value, $value ) { 
  • trunk/wp-admin/upgrade-functions.php

    r4495 r4556  
    175175    if ( $wp_current_db_version < 3845 ) 
    176176        upgrade_210(); 
     177 
     178    if ( $wp_current_db_version < 4351 ) 
     179        upgrade_old_slugs(); 
    177180 
    178181    $wp_rewrite->flush_rules(); 
     
    544547    } 
    545548} 
     549 
     550function upgrade_old_slugs() { 
     551    // upgrade people who were using the Redirect Old Slugs plugin 
     552    global $wpdb; 
     553    $wpdb->query("UPDATE $wpdb->postmeta SET meta_key = '_wp_old_slug' WHERE meta_key = 'old_slug'"); 
     554} 
     555 
    546556 
    547557// The functions we use to actually do stuff 
  • trunk/wp-includes/default-filters.php

    r4265 r4556  
    147147add_filter('mce_buttons', '_mce_add_direction_buttons'); 
    148148 
     149// Redirect Old Slugs 
     150add_action('template_redirect', 'wp_old_slug_redirect'); 
     151add_action('edit_post', 'wp_check_for_changed_slugs'); 
     152add_action('edit_form_advanced', 'wp_remember_old_slug'); 
     153 
    149154// Actions 
    150155add_action('wp_head', 'rsd_link'); 
  • trunk/wp-includes/query.php

    r4517 r4556  
    11171117} 
    11181118 
     1119 
     1120// Redirect old slugs 
     1121function wp_old_slug_redirect () { 
     1122    global $wp_query; 
     1123    if ( is_404() && '' != $wp_query->query_vars['name'] ) : 
     1124        global $wpdb; 
     1125 
     1126        $query = "SELECT post_id FROM $wpdb->postmeta, $wpdb->posts WHERE ID = post_id AND meta_key = '_wp_old_slug' AND meta_value='" . $wp_query->query_vars['name'] . "'"; 
     1127 
     1128        // if year, monthnum, or day have been specified, make our query more precise 
     1129        // just in case there are multiple identical _wp_old_slug values 
     1130        if ( '' != $wp_query->query_vars['year'] ) 
     1131            $query .= " AND YEAR(post_date) = '{$wp_query->query_vars['year']}'"; 
     1132        if ( '' != $wp_query->query_vars['monthnum'] ) 
     1133            $query .= " AND MONTH(post_date) = '{$wp_query->query_vars['monthnum']}'"; 
     1134        if ( '' != $wp_query->query_vars['day'] ) 
     1135            $query .= " AND DAYOFMONTH(post_date) = '{$wp_query->query_vars['day']}'"; 
     1136 
     1137        $id = (int) $wpdb->get_var($query); 
     1138 
     1139        if ( !$id ) 
     1140            return; 
     1141 
     1142        $link = get_permalink($id); 
     1143 
     1144        if ( !$link ) 
     1145            return; 
     1146 
     1147        wp_redirect($link, '301'); // Permanent redirect 
     1148        exit; 
     1149    endif; 
     1150} 
     1151 
     1152 
    11191153// 
    11201154// Private helper functions 
  • trunk/wp-includes/version.php

    r4551 r4556  
    44 
    55$wp_version = '2.1-alpha3'; 
    6 $wp_db_version = 4350; 
     6$wp_db_version = 4351; 
    77 
    88?> 
Note: See TracChangeset for help on using the changeset viewer.