我为图片附件创建了以下元数据选项。基本上,用户选择一个单选按钮 - 然后将相应的类添加到div,该div围绕附件。由于某种原因,所选的单选按钮值未被保存。但是,我注意到如果我将输入切换为常规文本字段,一切都很好。
function add_attachment_classes_field( $form_fields, $post ) {
$field_value = get_post_meta( $post->ID, 'classes', true );
$form_fields['classes'] = array(
'value' => $field_value ? $field_value : '',
'label' => __( 'Classes' ),
'input' => 'html',
'html' => "<div><input checked='checked' style='width: initial' type='radio' name='border-style' value=''> None</div>
<div><input style='width: initial' type='radio' name='border-style' value='green-border'> Green</div>
<div><input style='width: initial' type='radio' name='border-style' value='red-border'> Red</div>
<div><input style='width: initial' type='radio' name='border-style' value='jagged-border'> Jagged</div>
<div><input style='width: initial' type='radio' name='border-style' value='purple-border'> Purple</div>",
);
return $form_fields;
}
function save_attachment_classes( $attachment_id ) {
if ( isset( $_REQUEST['attachments'][$attachment_id]['classes'] ) ) {
$classes = $_REQUEST['attachments'][$attachment_id]['classes'];
update_post_meta( $attachment_id, 'classes', $classes );
}
}
function wrap_my_div($html, $id, $caption, $title, $align, $url, $size, $alt) {
$field_value = get_post_meta( $id, 'classes', true );
return '<div class="'.$field_value.'">'.$html.'</div>';
}
add_action( 'edit_attachment', 'save_attachment_classes' );
add_filter( 'attachment_fields_to_edit', 'add_attachment_classes_field', 10, 2 );
add_filter( 'image_send_to_editor', 'wrap_my_div', 10, 8 );
答案 0 :(得分:1)
问题似乎是单选按钮的name属性。
我将每个更改为以下内容并解决了问题:
name='attachments[{$post->ID}][classes]'