Bỏ qua điều hướng

Open

Reported for: WPML Multilingual CMS 4.6.15

Topic Tags: Compatibility

Overview of the issue

When using Elementor’s Image Widget, the translated version of a page may still reference the original language’s image, leading to incorrect ALT text being displayed in the secondary language.

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 snippet:
    // WPML: Find translatable IDs in Elementor Widgets
    add_filter( 'elementor/frontend/before_render', 'wpml_compsupp_find_translatable_post_ids_in_settings' );
    
    function wpml_compsupp_find_translatable_post_ids_in_settings( $element ) {
    
        $widget_name = $element->get_name();
        $settings    = $element->get_settings();
    
        // Recursively translate post IDs in settings array
        $translated_settings = wpml_compsupp_find_translatable_post_ids_in_array( $settings, $widget_name );
    
        $element->set_settings( $translated_settings );
    }
    
    function wpml_compsupp_find_translatable_post_ids_in_array( $settings, $widget_name, $path = [] ) {
    
        $apply_translations = true; // if set to false, translated IDs are not applied to the content
    
        foreach ( $settings as $key => &$value ) {
            // Translate IDs and handle nested settings
            $currentPath = array_merge($path, [$key]);
    
            if ( is_array( $value ) || is_object( $value ) ) {
                // If the value is an array or object, run the function recursively
                $value = wpml_compsupp_find_translatable_post_ids_in_array((array)$value, $widget_name, $currentPath);
            } elseif ( is_numeric( $value ) && $value != 0 ) {
                // Translate post ID if it is numeric and a valid post type exists
                $post_type   = get_post_type( $value );
                $post_status = get_post_status( $value );
    
                if ( $post_type && $post_status == 'publish' ) {
                    $translated_value = apply_filters( 'wpml_object_id', $value, $post_type, true );
    
                    if ($apply_translations === true ) {
                        $value = $translated_value;
                    }
                }
            }
        }
    
        return $settings;
    }
    

Để lại câu trả lời

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.

Bạn có thể sử dụng các thẻ này:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>