/**
* Redirect a "/%postname%/" permalink structure to the their new URL.
*
* @link http://wordpress.stackexchange.com/q/136306/1685
*/
function wpse_136306_redirect() {
global $wp;
// Check that we\'ve hit a 404 for a non-empty permalink without slashes
if ( is_404() && strlen( $wp->request ) && strpos( $wp->request, \'/\' ) === false ) {
// Attempt to find a post that matches the request.
$posts = get_posts(
array(
\'posts_per_page\' => 1,
\'name\' => $wp->request,
)
);
if ( $posts && $url = get_permalink( $posts[0]->ID ) ) {
// All good, let\'s redirect to the new URL.
wp_redirect( $url, 301 );
exit;
}
}
}
add_action( \'template_redirect\', \'wpse_136306_redirect\', 1 );