Last updated on October 18, 2023

WPML troubleshooting multilingual media

Don’t know where to add this snippet? Read our guide: How to add code snippets.

Resolve issues with multilingual media in WPML.

The code snippet below can be useful for troubleshooting issues related to multilingual media when using the WPML plugin in WordPress. It helps in identifying and resolving problems that may arise when translating media files in different languages.

function wpsnippets_wpml_media_troubleshooting() {
    global $wpdb;

    // Get all media attachments
    $attachments = $wpdb->get_results(
        "SELECT ID, post_title, post_type
        FROM $wpdb->posts
        WHERE post_type IN ('attachment', 'revision')"
    );

    // Loop through each attachment
    foreach ($attachments as $attachment) {
        $attachment_id = $attachment->ID;
        $attachment_title = $attachment->post_title;
        $attachment_type = $attachment->post_type;

        // Check if attachment is translated
        if (function_exists('wpml_object_id')) {
            $translated_id = wpml_object_id($attachment_id, $attachment_type, true);
            if ($translated_id != $attachment_id) {
                echo "Translated attachment found: ID - $attachment_id, Title - $attachment_title, Translated ID - $translated_id<br>";
            }
        }
    }
}

To use this code snippet, you can add it to your theme’s functions.php file or create a custom plugin. Once added, you can call the wpsnippets_wpml_media_troubleshooting() function to execute the troubleshooting process.

The code retrieves all media attachments using a database query and then checks if each attachment has a translated version using the wpml_object_id() function provided by WPML. If a translated version is found, it will display the attachment’s ID, title, and the translated ID.

This code snippet can be helpful when you encounter issues with translated media attachments not being displayed correctly or when you need to verify if all media files have been properly translated using WPML.

Examples

Example 1: Troubleshooting missing translations for media files

This use case demonstrates how to troubleshoot missing translations for media files when using the WPML plugin. The code example below checks if a translated version of a media file exists and falls back to the default language version if it doesn’t.

function wpsnippets_get_translated_media_url( $attachment_id ) {
    if ( function_exists( 'wpml_get_language_information' ) ) {
        $default_language = wpml_get_default_language();
        $current_language = wpml_get_language_information( null, 'locale' );

        if ( $current_language !== $default_language ) {
            $translated_attachment_id = apply_filters( 'wpml_object_id', $attachment_id, 'attachment', true, $default_language );
            if ( $translated_attachment_id ) {
                $translated_attachment_url = wp_get_attachment_url( $translated_attachment_id );
                if ( $translated_attachment_url ) {
                    return $translated_attachment_url;
                }
            }
        }
    }

    return wp_get_attachment_url( $attachment_id );
}

The code first checks if the WPML plugin is active and retrieves the default language and current language. If the current language is not the default language, it uses the wpml_object_id filter to get the translated attachment ID. If a translated attachment ID is found, it retrieves the URL of the translated attachment. If no translated attachment URL is found, it falls back to the default language attachment URL.

Example 2: Troubleshooting incorrect media URLs in different languages

This use case demonstrates how to troubleshoot incorrect media URLs in different languages when using the WPML plugin. The code example below ensures that the correct media URL is used based on the current language.

function wpsnippets_get_translated_media_url( $attachment_id ) {
    if ( function_exists( 'wpml_get_language_information' ) ) {
        $current_language = wpml_get_language_information( null, 'locale' );

        if ( $current_language === 'fr_FR' ) {
            $translated_attachment_id = apply_filters( 'wpml_object_id', $attachment_id, 'attachment', true, 'fr' );
            if ( $translated_attachment_id ) {
                $translated_attachment_url = wp_get_attachment_url( $translated_attachment_id );
                if ( $translated_attachment_url ) {
                    return $translated_attachment_url;
                }
            }
        }
    }

    return wp_get_attachment_url( $attachment_id );
}

The code checks if the WPML plugin is active and retrieves the current language. If the current language is set to French (fr_FR), it uses the wpml_object_id filter to get the translated attachment ID for the French language. If a translated attachment ID is found, it retrieves the URL of the translated attachment. If no translated attachment URL is found or the current language is not French, it falls back to the default language attachment URL.

Example 3: Troubleshooting missing media translations for specific post types

This use case demonstrates how to troubleshoot missing media translations for specific post types when using the WPML plugin. The code example below checks if a translated version of a media file exists for specific post types and falls back to the default language version if it doesn’t.

function wpsnippets_get_translated_media_url( $attachment_id ) {
    if ( function_exists( 'wpml_get_language_information' ) ) {
        $default_language = wpml_get_default_language();
        $current_language = wpml_get_language_information( null, 'locale' );
        $post_type = get_post_type();

        if ( $current_language !== $default_language && in_array( $post_type, array( 'post', 'page' ) ) ) {
            $translated_attachment_id = apply_filters( 'wpml_object_id', $attachment_id, 'attachment', true, $default_language );
            if ( $translated_attachment_id ) {
                $translated_attachment_url = wp_get_attachment_url( $translated_attachment_id );
                if ( $translated_attachment_url ) {
                    return $translated_attachment_url;
                }
            }
        }
    }

    return wp_get_attachment_url( $attachment_id );
}

The code first checks if the WPML plugin is active and retrieves the default language, current language, and the post type. If the current language is not the default language and the post type is either “post” or “page”, it uses the wpml_object_id filter to get the translated attachment ID. If a translated attachment ID is found, it retrieves the URL of the translated attachment. If no translated attachment URL is found or the current language is the default language or the post type is not “post” or “page”, it falls back to the default language attachment URL.

Last updated on October 18, 2023. Originally posted on January 5, 2024.

Leave a Reply

Your email address will not be published. Required fields are marked *