Get permalink of translated page
-
Hello
Currently when inspecting a page in the native language I get a set ID and locale, however when inspecting the translation the ID is the same but locale different. How can I get each of these pages URLs *somewhere else* with using eg. get_permalink or filters. Currently if I use get_permalink( SET_ID ), I’ll receive only one of the URLs when there are actually 2 pages using the same page ID. Is there any way of getting all the language versions of a certain page?
-
The post translations are not in fact separate posts and they are stored in a different MySQL table. TranslatePress does not create new posts for the translated versions of the original post. But the translated versions will have separate URLs. That means every page has a separate URL for each language no matter if that page is translated or not.
Basically, the post ID is the same for both original content & its translations.
Hello Alex
I noticed the same thing that you are re-using the same post-id for both language version. I’m currently developing a plugin and would like to add support for Translatepress but I’m unable to fully do that due to not having the right tools.
I’m looking for a way to get the permalink of a specific page in specific language, do you offer any hook or function that could be utilized for this?
The plugin that I’m developing creates dynamic pages with add_rewrite_rule and populates those with content. You pretty much choose a parent page, and if the parent page URL had some set sub-pages in the URL, the rewrite rule kicks in and returns a page according to the specs in the URL. E.g. “exampleurl.com/parent/post1234” where post1234 comes from a external source and is being served if we get a match at “exampleurl.com/parent/%s”.
If you could provide some hook so that I could get the permalink of a specific language, I would be able to make the plugin I’m working on to support Translatepress fully.
One example how it could look like:
$value = apply_filters( ‘trp_permalink’, ‘domain.com/parent’, ‘de’ );and $value would become “domain.com/de/elternteil”
Construct the links based on home_url() The language parameter will get added automatically. There is an internal function get_url_for_language() that automatically adds the language parameter. Regardless of what you use, you’ll need to add data-no-translation on the links or we’ll try to process them and might remove the language param in certain cases.
We will try to document this on our website: https://translatepress.com/docs/translatepress/
One isse that comes directly up when using home_url() is that I need to verify the data of various languages under one locale, hence needing somehow in a iteration to check what the permalink for a specific page is.
Maybe this pseudocode helps to bring up the issue:
e.g.
$permalinks = [];
$languages = get_all_used_two_letter_language_slugs();
$parent_page_id = 123;
foreach( $languages as $lang ){
$permalinks[$lang] = get_permalink($parent_page_id);
}The home_url() can maybe give the right language but I would still be missing the translated name of the page, if someone uses the Pro-version of your plugin. Also building the language manually in would not be feasible as the language could be /en/ or /english/ or something else.
Any updates on this?
Hello,
You shouldn’t really have to create that list of translated pages. If possible always create url’s based on the default language using site_url() instead of home_url(), or using WordPress functions like get_permalink(). Then list those url’s and TranslatePress will try to convert them when needed to their similar language.
If you do need it, then use the rest api.
The rest-api is processed through the translation engine in TranslatePress. So you can access it at /de/wp-json/wp/v2/posts for example. Basically every REST endpoint is processed when accessed via the language prefix.
So you’ll now have a list of posts in that language, with the correct permalink.
You can also access de/wp-json/wp/v2/posts/1 where 1 is the post ID. Again, you should receive a translated version.
As of right now, we don’t have a simple function where you receive the slug of a particular.
There are limitations of what we can do due to how we’re translating WordPress and there are projects where you should probably use a different translation plugin like WPML or Polylang. And there’s nothing wrong with that.
In my use case I would need the translation slugs during PHP-processing. I feel that it would be really wrong to have to do a wp_remote_get towards the internal API just to get a version of a URL in a different language. I would appreciate if you could expose some functions from the plugin that custom/integration solutions would be more manageable 🙂
I think I could do a workaround with that rest-api solution but right now it feels much more tedious than it should be.
Hi,
We’ve added this docs page recently: https://translatepress.com/docs/developers/translating-an-internal-url/
But it will retrieve the permalinks, not the slugs. I should have suggested this sooner.
Hope it helps.
Hello
This is enough for me to make a workaround with some home_url() and string replacements 🙂 Thank you for the updated instructions. I noticed that there is a small issue in the instructions with the parameter name.
“$language” should be “$target_language”
Also, you mention language code yet you show a locale code e.g. “en” vs “en_GB”. I tested this with language codes and got it working, maybe you could update the example to host “en” or some other language like “es” to clarify it.
- The topic ‘Get permalink of translated page’ is closed to new replies.