Search code examples
wordpresshttp-redirecttranslationwpml

Show 'This page not translated' if translation doesn't exist. WP and WPML


Well, I have a WordPress site with WPML. Not all of my pages are translated to EN version. And I know to realize following. When somebody tries to go to URL which doesn't exist I want to show him/her 'Not translated page' (with a recommendation to observe this content in another language). How can I realize it? Thanks!


Solution

  • add_action('wp_head', 'wpml_custom');
    function wpml_custom() {
        global $wp_query;
        $postId = $wp_query->post->ID;
        $postType = $wp_query->post->post_type;
        $args = [
            'element_id' => $postId, 'element_type' => $postType
        ];
        $translation = apply_filters( 'wpml_element_language_details', null, $args );
        $currentLang = apply_filters( 'wpml_current_language', null );
    
        if (preg_match('/^\/$/', $_SERVER['REQUEST_URI'])) {
            return;
        } elseif ($translation->language_code !== $currentLang) {
            require get_template_directory()  . '/no-translation.php';
            exit();
        }
    

    }