Skip to content

Instantly share code, notes, and snippets.

@LinzardMac
Created August 10, 2015 21:33
Show Gist options
  • Save LinzardMac/dee0488120d436e5b43b to your computer and use it in GitHub Desktop.
Save LinzardMac/dee0488120d436e5b43b to your computer and use it in GitHub Desktop.
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