Notice When WPML Shows Content in Another Language
Adds a short notice above posts that WPML displays in the default language because no translation exists yet ("display as translated" mode), so visitors know why the text is not in their language.
Opens WPIDE on
Code
$wpide_notice = 'This content is not available in your language yet, so we are showing the original version.';
add_action( 'admin_init', function () use ( $wpide_notice ) {
do_action( 'wpml_register_single_string', 'WPIDE Snippets', 'Fallback language notice', $wpide_notice );
} );
add_filter( 'the_content', function ( $content ) use ( $wpide_notice ) {
if ( ! defined( 'ICL_SITEPRESS_VERSION' ) || is_admin() || ! is_singular() || ! in_the_loop() || ! is_main_query() ) {
return $content;
}
$details = apply_filters( 'wpml_post_language_details', null, get_the_ID() );
$current = apply_filters( 'wpml_current_language', null );
if ( ! is_array( $details ) || empty( $details['language_code'] ) || $details['language_code'] === $current ) {
return $content;
}
$text = apply_filters( 'wpml_translate_single_string', $wpide_notice, 'WPIDE Snippets', 'Fallback language notice' );
return '<p class="wpide-fallback-notice" role="note" style="padding:.75em 1em;border-inline-start:4px solid #dba617;background:rgba(219,166,23,.1)">' . esc_html( $text ) . '</p>'
. '<div lang="' . esc_attr( $details['language_code'] ) . '">' . $content . '</div>';
} );
Notes
Translate the notice text through String Translation under the "WPIDE Snippets" domain after visiting the dashboard once.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.