Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

The code below adds a custom input field to the attachments editor. How can I convert the text input to a checkbox and get/set the value of the checkbox on load and save?

Note: "input" => "checkbox" does not work :(

function image_attachment_fields_to_edit($form_fields, $post) {  
        $form_fields["imageLinksTo"] = array(  
            "label" => __("Image Links To"),  
            "input" => "text",
            "value" => get_post_meta($post->ID, "_imageLinksTo", true)  
        );    
        return $form_fields;  
    }  

function image_attachment_fields_to_save($post, $attachment) {  
        if( isset($attachment['imageLinksTo']) ){  
            update_post_meta($post['ID'], '_imageLinksTo', $attachment['imageLinksTo']);  
        }  
        return $post;  
    } 

add_filter("attachment_fields_to_edit", "image_attachment_fields_to_edit", null, 2); 
add_filter("attachment_fields_to_save", "image_attachment_fields_to_save", null, 2); 
share|improve this question
add comment

2 Answers

up vote 6 down vote accepted

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.

share|improve this answer
    
Can you please add a working example of the code that saves this information? Would be really great! :) –  Ole Henrik Skogstrøm Mar 23 '12 at 20:27
add comment

Below is a complete block for adding an IsLogo checkbox , including the saving :

function attachment_fields_to_edit_islogoimage( $form_fields, $post ) {
    $islogo = (bool) get_post_meta($post->ID, '_islogo', true);
    $checked = ($islogo) ? 'checked' : '';

    $form_fields['islogo'] = array(
        'label' => 'Logo Image ?',
        'input' => 'html',
        'html' => "<input type='checkbox' {$checked} name='attachments[{$post->ID}][islogo]' id='attachments[{$post->ID}][islogo]' />",
        'value' => $islogo,
        'helps' => 'Should this image appear as a proposal Logo ?'
    );
    return $form_fields;

}
add_filter( 'attachment_fields_to_edit', 'attachment_fields_to_edit_islogoimage', null, 2 );

function attachment_fields_to_save_islogoimage($post, $attachment) {
    $islogo = ($attachment['islogo'] == 'on') ? '1' : '0';
    update_post_meta($post['ID'], '_islogo', $islogo);  
    return $post;  
}
add_filter( 'attachment_fields_to_save', 'attachment_fields_to_save_islogoimage', null, 2 );

my 2 cents.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.