Skip to content

Instantly share code, notes, and snippets.

@anilmeena
Created October 24, 2016 03:35
Show Gist options
  • Select an option

  • Save anilmeena/1f5451aec93d271ca3244765ff404d62 to your computer and use it in GitHub Desktop.

Select an option

Save anilmeena/1f5451aec93d271ca3244765ff404d62 to your computer and use it in GitHub Desktop.
Add extra fields in media attachment
<?php
/* Add extra fields in media attachment */
/**
* 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 custom_image_class_field( $form_fields, $post ) {
$form_fields['image-class-name'] = array(
'label' => 'Image Class',
'input' => 'text',
'value' => get_post_meta( $post->ID, 'image_class_name', true ),
'helps' => 'Please provide image class eg. icons, coupons etc.',
);
/*$form_fields['be-photographer-url'] = array(
'label' => 'Photographer URL',
'input' => 'text',
'value' => get_post_meta( $post->ID, 'be_photographer_url', true ),
'helps' => 'Add Photographer URL',
);*/
return $form_fields;
}
add_filter( 'attachment_fields_to_edit', 'custom_image_class_field', 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 be_attachment_field_credit_save( $post, $attachment ) {
if( isset( $attachment['image-class-name'] ) )
update_post_meta( $post['ID'], 'image_class_name', $attachment['image-class-name'] );
/*if( isset( $attachment['be-photographer-url'] ) )
update_post_meta( $post['ID'], 'be_photographer_url', esc_url( $attachment['be-photographer-url'] ) );*/
return $post;
}
add_filter( 'attachment_fields_to_save', 'be_attachment_field_credit_save', 10, 2 );
/* Add extra fields in media attachment end */
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment