Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
set up 404 redirects from /%postname%/ to /blog/%category%/%postname%/
<?php
/** set up redirects from old structure */
function aly_post_redirects() {
if ( ! is_user_logged_in() )
return;
if ( is_404() ) :
$slug = $_SERVER['REQUEST_URI'];
$slug = str_replace( '/', '', $slug );
$item = get_page_by_path( $slug, OBJECT, 'post' );
if ( ! $item )
return;
$post_link = get_permalink( $item->ID );
if ( ! $post_link )
return;
wp_redirect( esc_url_raw( $post_link ), 301 );
exit;
endif;
}
add_action ( 'template_redirect', 'aly_post_redirects', 1 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment