diff --git a/wp-admin/admin-ajax.php b/wp-admin/admin-ajax.php index fee91f81b7..5964218a7e 100644 --- a/wp-admin/admin-ajax.php +++ b/wp-admin/admin-ajax.php @@ -571,7 +571,9 @@ break; case 'sample-permalink': check_ajax_referer( 'samplepermalink', 'samplepermalinknonce' ); $post_id = isset($_POST['post_id'])? intval($_POST['post_id']) : 0; - die(get_sample_permalink_html($post_id, $_POST['new_title'], $_POST['new_slug'])); + $title = isset($_POST['new_title'])? $_POST['new_title'] : ''; + $slug = isset($_POST['new_slug'])? $_POST['new_slug'] : ''; + die(get_sample_permalink_html($post_id, $title, $slug)); break; default : do_action( 'wp_ajax_' . $_POST['action'] ); diff --git a/wp-admin/includes/post.php b/wp-admin/includes/post.php index 000269597c..9c6ac14fac 100644 --- a/wp-admin/includes/post.php +++ b/wp-admin/includes/post.php @@ -581,7 +581,7 @@ function postbox_classes( $id, $page ) { } } -function get_sample_permalink($id, $title, $name = null) { +function get_sample_permalink($id, $title=null, $name = null) { $post = &get_post($id); if (!$post->ID) { return array('', ''); @@ -589,17 +589,19 @@ function get_sample_permalink($id, $title, $name = null) { $original_status = $post->post_status; $original_date = $post->post_date; $original_name = $post->post_name; - $original_title = $post->post_title; - - $post->post_title = $title; - $post->post_name = sanitize_title($post->post_name? $post->post_name : $post->post_title, $post->ID); + // Hack: get_permalink would return ugly permalink for + // drafts, so we will fake, that our post is published if (in_array($post->post_status, array('draft', 'pending'))) { $post->post_status = 'publish'; $post->post_date = date('Y-m-d H:i:s'); + $post->post_name = sanitize_title($post->post_name? $post->post_name : $post->post_title, $post->ID); } + + // If the user wants to set a new name -- override the current one + // Note: if empty name is supplied -- use the title instead, see #6072 if (!is_null($name)) { - $post->post_name = sanitize_title($name? $name : $post->post_title, $post->ID); + $post->post_name = sanitize_title($name? $name : $title, $post->ID); } $permalink = get_permalink($post, true); diff --git a/wp-includes/js/autosave.js b/wp-includes/js/autosave.js index b96fcc783d..96ea8d672c 100644 --- a/wp-includes/js/autosave.js +++ b/wp-includes/js/autosave.js @@ -92,6 +92,7 @@ function autosave_update_slug(post_id) { { action: 'sample-permalink', post_id: post_id, + new_title: jQuery('#title').val(), samplepermalinknonce: jQuery('#samplepermalinknonce').val() }, function(data) {