Search code examples
wordpresstranslationwpml

WPML language switcher. Link to custom page when no translation


I have a question. I am using WPML plugin. In WPML->languages->Language switcher options i checked "Link to home of language for missing translations". Now when i click language switcher country flags and translation is missing it will redirect to homepage. My question is how to redirect to custom page when translation is not found. I want to create page with text "Sorry translation is missing. Please contact us for more info..."

Thank you for your time


Solution

  • First make sure you have WPML->languages->Language switcher options "Link to home of language for missing translations" checked. Then create your custom translation not found page in main language and translate it to other languages using WPML. Then add this code to your functions.php

    add_filter('wpml_ls_language_url', 'redirect_link',10,2);
        function redirect_link($url,$lang){
            if($lang['missing'] == 1) {
                $permalink_to_translation_not_found_page_in_main_language = get_the_permalink(40);  //40 is page id of a custom translation not found page in main language
                $lang['url'] = apply_filters( 'wpml_permalink', $permalink_to_translation_not_found_page_in_main_language , $lang['language_code'] );
            }
            return $lang['url'];
        }
    

    I hope this is useful for someone