Skip to content

Instantly share code, notes, and snippets.

@steve10287
Created October 12, 2017 11:58
Show Gist options
  • Save steve10287/eddcf65e90d50a01b2b08d56db594443 to your computer and use it in GitHub Desktop.
Save steve10287/eddcf65e90d50a01b2b08d56db594443 to your computer and use it in GitHub Desktop.
Polylang Canonical Tags with Yoast's API
<?php
/**
This is used on a site where blog posts are in each english region, to avoid content duplication this method generates
the canonical for the main English site language
**/
function is_blog_page () {
global $post;
$posttype = get_post_type($post );
return ( ((is_archive()) || (is_author()) || (is_category()) || (is_home()) || (is_single()) || (is_tag())) && ( $posttype == 'post') ) ? true : false ;
}
add_filter( 'wpseo_canonical', 'blog_canonical_generator' );
/**
* Get the EN transation for english speaking blog posts, to avoid content duplication across english languages.
* @param string url - Yoast's canonical url for the queried object
* @return string - EN url or default for non english speaking
*/
function blog_canonical_generator($url) {
$currentLanguage = pll_current_language();
$englishSlugs = array('en-nz', 'en-gb', 'en-us', 'en-ca', 'en-au');
if ( is_home() ) {
$post = pll_get_post( get_option( 'page_for_posts' ), 'en' );
} else if ( is_blog_page() ) {
$post = pll_get_post( get_queried_object_id(), 'en' );
}
if ( isset($post) && in_array($currentLanguage, $englishSlugs) ) {
return get_permalink($post);
}
return $url;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment