Many WordPress themes include hard coded links. For example, footer links that point to support and contact pages may look like this:

<a href="/support/">Support</a> | <a href="/contact-us/">Contact us</a>

When the site is running multilngual these links must adapt for the display language. Both the link text (what the visitor sees) and the URL (where the link is pointing to) need to change when switching languages.

WPML icl_link_to_element() function will produce localized links to pages and should be used for this purpose.

Note: For WPML versions >=3.2 please use the wpml_element_link filter hook. The function shown below can still be used but it has been deprecated and will be removed completely in 3.3

Function description

icl_link_to_element(ID, type, text, arguments, anchor);

Argument Description Required / optional
ID The ID of the post, page, tag or category to link to. Required
type The type of page to link to. Can be ‘post’, ‘page’, ‘tag’ or ‘category’. Optional, default is ‘post”
text The link text. If not specified will produce the name of the element in the current language. Optional, defaults to the element’s name. To leave at default, enter false.
arguments Optional arguments for the link. When used, this should be a PHP array. Optional, defaults to no arguments
anchor Optional anchor for the link. Optional, defaults to no anchor

Example usage

Example Purpose Produced HTML
<?php icl_link_to_element(10); ?> Link to page 10 (support page) <a href=”/support/”>Support</a>
<?php icl_link_to_element(10,’post’,__(‘Get help’)); ?> Link to support page with an alternative link text <a href=”/support/”>Get help</a>
<?php icl_link_to_element(10,’post’,false, array(‘category’=>’products’,’priority’=>’high’); ?> Link to support page and add two arguments <a href=”/support/?category=products&amp;priority=high”>Support</a>
<?php icl_link_to_element(10,’post’,false,’priority’=>’high’,’faq’); ?>
<?php icl_link_to_element(3,’tag’); ?> Link to tag with ID 3 (our ‘News’ tag) <a href=”/tag/news/”>News</a>

How it works

You can specify the ID of a page / post / tag / category in any language. WPML will check if translation exists for the current language. If so, it will produce the link to it. If translation doesn’t exist, it will link to the page in the original language.

When you specify the link text manually, it’s a good idea to wrap it in the gettext call __(), so that the text displays in the correct language. If you don’t specify any link text (as in the first example), WPML will produce the name of that page.