Created
October 12, 2017 11:58
-
-
Save steve10287/eddcf65e90d50a01b2b08d56db594443 to your computer and use it in GitHub Desktop.
Polylang Canonical Tags with Yoast's API
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 | |
/** | |
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