/** * Extra Examples for NetTuts article: * Creating Custom Fields for Attachments in WordPress * http://net.tutsplus.com/tutorials/wordpress/creating-custom-fields-for-attachments-in-wordpress/ */ /** * Start: select box example */ function attachment_selectbox_edit($form_fields, $post) { // select options: you could code these manually or get it from a database $select_options = array( "there" => "There (over there)", "their" => "Their (their stuff)", "they're" => "They're (they are)", ); // get the current value of our custom field $current_value = get_post_meta($post->ID, "_mySelectBox", true); // build the html for our select box $mySelectBoxHtml = ""; // add our custom select box to the form_fields $form_fields["mySelectBox"]["label"] = __("Grammar nazi says what?"); $form_fields["mySelectBox"]["input"] = "html"; $form_fields["mySelectBox"]["html"] = $mySelectBoxHtml; return $form_fields; } add_filter("attachment_fields_to_edit", "attachment_selectbox_edit", null, 2); function attachment_selectbox_save($post, $attachment) { if( isset($attachment['mySelectBox']) ){ update_post_meta($post['ID'], '_mySelectBox', $attachment['mySelectBox']); } return $post; } add_filter("attachment_fields_to_save", "attachment_selectbox_save", null, 2); /** * End: select box example */ // --------------------------------------------------------------------------- /** * Start: checkbox example */ function attachment_checkbox_edit($form_fields, $post) { // get the current value of our custom field $current_value = get_post_meta($post->ID, "_myCheckBox", true); // if this value is the current_value we'll mark it checked $checked = ($current_value == "accepted_terms") ? ' checked ' : ''; // update 2010-08-05 @ 5:10pm CDT: hidden field takes over if the checkbox is unchecked, in essence deleting the value $myCheckBoxHtml = " "; $form_fields["myCheckBox"]["label"] = __("Accept the Terms Yo!"); $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); /** * End: checkbox example */