Save Multiple Checkbox Values in Attachment Details
-
I am attempting to create a method of tagging people in the Attachment Details section of my WordPress site. Basically it creates a checkbox for each family member I have entered information for. If the person is in the photo you check the box next to their name.
I have been able to form with the checkboxes to appear next to the photos but I can’t seem to save more than one checkbox value. Every time I check a box it overwrites the old value rather than saving it as an array.
I have been trying to solve this for a while with no luck. Could anybody point out what I’m doing wrong.
This is the code from my functions file…
function attachment_checkbox_edit($form_fields, $post) { //get a list of all the relatives you have already entered information for $relatives = get_relatives_by_id(); // get the current value of our custom field $current_value = get_post_meta($post->ID, "_myCheckBox", true); $current_value_count = count($current_value); $myCheckBoxHtml = "<input type='hidden' name='attachments[{$post->ID}][myCheckBox]' value='null' />"; foreach($relatives as $relative){ // if this value is the current_value we'll mark it selected $checked = ($current_value == $relative['id']) ? ' checked ' : ''; //$checked = (in_array($relative['id'], $current_value))? ' checked ' : ''; //$current_val_array = array_values ( $current_value ); $myCheckBoxHtml .= "<input type='checkbox' name='attachments[{$post->ID}][myCheckBox]' id='attachments[{$post->ID}][myCheckBox]'"; $myCheckBoxHtml .= "value='{$relative['id']}' {$checked} />"; $myCheckBoxHtml .= "{$relative['name']} {$relative['name_last']}"; $myCheckBoxHtml .= "</input><br/>"; } $myCheckBoxHtml .= "<h1>".$attached_customs_array."</h1>"; $form_fields["myCheckBox"]["label"] = __("Who is in this picture"); $form_fields["myCheckBox"]["input"] = "html"; $form_fields["myCheckBox"]["html"] = $myCheckBoxHtml; return $form_fields; } add_filter("attachment_fields_to_edit", "attachment_checkbox_edit", null, 2); function attachment_checkbox_save($post, $attachment) { if( isset($attachment['myCheckBox']) ){ update_post_meta($post['ID'], '_myCheckBox', $attachment['myCheckBox']); } return $post; } add_filter("attachment_fields_to_save", "attachment_checkbox_save", null, 2);The forms look like this when output…
<td class="field"> <input type="hidden" name="attachments[75][myCheckBox]" value="null"> <input type="checkbox" name="attachments[75][myCheckBox]" id="attachments[75][myCheckBox]" value="92">Stav Levinson<br> <input type="checkbox" name="attachments[75][myCheckBox]" id="attachments[75][myCheckBox]" value="111">Randy Levinson<br> <input type="checkbox" name="attachments[75][myCheckBox]" id="attachments[75][myCheckBox]" value="99">Harold Levinson<br> <input type="checkbox" name="attachments[75][myCheckBox]" id="attachments[75][myCheckBox]" value="80">Amanda Levinson<br> </td>
The topic ‘Save Multiple Checkbox Values in Attachment Details’ is closed to new replies.