Skip to content

Instantly share code, notes, and snippets.

@AntonLitvin
Created November 16, 2020 08:03
Show Gist options
  • Save AntonLitvin/c480810e6d1c47a301c1a11ea3513d7a to your computer and use it in GitHub Desktop.
Save AntonLitvin/c480810e6d1c47a301c1a11ea3513d7a to your computer and use it in GitHub Desktop.
Переадресация для страниц с разными урлами CPT
add_action( 'template_redirect', 'rider_redirect_canonical' );
function rider_redirect_canonical(){
if ( is_single() && ('post' === get_post_type()) ) {
$tax = 'category';
// проверяем наличие терминов, иначе редирект бессмысленный
if ( $terms = get_the_terms(get_the_ID(), $tax) ) {
// собираем ссылку куда редиректить
if ( isset( $_SERVER['HTTP_HOST'] ) ) {
$requested_url = is_ssl() ? 'https://' : 'http://';
$requested_url .= $_SERVER['HTTP_HOST'];
$requested_url .= $_SERVER['REQUEST_URI'];
}
//vardump($requested_url);
//vardump(get_the_permalink());
// сверяем с текущей
if ( $requested_url != get_the_permalink() ) {
wp_redirect( get_the_permalink(), 301 );
exit();
}
} // end if terms
} // end if is_single()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment