WordPress, WPML and Widget Title Link plugin

If you need to have your widget titles a links but you have multi language website with WPML plugin you can use a plugin “Widget Title Links” by Ragulka¬†and my small code snippet, please add this to your theme’s functions.php:

add_filter('widget_title_link', 'wpml_widget_title_link');

function wpml_widget_title_link($title_link)
{
    // this removes http:// because it is added by esc_url in widget-title-link.php:L88
    $title_link = preg_replace('/https?:\/\//', '', $title_link);

    // this WPML helper generates link to translated post/page/category/etc WP object
    $link = icl_link_to_element(
        $title_link,
        $element_type = 'post',
        $link_text = '',
        $optional_parameters = array(),
        $anchor = '',
        $echoit = false,
        $return_original_if_missing = true
    );

    // this cuts URL from a generated link
    if (preg_match('/href="([^"]+)"/', $link, $matches)) {
        return $matches[1];
    } else {
        return false;
    }
}

After that you can specify WP Object ID in the widget “Title link” field and WPML will find translated object and create URL for widget title:

Screenshot 2014-07-16 13.16.12