Skip to content

Instantly share code, notes, and snippets.

@heyjones
Created October 26, 2015 18:18
Show Gist options
  • Save heyjones/3c0fa0e0536321d73fe3 to your computer and use it in GitHub Desktop.
Save heyjones/3c0fa0e0536321d73fe3 to your computer and use it in GitHub Desktop.
WP Redirects
function wp_redirects(){
$redirects = array(
'/old/' => '/new/'
);
$protocol = isset( $_SERVER['HTTPS'] ) ? 'https://' : 'http://';
$site_url = str_replace( $protocol, '', get_site_url() );
$url = str_replace( $site_url, '', $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] );
$redirect = $redirects[ $url ];
if( isset( $redirect )){
$redirect = $protocol . $site_url . $redirect;
wp_redirect( $redirect, 301 );
exit;
}else{
global $wp_query;
$wp_query->set_404();
status_header( 404 );
}
}
add_action( 'init', 'wp_redirects', 100 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment