'', ), $attr); $current_language = get_locale(); if( $current_language == $attr['language'] ){ $output = translangated_kses_shortcode_output( do_shortcode( (string) $content ) ); }else{ $output = ""; } return $output; } // add conditional languages shortcode add_shortcode( 'translangated-include', 'translangated_include_content_in_language'); /* --------------------------------------------------------------------------- * Shortcode [translangated-include lang="en_us,fr_fr,ro_RO" enable_translation="yes" (default value)] [/translangated-include] * * Displays content in the chosen languages (attribute lang="") and allows to decide if the content should be translatable or not * ( enable_translation="yes"/"no" - default value is "yes" ) * --------------------------------------------------------------------------- */ function translangated_include_content_in_language( $attr, $content = null ){ $attr = shortcode_atts([ 'lang' => '', 'enable_translation' => 'yes', // default is "yes" if not set ], $attr, 'translangated-include'); $output = translangated_get_include_exclude_content( true, $attr['lang'], $attr['enable_translation'], $content ); return $output; } // add conditional language shortcode add_shortcode( 'translangated-exclude', 'translangated_exclude_content_in_language'); /* --------------------------------------------------------------------------- * Shortcode [translangated-exclude lang="en_us,fr_fr,ro_RO" enable_translation="yes" (default value)] [/translangated-exclude] * * Restricts content in the chosen languages (attribute lang="") and allows to decide if the content should be translatable or not * ( enable_translation="yes"/"no" - default value is "yes" ) * --------------------------------------------------------------------------- */ function translangated_exclude_content_in_language( $attr, $content = null ){ $attr = shortcode_atts([ 'lang' => '', 'enable_translation' => 'yes', // default is "yes" if not set ], $attr, 'translangated-exclude'); $output = translangated_get_include_exclude_content( false, $attr['lang'], $attr['enable_translation'], $content ); return $output; } /** * Escapes the content returned by the language shortcodes: post HTML plus the no-translation wrapper tag. */ function translangated_kses_shortcode_output( $html ) { $allowed = wp_kses_allowed_html( 'post' ); $allowed['trp-tag'] = array( 'data-no-translation' => true ); return wp_kses( (string) $html, $allowed ); } // function to be called inside the language_include/ exclude shortcodes // $include can be true or false function translangated_get_include_exclude_content( $include, $allowed_languages, $enable_translation_var, $content = null ) { global $TRANSLANGATED_LANGUAGE; $allowed_languages = array_map('trim', explode(',', strtolower($allowed_languages))); $enable_translation = true; if (isset($enable_translation_var)) { $value = strtolower(trim($enable_translation_var)); if ($value === 'no') { $enable_translation = false; } } if ($include === in_array(strtolower($TRANSLANGATED_LANGUAGE), $allowed_languages)) { /* "include the content" and "current language is among specified languages" * OR * "exclude the content" and "current language is NOT among specified languages" */ $content = (string) $content; $output = $enable_translation ? do_shortcode( $content ) : do_shortcode( '' . $content . '' ); $output = translangated_kses_shortcode_output( $output ); }else{ $output = ""; } return $output; } add_filter('translangated_exclude_words_from_automatic_translation', 'translangated_add_shortcode_content_to_excluded_words_from_auto_translation'); function translangated_add_shortcode_content_to_excluded_words_from_auto_translation($excluded_words){ global $TRANSLANGATED_LANGUAGE_SHORTCODE; if (!isset($TRANSLANGATED_LANGUAGE_SHORTCODE)){ $TRANSLANGATED_LANGUAGE_SHORTCODE = array(); } $excluded_words = array_merge($excluded_words, $TRANSLANGATED_LANGUAGE_SHORTCODE); return $excluded_words; }