简体   繁体   English

具有CMB2和WPML媒体的WordPress:file_list,如何在翻译中获取正确的ID?

[英]WordPress with CMB2 and WPML Media: file_list, how to get correct id in translations?

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) ; 在WordPress中,我使用的是CMB2插件(用于创建metabox和metafield的框架)和WPML媒体(WPML附加组件可避免为每种语言保存相同的附件,从而将以下内容的翻译:标题,alt,描述和标题转换为该附件) ; I'm creating a metafield like file_list that saves attachments in an array (id => url) like this: 我正在创建一个像file_list这样的元字段,将附件保存在array (id => url)如下所示:

$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. 因此,当我以WPML中的主要语言设置帖子时,一切都可以正常工作。 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. 但是,当我创建翻译并在file_list字段中插入附件时,id始终引用主要语言的附件而不是其翻译,因此在前端-当您以翻译后的语言显示网站时-标题,alt,描述在file_list字段中上传的图像的标题和标题是主要语言。 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. 我已经看到CMB2提供了创建自定义清理功能的可能性,通过该功能可以在将值插入数据库之前对其进行检查。 So I set the sanitization_cb parameter in the metafield this way: 所以我设置了sanitization_cb在metafield这样的参数:

$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' )
    ),
    'sanitization_cb' => 'benny_cmb_sanitize_file_list'
) );

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: 如果不匹配,它将查找翻译ID,如果已翻译,它将替换它:

function benny_cmb_sanitize_file_list( $value, $field_args, $field ) {

    if ( ! function_exists( 'icl_object_id' ) )
        return $value;

    if ( empty( $value ) )
        return $value;

    $main_lang = apply_filters( 'wpml_default_language', NULL );

    $post_id = $field->object_id;

    $post_lang = apply_filters( 'wpml_post_language_details', NULL, $post_id );
    $post_lang = $post_lang[ 'language_code' ];

    if ( $post_lang == $main_lang )
        return $value;

    $photo = $value;

    $trad_photo = array();

    foreach ( $photo as $id => $url ) {

        $photo_lang = apply_filters( 'wpml_post_language_details', NULL, $id );
        $photo_lang = $photo_lang[ 'language_code' ];

        $post_type = get_post_type( $id );

        if ( $photo_lang !== $post_lang ) {

            $trad_id = apply_filters( 'wpml_object_id', $id, $post_type, FALSE, $post_lang );

            if ( ! empty( $trad_id ) ) {

                $trad_photo[ $trad_id ] = $url;

            }
        }
    }

    if ( ! empty( $trad_photo ) ) {

        return $trad_photo;

    }

    return $value;

}

Now it works great and in the front-end I visualize the image attributes (title, alt, etc.) in the correct language. 现在,它可以很好地工作,并且在前端,我可以使用正确的语言可视化图像属性(标题,alt等)。

PS I don't know if it's the right methodology or the most beautiful form, I'm open to every feedback :) PS我不知道这是正确的方法还是最漂亮的形式,我愿意接受所有反馈意见:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM