Hard coded theme links

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.

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.

34 Responses to “Hard coded theme links”

  1. Pawel says:

    Please update that type argument defaults to ‘post’, not ‘page’. Also for page lookups type should be set to ‘post’ not ‘page’ to give proper results.

    • Amir says:

      Actually, in WordPress ‘pages’ and ‘posts’ share the same database table, so when you lookup a ‘post’ or ‘page’ using this function you get exactly the same thing.

      We kept both parameters so that the documentation makes sense, but they give the same result.

      • Pawel says:

        In my environment (WP 2.7.1, WPML 1.0.3) I’m receiving different results for ‘post’ and ‘page’. For ‘post’ as type argument I’m getting desired results, for same function call (only ID and type arguments) with ‘page’ as a type I’m receiving link to page with different ID.

        in inc/template-functions.php
        $trid = $sitepress->get_element_trid($element_id, $element_type)
        returns proper ID for call with ‘post’ as element_type and nothing for call with ‘page’ as element_type

        Apologies for starting discussion here, I know I should move to forum.

        • Micheal says:

          Keep in mind that the ‘page’ and ‘post’ differs in WordPress. If you create a Custom Post type, and add hierarchical=>true to the values, you now have to refer to it as a ‘page’ under certain circumstances or else it’s completely ignored. This may not be the case with all feature checks, so it all depends on your scenario how you have to call them. It’s certainly not universal.

          Also like Amir has pointed out, posts and pages reside in the same location and are essentially the same. It all depends on how you have things set up.

  2. othersphere says:

    howdy,
    is there a way to modify the link? i need to add a class to it (a different class to each link), eg.

    Something

    many thanks for an answer
    osp

    • othersphere says:

      oh, sorry, please correct: a href=”something/” class=”class-of-something”

    • admin says:

      We didn’t add optional styling to this call. I think that you can just wrap that link in a SPAN with a class. Then, in your CSS just style the links in the span class and you’ll get the results you need.

  3. Marco says:

    Hello
    I need to help for CSS.
    Why doesn’t implement CSS for functions ICL_LINK_TO_ELEMENT e ICL_GET_HOME_LINK? I want add css for these two functions. I’m resolve partially with div id=”menu-wrap” and using tag and , but doesn’t work the dropdown menu with subcategory. How can i do?

    Thank you for answer

    Bye

  4. Maggy says:

    Hi Amir,

    I really like your plugin, works perfect. Thank you for making it available for the community!

    I have a question regarding the above hard coded links. Is it possible to link this function to an image instead of text? I can’t get it to work…

    Thanks in advance.

    Maggy

  5. Adrian says:

    Hi I’m trying to use this method:
    icl_link_to_element(16, ‘post’, __(‘Home’))
    The translation itself works fine, however it returns everything in lower case for some reason. Any ideas?

  6. ieviesis says:

    It would be great if this function contained one more argument that could hold valueso of link attributes, such as title,class etc..

  7. K says:

    Is there a function to get only the URI? I have a 3 huge boxes as links on the front pages and everything is contained in the “a” tags.

    I know I can use the text variable inside this function, but it looks messy, I want less functionality for more flexibility :)

  8. Michael Joanisse says:

    Hi there, i’m using the following parameters:

    How can i provide the translated, alternative text for the post/page it’s referencing to? Right now, this points to a page title ‘Welcome Parents to . . .’ and it’s anchor as intended, is replaced by words specified in parameter.

    But, how can i achieve the same thing, translating it to second language?
    I would need something similar..

    Thank you in advance for any help.

  9. Are there pre-defined arguments for the above function icl_link_to_element, and if so, is there a list of them somewhere? Or do they have to be defined individually per use?

    Thanks

  10. David says:

    Is this function no longer necessasary? The latest version of the plugin includes an option that seems new to me – on the Languages options admin screen:

    Make themes work multilingual

    Adjust IDs for multilingual functionality

    This feature turns themes into multilingual, without having to edit their PHP files.

    Is this option replacing the icl_link_to_element() function? Or are these two entirely different things?

  11. Martin says:

    Hi,
    I just needed to wrap an IMG (same image for both lang) within the HREF of the appropriated language.
    I don’t pretend that this is a good solution but it did the job for me:

    <a href="”><img src="/img/picture.jpg” alt=”">

    Maybe it can help someone

  12. lelouch says:

    It would be really useful to have the ability to add attributes to the produced A tag. Wrapping around another element works, but one cannot be forced to do so.

    As an alternative, a function should exist that returns only the generated HREF to be used wherever one wants, I did one myself to accomplish that:

    function iclx_href_to_element( $ID, $type, $arguments )
    {
    ob_start( );
    icl_link_to_element( $ID, $type, ”, $arguments );
    $html = ob_get_clean( );
    if( preg_match( ‘~href=”([^"]*)”~is’, $html, $regs ) ) {
    return $regs[1];
    } else {
    return ”;
    }
    }

    • Matthew says:

      I had the same need, but I tried this function and it didn’t work for me.

      Instead, I copied the incl_link_to_element function and modified it with a new name and made it return its internal $url variable instead of the entire link element that it constructs. This isn’t very graceful, but it’s good enough for my project needs.

      I created a single-file plugin to house this modified function for portability — but it could also go in the theme’s functions file.

      It’d be great to see the API extended to handle this in a more graceful and baked-in way.

      • amir says:

        This is a valid solution. What I would recommend is to use icl_object_id and then use the WP API functions that general the URL for that object. It’s basically the same as you’re doing, just without modifying any functions.

  13. Nestor says:

    How to link to a custom taxonomy term??

  14. Paulo says:

    I’m sure this has been asked before but I can’t find it.
    How can I hide links to untranslated pages? (I have a hard coded menu)

    Thanks,
    Paulo

Leave a Reply

Please leave here comments about this page only.
For technical support and feature suggestions, head to our forum. We are waiting there!

You can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>