Skip to content

Instantly share code, notes, and snippets.

@manishsongirkar
Created October 18, 2012 10:55
Show Gist options
  • Save manishsongirkar/3910985 to your computer and use it in GitHub Desktop.
Save manishsongirkar/3910985 to your computer and use it in GitHub Desktop.
Add Custom input field in media uploader.
/**
* Add Photographer Name and URL fields to media uploader
*
* @param $form_fields array, fields to include in attachment form
* @param $post object, attachment record in database
*
* @return $form_fields, modified form fields
*/
function rtp_attachment_field_credit( $form_fields, $post ) {
$form_fields['rtp-photographer-name'] = array(
'label' => 'Photographer Name',
'input' => 'text',
'value' => get_post_meta( $post->ID, 'rtp_photographer_name', true ),
'helps' => 'Provided name of photographer',
);
$form_fields['rtp-photographer-url'] = array(
'label' => 'Photographer URL',
'input' => 'text',
'value' => get_post_meta( $post->ID, 'rtp_photographer_url', true ),
'helps' => 'If photographer name is present, will link here',
);
return $form_fields;
}
add_filter( 'attachment_fields_to_edit', 'rtp_attachment_field_credit', 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 rtp_attachment_field_credit_save( $post, $attachment ) {
if( isset( $attachment['rtp-photographer-name'] ) )
update_post_meta( $post['ID'], 'rtp_photographer_name', $attachment['rtp-photographer-name'] );
if( isset( $attachment['rtp-photographer-url'] ) )
update_post_meta( $post['ID'], 'rtp_photographer_url', $attachment['rtp-photographer-url'] );
return $post;
}
add_filter( 'attachment_fields_to_save', 'rtp_attachment_field_credit_save', 10, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment