Skip to content

Instantly share code, notes, and snippets.

@k4zuki02h4t4
Created May 6, 2017 10:08
Show Gist options
  • Save k4zuki02h4t4/12b19cc5074eafe1abd2e2042d6dbea2 to your computer and use it in GitHub Desktop.
Save k4zuki02h4t4/12b19cc5074eafe1abd2e2042d6dbea2 to your computer and use it in GitHub Desktop.
WordPress redirect。
<?php
function vasimpleredirect_redirect() {
global $post, $pagenow;
if ( is_admin() || is_user_logged_in() || 'wp-login.php' === $pagenow ) {
return;
}
$front_id = get_option( 'page_on_front' );
$posts_id = get_option( 'page_for_posts' );
$redirect_url = untrailingslashit( 'https://www.visualive.jp' );
$redirect_path = untrailingslashit( '/blog/archives' );
$redirect_name = '%post_id%';
if ( is_front_page() && ! is_home() ) {
$post_id = $front_id;
} elseif ( is_home() && ! is_front_page() ) {
$post_id = $posts_id;
} elseif ( false !== get_the_ID() ) {
$post_id = get_the_ID();
} else {
$post_id = $post->ID;
}
$_post = get_post( $post_id );
$redirect = trailingslashit( $redirect_url ) . trailingslashit( $redirect_path ) . trailingslashit( $redirect_name );
$redirect = str_replace( '%post_id%', $_post->ID, $redirect );
$redirect = str_replace( '%post_name%', $_post->post_name, $redirect );
if ( ( is_front_page() && ! is_home() ) || ( is_front_page() && is_home() ) ) {
wp_redirect( trailingslashit( $redirect_url ), 301 );
} elseif ( is_home() && ! is_front_page() ) {
wp_redirect( $redirect_url . trailingslashit( '/blog' ), 301 );
} elseif ( is_single() ) {
wp_redirect( $redirect, 301 );
}
}
add_action( 'template_redirect', 'vasimpleredirect_redirect', - 1 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment