我正在尝试部署几个自定义可重复字段,电话和手机。每个字段都有多个输入(国家代码,区号(用于电话),号码和分机(用于电话))。这些字段将与其他字段一起作为“联系人”集的一部分,并属于组字段。因此,将可重复的自定义字段转换为组字段。
在一个现有的类上,我曾经从the example of an US address field type provided by CMB2 devs返回某些CPT的值(说实话,我无法上班-它未能说找不到{{1 }}类)我包括以下内容来渲染,清理和转义此类字段:
CMB2_Type_Base
对于初始化,我执行了以下操作:
class cmb2Opciones
{
public function __construct()
{
add_action( 'cmb2_sanitize_fijos', array( $this, 'cmb2_sanitize_telefonos_callback' ), 10, 5 );
add_action( 'cmb2_sanitize_celulares', array( $this, 'cmb2_sanitize_telefonos_callback' ), 10, 5 );
add_action( 'cmb2_types_esc_fijos', array( $this, 'cmb2_escape_telefonos_callback' ), 10, 4 );
add_action( 'cmb2_types_esc_celulares', array( $this, 'cmb2_escape_telefonos_callback' ), 10, 4 );
add_action( 'cmb2_render_fijos', array( $this, 'cmb2_render_fijos_callback' ), 10, 5 );
add_action( 'cmb2_render_celulares', array( $this, 'cmb2_render_celulares_callback' ), 10, 5 );
}
public function cmb2_escape_telefonos_callback( $check, $meta_value, $field_args, $field_object )
{
if ( ! is_array( $meta_value ) || ! $field_args['repeatable'] )
{
return $check;
}
foreach ( $meta_value as $key => $val )
{
if ( empty ( implode ( '', $val )))
{
unset ( $meta_value[$key] );
}
else
{
$meta_value[ $key ] = esc_attr ( $val );
}
}
return array_filter( $meta_value );
}
public function cmb2_sanitize_telefonos_callback( $check, $meta_value, $object_id, $field_args, $sanitize_object )
{
if ( ! is_array( $meta_value ) || ! $field_args['repeatable'] )
{
return $check;
}
foreach ( $meta_value as $key => $val )
{
$meta_value[ $key ] = array_filter( array_map( 'sanitize_text_field', $val ) );
}
return array_filter( $meta_value );
}
public function cmb2_render_fijos_callback( $field, $escaped_value, $object_id, $object_type, $field_type )
{
$escaped_value = wp_parse_args( $escaped_value, array(
'fijo_pais' => '',
'fijo_area' => '',
'fijo_numero' => '',
'fijo_extension' => '',
));
$output = '<div style="overflow:hidden">';
$output .= '<div class="alignleft" style="width:4em">';
$output .= $field_type->input( array(
'name' => $field_type->_name( '[fijo_pais]' ),
'id' => $field_type->_id( '_fijo_pais' ),
'value' => $escaped_value['fijo_pais'],
'type' => 'number',
));
$output .= '</div>';
$output .= '<div class="alignleft" style="width:3em">';
$output .= $field_type->input( array(
'name' => $field_type->_name( '[fijo_area]' ),
'id' => $field_type->_id( '_fijo_area' ),
'value' => $escaped_value['fijo_area'],
'type' => 'number',
));
$output .= '</div>';
$output .= '<div class="alignleft" style="width:calc(100% - 25em)">';
$output .= $field_type->input( array(
'name' => $field_type->_name( '[fijo_numero]' ),
'id' => $field_type->_id( '_fijo_numero' ),
'value' => $escaped_value['fijo_numero'],
'type' => 'number',
));
$output .= '</div>';
$output .= '<div class="alignleft" style="width:15em">';
$output .= $field_type->input( array(
'name' => $field_type->_name( '[fijo_extension]' ),
'id' => $field_type->_id( '_fijo_extension' ),
'value' => $escaped_value['fijo_extension'],
'type' => 'text',
));
$output .= '</div></div>';
echo $output;
echo $field_type->_desc( true );
}
public function cmb2_render_celulares_callback( $field, $escaped_value, $object_id, $object_type, $field_type )
{
$escaped_value = wp_parse_args( $escaped_value, array(
'celular_pais' => '',
'celular_numero' => '',
));
$output = '<div style="overflow:hidden">';
$output .= '<div class="alignleft" style="width:4em">';
$output .= $field_type->input( array(
'name' => $field_type->_name( '[celular_pais]' ),
'id' => $field_type->_id( '_celular_pais' ),
'value' => $escaped_value['celular_pais'],
'type' => 'number',
));
$output .= '</div>';
$output .= '<div class="alignleft" style="width:calc(100% - 5em)">';
$output .= $field_type->input( array(
'name' => $field_type->_name( '[celular_numero]' ),
'id' => $field_type->_id( '_celular_numero' ),
'value' => $escaped_value['celular_numero'],
'type' => 'number',
));
$output .= '</div></div>';
echo $output;
echo $field_type->_desc( true );
}
// Other non-related CMB2 functions...
}
问题在于,对于每个可重复字段,它显示的是自定义字段的6倍,如果我填写一行,只是因为网络神奇而美妙,它就不会保存:
我不确定是否做错了什么,或者这是否是某种错误,因为我记得CMB2中进行了一些修复,使其更易于重复使用。但是,如果有人可以指出发生的事情,我会感到很高兴。