attachment_fields = $fields;
add_filter( 'attachment_fields_to_edit', array( $this, 'addFields' ), 11, 2 );
add_filter( 'attachment_fields_to_save', array( $this, 'saveFields' ), 11, 2);
}
public function addFields( $form_fields, $post = null) {
if ( ! empty( $this->attachment_fields ) ) {
foreach ( $this->attachment_fields as $field => $values ) {
if ( preg_match( "/" . $values['application'] . "/", $post->post_mime_type) && ! in_array( $post->post_mime_type, $values['exclusions'] ) ) {
$meta = get_post_meta( $post->ID, '_' . $field, true );
switch ( $values['input'] ) {
default:
case 'text':
$values['input'] = 'text';
break;
case 'textarea':
$values['input'] = 'textarea';
break;
case 'select':
$values['input'] = 'html';
$html = '';
$values['html'] = $html;
break;
case 'checkbox':
$values['input'] = 'html';
if ( $meta == 'on' )
$checked = ' checked="checked"';
else
$checked = '';
$html = '';
$values['html'] = $html;
break;
case 'radio':
$values['input'] = 'html';
$html = '';
if ( ! empty( $values['options'] ) ) {
$i = 0;
foreach ( $values['options'] as $key => $value ) {
if ( $meta == $key )
$checked = ' checked="checked"';
else
$checked = '';
$html .= '
';
$i++;
}
}
$values['html'] = $html;
break;
}
$values['value'] = $meta;
$form_fields[$field] = $values;
}
}
}
return $form_fields;
}
function saveFields( $post, $attachment ) {
if ( ! empty( $this->attachment_fields ) ) {
foreach ( $this->attachment_fields as $field => $values ) {
if ( isset( $attachment[$field] ) ) {
if ( strlen( trim( $attachment[$field] ) ) == 0 )
$post['errors'][$field]['error'][] = __( $values['error_text'] );
else
update_post_meta( $post['ID'], '_' . $field, $attachment[$field] );
}
else {
delete_post_meta( $post['ID'], $field );
}
}
}
return $post;
}
}
$am = new Attachment_Meta( $additional_fields );