Support

Account

Home Forums Backend Issues (wp-admin) Gallery field + WPML: image title is always shown in original language Reply To: Gallery field + WPML: image title is always shown in original language

  • I think you need to tell ACF that it should load the value for the correct attachment translation.

    I just ran into the same problem and solved it with the following filter function added to functions.php. I have the the WPML Media Translation Module activated, but didn’t test it without it.

    
    /**
     * Make Media attachments translatable with WPML
     *
     * Filter ACF images and galleries to switch attachment ids with their
     * corresponding WPML translation.
     */
    add_filter( 'acf/load_value/type=gallery', 'my_acf_load_translated_attachment', 10, 3 );
    add_filter( 'acf/load_value/type=image', 'my_acf_load_translated_attachment', 10, 3 );
    
    function my_acf_load_translated_attachment($value, $post_id, $field) {
        $newValue = $value;
    
        // Make sure we are using WPML
        if ( function_exists('icl_object_id') ) {
            // Galleries come in arrays
            if ( is_array($value) ) {
                $newValue = array();
                foreach ($value as $key => $id) {
                    $newValue[$key] = icl_object_id($id, 'attachment');
                }
            }
            // Single images arrive as simple values
            else {
                $newValue = icl_object_id($value, 'attachment');
            }
        }
    
        return $newValue;
    }
    

    This isn’t tested thoroughly, but can you try if this is working for you?

    This might also fix the issue described in http://support.advancedcustomfields.com/forums/topic/wpml-and-gallery-field/.