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

9 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
    gift.

Leave a Reply

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