Skip to content

Instantly share code, notes, and snippets.

@norcross
Created February 4, 2014 16:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save norcross/8806677 to your computer and use it in GitHub Desktop.
Save norcross/8806677 to your computer and use it in GitHub Desktop.
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