I'm trying to add a custom field to the media uploader in WordPress. I ahve it working, but I'd like to make the custom field key a hidden one.
If you are familiar with the way WordPress handles custom fields, you'll know that setting the key to "_something" will hide that key from the drop down lists visible to the user.
/**
* Add Video URL fields to media uploader
*
* http://www.billerickson.net/wordpress-add-custom-fields-media-gallery/
*
* @param $form_fields array, fields to include in attachment form
* @param $post object, attachment record in database
* @return $form_fields, modified form fields
*/
function capgun2012_attachment_fields( $form_fields, $post ) {
$form_fields['capgun2012_video_url'] = array(
'label' => 'Vimeo URL',
'input' => 'text',
'value' => get_post_meta( $post->ID, 'capgun2012_video_url', true ),
'helps' => 'If provided, photo will be displayed as a video',
);
return $form_fields;
}
add_filter( 'attachment_fields_to_edit', 'capgun2012_attachment_fields', 10, 2 );
/**
* Save values of Photographer Name and URL in media uploader
*
* @param $post array, the post data for database
* @param $attachment array, attachment fields from $_POST form
* @return $post array, modified post data
*/
function capgun2012_attachment_fields_save( $post, $attachment ) {
if( isset( $attachment['capgun2012_video_url'] ) ) {
update_post_meta( $post['ID'], 'capgun2012_video_url', $attachment['capgun2012_video_url'] );
}
return $post;
}
add_filter( 'attachment_fields_to_save', 'capgun2012_attachment_fields_save', 10, 2 );
If I simply replace all the occurrences of "capgun2012_video_url" with "_capgun2012_video_url" then it doesn't work. I'm starting to think that the media uploader doesn't play well with hidden custom fields.
Please see attached screenshot of what I don't want to happen (the custom key showing in the custom fields drop down).
Thanks for the help.