Remember old post slugs and automatically redirect to the new slug. fixes #3202
git-svn-id: http://svn.automattic.com/wordpress/trunk@4556 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
parent
9af8606115
commit
42529f4516
|
@ -2069,6 +2069,43 @@ function wp_reset_vars( $vars ) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
function wp_check_for_changed_slugs($post_id) {
|
||||||
|
if ( !strlen($_POST['wp-old-slug']) )
|
||||||
|
return $post_id;
|
||||||
|
|
||||||
|
$post = &get_post($post_id);
|
||||||
|
|
||||||
|
// we're only concerned with published posts
|
||||||
|
if ( $post->post_status != 'publish' || $post->post_type != 'post' )
|
||||||
|
return $post_id;
|
||||||
|
|
||||||
|
// only bother if the slug has changed
|
||||||
|
if ( $post->post_name == $_POST['wp-old-slug'] )
|
||||||
|
return $post_id;
|
||||||
|
|
||||||
|
$old_slugs = get_post_meta($post_id, '_wp_old_slug');
|
||||||
|
|
||||||
|
// if we haven't added this old slug before, add it now
|
||||||
|
if ( !count($old_slugs) || !in_array($_POST['wp-old-slug'], $old_slugs) )
|
||||||
|
add_post_meta($post_id, '_wp_old_slug', $_POST['wp-old-slug']);
|
||||||
|
|
||||||
|
// if the new slug was used previously, delete it from the list
|
||||||
|
if ( in_array($post->post_name, $old_slugs) )
|
||||||
|
delete_post_meta($post_id, '_wp_old_slug', $post->post_name);
|
||||||
|
|
||||||
|
return $post_id;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
function wp_remember_old_slug() {
|
||||||
|
global $post;
|
||||||
|
$name = wp_specialchars($post->post_name); // just in case
|
||||||
|
if ( strlen($name) )
|
||||||
|
echo '<input type="hidden" id="wp-old-slug" name="wp-old-slug" value="' . $name . '" />';
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
// If siteurl or home changed, reset cookies and flush rewrite rules.
|
// If siteurl or home changed, reset cookies and flush rewrite rules.
|
||||||
function update_home_siteurl( $old_value, $value ) {
|
function update_home_siteurl( $old_value, $value ) {
|
||||||
global $wp_rewrite, $user_login, $user_pass_md5;
|
global $wp_rewrite, $user_login, $user_pass_md5;
|
||||||
|
|
|
@ -175,6 +175,9 @@ function upgrade_all() {
|
||||||
if ( $wp_current_db_version < 3845 )
|
if ( $wp_current_db_version < 3845 )
|
||||||
upgrade_210();
|
upgrade_210();
|
||||||
|
|
||||||
|
if ( $wp_current_db_version < 4351 )
|
||||||
|
upgrade_old_slugs();
|
||||||
|
|
||||||
$wp_rewrite->flush_rules();
|
$wp_rewrite->flush_rules();
|
||||||
|
|
||||||
update_option('db_version', $wp_db_version);
|
update_option('db_version', $wp_db_version);
|
||||||
|
@ -544,6 +547,13 @@ function upgrade_210() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function upgrade_old_slugs() {
|
||||||
|
// upgrade people who were using the Redirect Old Slugs plugin
|
||||||
|
global $wpdb;
|
||||||
|
$wpdb->query("UPDATE $wpdb->postmeta SET meta_key = '_wp_old_slug' WHERE meta_key = 'old_slug'");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
// The functions we use to actually do stuff
|
// The functions we use to actually do stuff
|
||||||
|
|
||||||
// General
|
// General
|
||||||
|
|
|
@ -146,6 +146,11 @@ add_filter('option_blog_charset', 'wp_specialchars');
|
||||||
add_filter('mce_plugins', '_mce_load_rtl_plugin');
|
add_filter('mce_plugins', '_mce_load_rtl_plugin');
|
||||||
add_filter('mce_buttons', '_mce_add_direction_buttons');
|
add_filter('mce_buttons', '_mce_add_direction_buttons');
|
||||||
|
|
||||||
|
// Redirect Old Slugs
|
||||||
|
add_action('template_redirect', 'wp_old_slug_redirect');
|
||||||
|
add_action('edit_post', 'wp_check_for_changed_slugs');
|
||||||
|
add_action('edit_form_advanced', 'wp_remember_old_slug');
|
||||||
|
|
||||||
// Actions
|
// Actions
|
||||||
add_action('wp_head', 'rsd_link');
|
add_action('wp_head', 'rsd_link');
|
||||||
add_action('wp_head', 'locale_stylesheet');
|
add_action('wp_head', 'locale_stylesheet');
|
||||||
|
|
|
@ -1116,6 +1116,40 @@ class WP_Query {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// Redirect old slugs
|
||||||
|
function wp_old_slug_redirect () {
|
||||||
|
global $wp_query;
|
||||||
|
if ( is_404() && '' != $wp_query->query_vars['name'] ) :
|
||||||
|
global $wpdb;
|
||||||
|
|
||||||
|
$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'] . "'";
|
||||||
|
|
||||||
|
// if year, monthnum, or day have been specified, make our query more precise
|
||||||
|
// just in case there are multiple identical _wp_old_slug values
|
||||||
|
if ( '' != $wp_query->query_vars['year'] )
|
||||||
|
$query .= " AND YEAR(post_date) = '{$wp_query->query_vars['year']}'";
|
||||||
|
if ( '' != $wp_query->query_vars['monthnum'] )
|
||||||
|
$query .= " AND MONTH(post_date) = '{$wp_query->query_vars['monthnum']}'";
|
||||||
|
if ( '' != $wp_query->query_vars['day'] )
|
||||||
|
$query .= " AND DAYOFMONTH(post_date) = '{$wp_query->query_vars['day']}'";
|
||||||
|
|
||||||
|
$id = (int) $wpdb->get_var($query);
|
||||||
|
|
||||||
|
if ( !$id )
|
||||||
|
return;
|
||||||
|
|
||||||
|
$link = get_permalink($id);
|
||||||
|
|
||||||
|
if ( !$link )
|
||||||
|
return;
|
||||||
|
|
||||||
|
wp_redirect($link, '301'); // Permanent redirect
|
||||||
|
exit;
|
||||||
|
endif;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
//
|
//
|
||||||
// Private helper functions
|
// Private helper functions
|
||||||
//
|
//
|
||||||
|
|
|
@ -3,6 +3,6 @@
|
||||||
// This holds the version number in a separate file so we can bump it without cluttering the SVN
|
// This holds the version number in a separate file so we can bump it without cluttering the SVN
|
||||||
|
|
||||||
$wp_version = '2.1-alpha3';
|
$wp_version = '2.1-alpha3';
|
||||||
$wp_db_version = 4350;
|
$wp_db_version = 4351;
|
||||||
|
|
||||||
?>
|
?>
|
||||||
|
|
Loading…
Reference in New Issue