Halloween Sale 15% off Ends in See the plans
PHP

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.

Don’t have WPIDE yet? Install it on your site

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

  1. Install WPIDE on your WordPress site (it is free) and open Code Snippets.
  2. Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
  3. It is added inactive: review the code and settings, then switch it on.

Have a snippet worth sharing?

Publish it to the library from WPIDE or right here. Sign in with any WordPress site that runs WPIDE, free or Pro. Public snippets are reviewed by the WPIDE team before they appear.

Which site should get this snippet?

Enter the address of your WordPress site. WPIDE opens there in a new tab with the snippet ready to review and save.

Remembered in this browser only, so you are asked once.

Report this snippet

Tell the WPIDE team what is wrong: broken code, something harmful, spam or copied work.