Skip Navigation

Open

Topic Tags: Compatibility

Overview of the issue

In the CozyStay theme, translating alt-texts for gallery images using WPML doesn’t work as expected. Despite translating the alt-texts through the Advanced Translation Editor (ATE), the translated alt-texts do not appear on the frontend for the translated pages.

Workaround

Please, make sure of having a full site backup of your site before proceeding.

  • Open your theme’s functions.php file.
  • Add the following code:
    // WPML Workaround for compsupp-7616
    add_filter('elementor/frontend/before_render', 'wpml_compsupp7616_filter_image_gallery_widget');
    
    function wpml_compsupp7616_filter_image_gallery_widget($element) {
    
        if ('cs_gallery' !== $element->get_name()) {
            return;
        }
    
        $settings = $element->get_settings();
    
        if (isset($settings['gallery']) && is_array($settings['gallery'])) {
            foreach ($settings['gallery'] as $key => $image) {
                if (isset($image['id'])) {
                    $post_type = get_post_type($image['id']);
    
                    if (!$post_type) {
                        continue;
                    }
    
                    $settings['gallery'][$key]['id'] = apply_filters('wpml_object_id', $image['id'], $post_type, true);
                }
            }
    
            $element->set_settings('gallery', $settings['gallery']);
        }
    }
    

2 Responses to “CozyStay Theme - Translated Alt Texts for Gallery Images Not Displaying”

  1. Hey there!

    I noticed you’re facing the same issue with the translated alt-texts not showing up in the CozyStay theme. I ran into this problem too, and after a bit of digging, I found a workaround that solved it for me.

    You’ll need to add a small code snippet to your theme’s functions.php file. It basically ensures that WPML picks up the translated image IDs correctly, so the alt-texts show up properly on the frontend. Here’s what worked for me:

    add_filter(‘elementor/frontend/before_render’, ‘wpml_compsupp7616_filter_image_gallery_widget’);

    function wpml_compsupp7616_filter_image_gallery_widget($element) {

    if (‘cs_gallery’ !== $element->get_name()) {
    return;
    }

    $settings = $element->get_settings();

    if (isset($settings[‘gallery’]) && is_array($settings[‘gallery’])) {
    foreach ($settings[‘gallery’] as $key => $image) {
    if (isset($image[‘id’])) {
    $post_type = get_post_type($image[‘id’]);

    if (!$post_type) {
    continue;
    }

    $settings[‘gallery’][$key][‘id’] = apply_filters(‘wpml_object_id’, $image[‘id’], $post_type, true);
    }
    }

    $element->set_settings(‘gallery’, $settings[‘gallery’]);
    }
    }

    Just make sure to back up your site before you edit anything. Once you add the code, it should force the gallery widget to load the translated images and their alt-texts correctly.

    Hope this helps! Let me know if it works for you too! 😊

Leave a Reply

Please stay on topic and be respectful to others. If you need help with issues not related to this post, use our Support Forum to start a chat or submit a ticket.

You can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>