Skip to content

Instantly share code, notes, and snippets.

@sscovil
Last active December 19, 2015 05:39
Show Gist options
  • Save sscovil/5905863 to your computer and use it in GitHub Desktop.
Save sscovil/5905863 to your computer and use it in GitHub Desktop.
Written in response to a WP support forum question.
<?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' );
@sscovil
Copy link
Author

sscovil commented Jul 2, 2013

@sscovil
Copy link
Author

sscovil commented Jul 3, 2013

Revised the old code to use the WP core function wp_redirect.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment