Getting the URL for a translated page based on the URL of a different language using WPML

When working with WPML the other day I ran into the following issue. I had to redirect the visitor to the preferred language translated page, based on an url. This also ment that at the point of redirecting there no post id yet know.

I wrote the following function to solve the problem described above.

function get_url_for_language( $original_url, $language )
	$post_id = url_to_postid( $original_url );
	$lang_post_id = icl_object_id( $post_id , 'page', true, $language );
	$url = "";
	if($lang_post_id != 0) {
		$url = get_permalink( $lang_post_id );
	}else {
		// No page found, it's most likely the homepage
		global $sitepress;
		$url = $sitepress->language_url( $language );
	return $url;

Related Posts

Powered By Related Posts for WordPress
Click Here to Learn More About Related Posts for WordPress

11 thoughts on “Getting the URL for a translated page based on the URL of a different language using WPML

  1. Thanks!, Excellent function 😀

  2. Oh man…
    You are a savior! 🙂

  3. Thanks ! You are Great bro…

  4. With the ease of watching flicks in your very
    own residence, there should be no factor to brave the components, drag the
    children out in the cool, and run the possibility of searching
    for that glossy place when traveling. This is the moment of year that everybody intends to stay at home where it’s nice as well as warm and also comfortable.
    By now the Xmas toys have been well had fun with, and your home might
    appear to be obtaining smaller due to the monotony of the family with each various other.

    The holidays more than and also the rush has actually subsided, and the options you have for home entertainment are as
    close as your computer system! You can have fun with a few
    of those remarkable video games the kids obtained for Xmas, or you can make a household film night.
    Make certain you have lots of popcorn and also fruit juice on hand to make sure that either a well-laid plan or spur of the minute choice
    will certainly stream efficiently as well as easily with stress and anxiety
    for no person!

  5. Hi there, constantly i used to check blog posts here in the early hours
    in the dawn, because i enjoy to find out more and more.

  6. May I simply say what a relief to discover somebody who really knows what they’re talking about online.
    You definitely realize how to bring a problem to light and make it
    important. A lot more people ought to check this out and understand this side of
    your story. I was surprised you aren’t more popular because you certainly possess the

  7. It’s a pity you don’t have a donate button! I’d certainly donate to this outstanding blog!

    I suppose for now i’ll settle for bookmarking and adding your RSS
    feed to my Google account. I look forward to new updates and will
    talk about this blog with my Facebook group.
    Talk soon!

  8. I read this post completely about the difference of latest and preceding technologies, it’s awesome article.

Leave a Reply

Your email address will not be published. Required fields are marked *