Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Replicate multiple-input-value bug in media modal
/*
*In plugin file or functions.php add the following code to create new form fields in your media modal
*/
function multiple_input_field( $form_fields, $post ) {
$foo = get_post_meta( $post->ID, 'foo', true );
$form_fields['foo'] = array(
'label' => 'Is Foo',
'input' => 'html',
'html' =>
'<input type="checkbox" id="attachments-'.$post->ID.'-foo" name="attachments['.$post->ID.'][foo][]" value="1"'.($foo ? ' checked="checked"' : '').' /> One</label></br>
<input type="checkbox" id="attachments-'.$post->ID.'-too" name="attachments['.$post->ID.'][foo][]" value="2"'.($foo ? ' checked="checked"' : '').' /> Two</label></br>
<input type="checkbox" id="attachments-'.$post->ID.'-three" name="attachments['.$post->ID.'][foo][]" value="3"'.($foo ? ' checked="checked"' : '').' />Three</label> ',
'helps' => 'Choose one or many'
);
return $form_fields;
}
add_filter( 'attachment_fields_to_edit', 'multiple_input_field', 10, 2 );
/*
* save fields
*/
function admin_attachment_field_multiple_values_save() {
$post_id = $_POST['id'];
$foo = $_POST['attachments'][$post_id ]['foo'];
if( isset( $_POST['attachments'][$post_id ]['foo'] ) ){
update_post_meta($post_id , 'foo', $foo);
}
}
add_action('wp_ajax_save-attachment-compat', 'admin_attachment_field_multiple_values_save', 0, 1);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment