Skip to content

Instantly share code, notes, and snippets.

@ataylorme
Last active December 17, 2015 16:29
Show Gist options
  • Save ataylorme/5639434 to your computer and use it in GitHub Desktop.
Save ataylorme/5639434 to your computer and use it in GitHub Desktop.
<?php
/* ========== 301 REDIRECTS ========== */
function my_wordpress_301_redirects() {
/* Bail if on the front page */
if( is_front_page() )
return;
$redirect_pages = array(
/* Pass a string to redirect to a new page */
'slug1' => 'new-slug1',
'slug2' => 'new-slug2',
/* Pass false to redirect to the homepage */
'slug3' => false,
'slug4' => false,
'slug5.xml' => 'new-slug3.xml',
'slug6.php' => 'new-slug4.php',
'slug7.html' => 'new-slug5.html'
);
$slug = array_pop(array_filter(explode('/', $_SERVER["REQUEST_URI"])));
/* Bail if slug is empty */
if( empty($slug) )
return;
foreach( $redirect_pages as $old=>$new ):
if( $slug == $old ):
if( $new == false )
wp_redirect( get_bloginfo('url'), 301 );
else
wp_redirect( get_bloginfo('url') . '/' . $new . '/', 301 );
exit;
endif;
endforeach;
} //end my_wordpress_301_redirects
add_action( 'get_header', 'my_wordpress_301_redirects', 10 );
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment