Last active
December 19, 2015 05:39
-
-
Save sscovil/5905863 to your computer and use it in GitHub Desktop.
Written in response to a WP support forum question.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* WordPress Conditional Redirect Function | |
* | |
* In this example, the conditional tag checks if the current page is a page with a slug of test and, | |
* if true, replaces example.com with test.example.com and redirects. | |
*/ | |
function my_custom_redirect() { | |
if ( is_page( 'test' ) ) { | |
global $wp; | |
$current_url = add_query_arg( $wp->query_string, '', home_url( $wp->request ) ); | |
wp_redirect( preg_replace( 'example.com', 'test.example.com', $current_url ), 301 ); | |
exit; | |
} | |
} | |
add_action( 'wp', 'my_custom_redirect' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Revised the old code to use the WP core function wp_redirect.