带有 CMB2 和 WPML Media 的 WordPress:file_list,如何在翻译中获得正确的 ID?
WordPress with CMB2 and WPML Media: file_list, how to get correct id in translations?
在 WordPress 中,我使用的是 CMB2 插件 (用于创建元框和元字段的框架) 和 WPML Media (WPML add-on 避免保存每种语言的相同附件,为该唯一附件创建以下内容的翻译:标题、替代项、描述和标题);我正在创建一个像 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' )
)
) );
因此,当我使用 WPML 中的主要语言集创建 post 时,一切正常。但是当我在 file_list
字段中创建翻译并插入附件时,ids 总是引用主要语言的附件而不是它的翻译,所以在前端 - 当你以翻译的语言显示网站时 - 标题在 file_list
字段中上传的图像的 alt、描述和标题使用主要语言。我怎么能看到他们的翻译而不是主要语言?
我看到 CMB2 提供了创建自定义清理功能的可能性,在将值插入数据库之前使用该功能检查值。所以我以这种方式在元字段中设置 sanitization_cb
参数:
$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'
) );
然后我创建了清理回调函数,以检查上传附件的语言是否与 post 的语言匹配;如果不匹配,它会查找翻译 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;
}
现在效果很好,在 front-end 我用正确的语言可视化了图像属性(标题、alt 等)。
P.S。我不知道这是正确的方法还是最漂亮的形式,我对每一个反馈都持开放态度:)
在 WordPress 中,我使用的是 CMB2 插件 (用于创建元框和元字段的框架) 和 WPML Media (WPML add-on 避免保存每种语言的相同附件,为该唯一附件创建以下内容的翻译:标题、替代项、描述和标题);我正在创建一个像 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' )
)
) );
因此,当我使用 WPML 中的主要语言集创建 post 时,一切正常。但是当我在 file_list
字段中创建翻译并插入附件时,ids 总是引用主要语言的附件而不是它的翻译,所以在前端 - 当你以翻译的语言显示网站时 - 标题在 file_list
字段中上传的图像的 alt、描述和标题使用主要语言。我怎么能看到他们的翻译而不是主要语言?
我看到 CMB2 提供了创建自定义清理功能的可能性,在将值插入数据库之前使用该功能检查值。所以我以这种方式在元字段中设置 sanitization_cb
参数:
$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'
) );
然后我创建了清理回调函数,以检查上传附件的语言是否与 post 的语言匹配;如果不匹配,它会查找翻译 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;
}
现在效果很好,在 front-end 我用正确的语言可视化了图像属性(标题、alt 等)。
P.S。我不知道这是正确的方法还是最漂亮的形式,我对每一个反馈都持开放态度:)