Created
August 10, 2015 21:33
-
-
Save LinzardMac/dee0488120d436e5b43b to your computer and use it in GitHub Desktop.
Replicate multiple-input-value bug in media modal
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
*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