How to add a checkbox element to attachments editor with example

Set the ‘input’ to ‘html’ and write out the html for the input:

function filter_attachment_fields_to_edit( $form_fields, $post ) {
    $foo = (bool) get_post_meta($post->ID, 'foo', true);

    $form_fields['foo'] = array(
    'label' => 'Is Foo',
    'input' => 'html',
    'html' => '<label for="attachments-'.$post->ID.'-foo"> '.
        '<input type="checkbox" id="attachments-'.$post->ID.'-foo" name="attachments['.$post->ID.'][foo]" value="1"'.($foo ? ' checked="checked"' : '').' /> Yes</label>  ',
    'value' => $foo,
    'helps' => 'Check for yes'
    );
    return $form_fields;
}

Saving works just as you did above, but you’re checking against a checkbox value instead, so you’ll need to update to true if isset() and update to false if not.

Leave a Comment

error code: 523