Polylang compatibility
-
Hi, so I’ve noticed there are some issues between RankMath and Polylang.
So far, I’ve been able to determine these conflicts:
- Activating the option Remove Category Base will cause inaccessible category archives;
- The XML Sitemaps have multiple issues: (a) for pages will only show the root homepage, not language specific homepages (b) for categories will only show URLs from the first configured language (c) for custom taxonomies will only show URLs from the default language;
- The Google News sitemap uses the default site language for each post;
- Breadcrumb text strings are not translated;
Only for the breadcrumb text strings I have a working solution in place now. And I’ll try to remember and convey to other site admins to never touch the Remove Category Base option.
But is is possible somehow to hook into the sitemap generation process and append language specific homepages and taxonomy term archives ? And set the correct language in the news sitemap?
Thanks for any thoughts ??
- This topic was modified 8 months, 1 week ago by Rolf Allard van Hagen. Reason: added one more conflict: news sitemap language
-
Hello @ravanh,
Sorry about that.
- We don’t have a custom code that could be added to correct the issue with the News Sitemap language tags. Other plugins that have compatibility with our plugin have included it in their plugins or via a custom modification as you can see one example here from WPML: https://wpml.org/errata/rank-math-seo-pro-incorrect-language-codes-in-news-sitemap/
You can use that as a basis for a custom implementation that could be used for Polylang.
However, the compatibility code that we have currently for Polylang doesn’t include that. - That is not something that can be done. We cannot prevent a single option from being hidden from other user roles. The granularity of our role manager only goes as far as the group options, not single options inside that group.
Don’t hesitate to get in touch if you have any other questions.
Ok, thanks for that link! It suggests over there to modify a Rankmath Pro file so that is not an option, but following the code, I notice that the news sitemap class uses
Locale::get_site_language();
to get a language attribute. Sadly, does not provide a filter for that, nor does theget_site_language
method… Could I make this a feature request?Looking further, I see that
get_site_language
basically takes the WP coreget_locale()
to return a two letter language code according to the sitemap specs. This means I could, in theory, use the WP corelocale
filter…Except: I need to be able to tell if I’m in a News Sitemap request or not. Otherwise, the filter would run every time
get_locale()
is run anywhere on the site by WP core, the theme or other plugins. So that’s not good…I found in the RankMath sitemap Router class that
get_query_var( 'sitemap' );
is used. Would this return the value “news” if we’re in a news sitemap request? Is so, I should be able to use that.Alas, there is no way to use the locale filter… the news sitemap does not use a query loop so I cannot detect which post (URL) is being treated. So no way to determine with which language to overwrite the locale…
So… any way RankMath would consider adding a useful language string filter somewhere?
Hello @ravanh,
The new update of the plugin which is currently in Beta will include a filter to modify that value in the News Sitemap: https://rankmath.com/changelog/beta/
With that, you will be able to filter out what URLs need to have a different language identifier in the News Sitemap.
Don’t hesitate to get in touch if you have any other questions.
Excellent news! Thank you ??
Hello @ravanh,
You’re welcome!
If you have any further questions or need assistance, feel free to reach out anytime.
Testing the new filter in the beta version, I fear I’m no succeeding to make it work.
I see the new filter in class-news-sitemap.php:
$lang = $this->do_filter( 'sitemap/news/language', $lang, $entity );
But even a crude filter like this does absolutely nothing:
add_filter( 'sitemap/news/language', function ( $lang, $entity ) { error_log( 'filtering' ); return 'xx'; }, 10, 2 );
Nothing in debug.log, no ‘xx’ news language XML node in the news sitemap. It’s as if the hook is ignored and the filter is not even run… Very strange.
Note: I have set
add_filter( 'rank_math/sitemap/enable_caching', '__return_false' );
at the same time.Hello @ravanh,
You can see that the filter runs by adding the following on your website:
add_filter( 'rank_math/sitemap/news/language', function( $lang, $entity ) { echo "<pre>"; print_r($lang); echo "</pre>"; return $lang; }, 10, 2 );
After you do that, if you access the News Sitemap you’ll see the current language being outputted for the first entry in the sitemap.
Don’t hesitate to get in touch if you have any other questions.
Ah… now I see my mistake: I forgot to prepend the hook slug with
rank_math/
. Thanks for the reply!I have some follow-up questions in pursuit of full Polylang compatibility:
- Is there a way to translate the Google News source name in the news sitemap? If not, could another filter hook be considered?
- Is there a way to make the HTML sitemap output respect the user/page language? I’m currently using the
[rank_math_html_sitemap]
shortcode to be able to have multiple pages (one per language) with an HTML sitemap but it shows posts and pages in all languages, not just the “current” language.
Thanks for any info ??
UPDATE: it appears the filter hook is useless… I’m currently working with this filter:
add_filter( 'rank_math/sitemap/news/language', function ( $lang, $entity ) { $post_id = url_to_postid( $entity['loc'] ); if ( $post_id ) { $lang = pll_get_post_language( $post_id ); } return $lang; }, 10, 2 );
And it does return the correct language but… only for the first post in the news sitemap!
I found these lines at the start of the method
get_news_publication()
in class-news-sitemap.php and commented them out:if ( ! is_null( $this->news_publication ) ) { return $this->news_publication; }
After doing that, the news post language starts reflecting individual post languages correctly…
So another feature request: could the static news publication nodes caching be dependant of the
rank_math/sitemap/enable_caching
filter as well?Hello @ravanh,
Regarding the questions:- Can you please clarify what you mean by the source name? What node in the sitemap are you trying to translate?
? - No, this is not possible. The HTML sitemap generation follows the same procedure as the other sitemap generations and it looks at the data in the database for the posts/pages and other CPT to include them in the sitemap if they are canonical and set to be indexed.
Going back to the filter, to update the language for all the entries you need to be running the logic inside a loop so that it goes through all of the entries. We just shared a very minimal example so that you can be sure the filter does run on that sitemap, but the logic to change the language based on the post will require much more careful consideration.
Last but not least, the News Sitemap responds in the same way as any other sitemap to the filter about caching.
Don’t hesitate to get in touch if you have any other questions.Hi,
With the news source name I mean the news:name node inside the news:publication node. For example:
<url> <loc>https://ontherhone.com/fr/naviguer-traversee-de-lyon-confluence-saone/</loc> <news:news> <news:publication> <news:name>Au fil du fleuve Rh?ne</news:name> <news:language>fr</news:language> </news:publication> <news:publication_date>2024-04-28T19:07:40+00:00</news:publication_date> <news:title><![CDATA[Naviguer sur le Rh?ne : traversée de Lyon et confluence de la Sa?ne]]></news:title> </news:news> </url>
About the filter: I fear there is a misunderstanding.
What I’m saying is that the filter, inside the class
get_news_publication()
method, is only run ONCE per news sitemap request. This happens because the news publication node is saved to the class$news_publication
property, on the first URL node, then on all subsequent URL nodes, the stored property value is returned, skipping the filter from then on…This means that in a news sitemap with multiple news items in different languages, ALL publication nodes will be the same as the first URL node’s.
Please take a slightly adapted version of your example code, to see it happen:
add_filter( 'rank_math/sitemap/news/language', function( $lang, $entity ) { static $count = 1; echo "<pre>"; print_r($count); echo "</pre>"; $count++; return $lang; }, 10, 2 );
You’ll notice I added a static counter, which should increment each time the filter is run, but on a live news sitemap with multiple entries, it will repeat
<pre>1</pre>
for each node.Hope that explains it more clearly ??
Hello @ravanh,
The publication name cannot be translated and it shouldn’t. That is the name of your publication and it’s sort of a brand name. For example, you won’t see Search Engine Land publication being translated into other languages.
As for the compatibility using the filter we shared, we cannot add the compatibility in our plugin but we can share the code that will be added to WPML using this filter to make the changes and you can adapt it for Polylang:add_filter('rank_math/sitemap/news/language', function ($lang, $entity) { global $sitepress; remove_filter( 'get_pages', [$sitepress, 'get_pages_adjust_ids'], 1 ); $my_post_language_details = apply_filters('wpml_post_language_details', NULL, url_to_postid($entity['loc'])); return $my_post_language_details['language_code'];}, 10, 2);
Don’t hesitate to get in touch if you have any other questions.
For example, you won’t see Search Engine Land publication being translated into other languages.
A good example. But on the other hand, it is Google themselves that advise to create distinct publication sources in Publisher Center for each language (whether on a different domain, subdomain or subdirectory) and that does allow distinct publication names… So I’m not so sure that it is strictly not allowed, or just not common (yet).
Anyway, that is of lesser importance than the
rank_math/sitemap/news/language
filter:I fear I’m still am not getting my point across…
Your example code is for WPML, and I have a similar routine that works for Polylang ready. But…
Please test your own example filter above on a site where there are multiple (recent news) posts in multiple languages. You will notice that ALL posts in the news sitemap will have the same language as the first post, irrespective of their true language.
You see these lines at the start of the method?
get_news_publication()
?in class-news-sitemap.php?if ( ! is_null( $this->news_publication ) ) { return $this->news_publication; }
This makes it so that the filter (applied after these lines) is only occurring ONCE per news sitemap request, not on each URL node. So there will be only ONE language mentioned in the news sitemap.
But, at least with Polylang when languages are set to subdirectories, there will be multiple language posts in the same news sitemap. I would suspect this is the same for WPML but I’m not
This means: the filter is useless for Polylang with languages not set to domain or subdomain ??
Unless… I could ask for this addition (or something similar) :
if ( false !== apply_filters( 'rank_math/sitemap/enable_caching' ) && ! is_null( $this->news_publication ) ) { return $this->news_publication; }
- We don’t have a custom code that could be added to correct the issue with the News Sitemap language tags. Other plugins that have compatibility with our plugin have included it in their plugins or via a custom modification as you can see one example here from WPML: https://wpml.org/errata/rank-math-seo-pro-incorrect-language-codes-in-news-sitemap/
- The topic ‘Polylang compatibility’ is closed to new replies.