In WordPress I'm using the CMB2 plugins (framework for creating metabox and metafield) and WPML Media (WPML add-on avoids saving the same attachment for each language, creating translations of: title, alt, description and caption, to that only attachment); I'm creating a metafield like file_list that saves attachments in an array (id => url) like this:
$cmb = new_cmb2_box( array(
'id' => 'benny_metabox_photo',
'title' => esc_html__( 'My Photo Metabox', 'mydomain' ),
'object_types' => array( 'post' )
) );
$cmb->add_field( array(
'name' => esc_html__( 'My Photo', 'mydomain' ),
'id' => 'benny-file-list',
'type' => 'file_list',
'preview_size' => array( 100, 100 ),
'text' => array(
'add_upload_files_text' => esc_html__( 'Add Photos', 'mydomain' ),
'remove_image_text' => esc_html__( 'Remove Photo', 'mydomain' ),
'file_text' => esc_html__( 'Photo:', 'mydomain' ),
'file_download_text' => esc_html__( 'Download', 'mydomain' ),
'remove_text' => esc_html__( 'Remove', 'mydomain' )
)
) );
So, when I create a post in the main language set in WPML everything works correctly. But when I create translations and insert attachments in the file_list field, the ids always refer to attachments in the main language and not to its translations, so in the frontend - when you display the site in the languages translated - the title, alt, description and caption for the images uploaded in the file_list field are in the main language. How could I do to view their translations and not the main language?
I have seen that CMB2 offers the possibility to create a custom sanitization function with which the values are checked before they're inserted in the database. So I set the
sanitization_cbparameter in the metafield this way:Then I created the callback function for sanitization, in order to check if the language of the uploaded attachment matches the language of the post; if it doesn't match, it looks for the translation id and if it has been translated it replaces it:
Now it works great and in the front-end I visualize the image attributes (title, alt, etc.) in the correct language.
P.S. I don't know if it's the right methodology or the most beautiful form, I'm open to every feedback :)