</php
/** function to add page slug in preview link of post and page
*
* @param type $preview_link
* @param type $post
* @return type string
*/
function custom_preview_post_link($preview_link, $post) {
/*
Firstly Check post status, if its draft/pending/auto-draft post then checks Db for post name. If post name is empty then sanititize page title. If post name and post title both are empty then use post id
Add resultant of above as a Query param 'url' to preview link of post.
*/
$pname = $post->post_name;
if (in_array($post->post_status, array('draft', 'pending', 'auto-draft'))) {
$pname = sanitize_title($post->post_name ? $post->post_name : $post->post_title, $post->ID);
}
$query_args = array();
$query_args['url'] = $pname;
$preview_link = add_query_arg($query_args, $preview_link);
return $preview_link;
}
add_filter('preview_post_link', 'custom_preview_post_link', 10, 2);
add_filter('preview_page_link', 'custom_preview_post_link', 10, 2);