Transposh translation filter and Yoast sitemaps compatibility

Transposh Translation Filter is a seldom updated, but very useful plugin for running multilanguage sites on WordPress. It is free, it is very intuitive to use and so far, does not appear to slow the site down too much. You can find the plugin here. Due to some differences with WordPress, the version on WordPress is outdated and no longer updated.

While the plugin creates translated page versions, the sitemaps created by the very popular Yoast SEO plugin (earlier known as WordPress SEO) don’t include those pages. The plugin documentation provides a patch that needs to be applied to the Yoast SEO plugin in the class-sitemaps.php file as follows.

/**
* This function integrates with yoast sitemap generator, and adds for each viewable language, the rest of the languages url
* Also - priority is reduced by 0.2
* And this requires the following patch in class-sitemaps.php
* For future yoast versions, and reference, look for this function:
if ( ! in_array( $url['loc'], $stackedurls ) ) {
// Use this filter to adjust the entry before it gets added to the sitemap
$url = apply_filters( 'wpseo_sitemap_entry', $url, 'post', $p );
if ( is_array( $url ) && $url !== array() ) {
$output .= $this->sitemap_url( $url );
$stackedurls[] = $url['loc'];
}
}

And change to:

if ( ! in_array( $url['loc'], $stackedurls ) ) {
// Use this filter to adjust the entry before it gets added to the sitemap
$url = apply_filters( 'wpseo_sitemap_entry', $url, 'post', $p );
if ( is_array( $url ) && $url !== array() ) {
$output .= $this->sitemap_url( $url );
$stackedurls[] = $url['loc'];
}
$langurls = apply_filters( 'wpseo_sitemap_language',$url);
if ( is_array( $langurls )) {
foreach ($langurls as $langurl) {
$output .= $this->sitemap_url( $langurl );
}
}
}
-------------------------------------------------------------------------
* @param yoast_url array $yoast_url Object containing the page information

However, on examining the code of the current version of the Yoast SEO plugin, I found that this function no longer exists at that location. With some experimentation, I found that the file inc/sitemaps/class-post-type-sitemap-provider.php needs to be edited to include the Transposh function. Find this part:

/**
* Filter URL entry before it gets added to the sitemap.
*
* @param array  $url  Array of URL parts.
* @param string $type URL type.
* @param object $post Data object for the URL.
*/
$url = apply_filters( 'wpseo_sitemap_entry', $url, 'post', $post );

if ( empty( $url ) ) {
continue;
}

if ( $post->ID === $this->get_page_for_posts_id() || $post->ID === $this->get_page_on_front_id() ) {

array_unshift( $links, $url );
continue;
}
$links[] = $url;
}

unset( $post, $url );
}

return $links;
}

And change it to

				/**
* Filter URL entry before it gets added to the sitemap.
*
* @param array  $url  Array of URL parts.
* @param string $type URL type.
* @param object $post Data object for the URL.
*/
$url = apply_filters( 'wpseo_sitemap_entry', $url, 'post', $post );

if ( empty( $url ) ) {
continue;
}

if ( $post->ID === $this->get_page_for_posts_id() || $post->ID === $this->get_page_on_front_id() ) {

array_unshift( $links, $url );
continue;
}
$links[] = $url;

/** Transposh Fix */
$langurls = apply_filters( 'wpseo_sitemap_language',$url);
if ( is_array( $langurls )) {
foreach ($langurls as $langurl) {
$links[] = $langurl;
continue;
}
}
/** End Transposh fix */
}

unset( $post, $url );
}

return $links;
}

Hope you find this useful.

If you want updates.

12 thoughts on “Transposh translation filter and Yoast sitemaps compatibility”

  1. Wow, I have looked this for ages. Thank you very much for sharing.
    You may notice already that the plugin also have hreflang problems.

    1- It does not generate hreflang for self-referencing.
    2- It does not generate hreflang for default language (default-x)

    You have a solution for these to problems, I will appreciate if you can share. I am also working on it, but could not find any solution yet. If I find any I will post here.

    Thanks

    Reply
  2. the problem is that with this fix editing the core of yoast we can update any more the plugin.

    Is anyway to implement this solution vía hook in functions.php?

    Reply
  3. great article, but i guess now transpoch is more advanced, but what i believe is a good question for 2019

    have you thought about how to program so it translates the schema that are produces by yoast?

    Reply

Leave a Reply to udiCancel reply