• easinewe

    (@easinewe)


    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>

Viewing 2 replies - 1 through 2 (of 2 total)
  • Moderator bcworkz

    (@bcworkz)

    The field names need to end with empty square brackets []. It’s also invalid HTML to have the same ID attributes on a page. Unless [myCheckBox] has some special meaning I’m unaware of, I would suggest something like this:
    $myCheckBoxHtml .= "<input type='checkbox' name='attachments[{$post->ID}][]' id='attachments{$relative['id']}[{$post->ID}]'";

    The output would start with something like this:
    <input type="checkbox" name="attachments[75][]" id="attachments92[75]" value="92">Stav Levinson<br>

    Thread Starter easinewe

    (@easinewe)

    @bcworkz I’ve set up the checkboxes as you suggested but I’m still having no luck saving the data as an array. I can save one checkbox field but not multiple checkbox values.

    What you are suggesting makes sense but I’m getting the feeling that it is not possible to save array of data through the attachments…

    https://core.trac.wordpress.org/ticket/28053

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Save Multiple Checkbox Values in Attachment Details’ is closed to new replies.