Skip to content

Instantly share code, notes, and snippets.

@twentyfortysix
Last active February 15, 2016 13:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save twentyfortysix/d7dcdc931aed3f2529a9 to your computer and use it in GitHub Desktop.
Save twentyfortysix/d7dcdc931aed3f2529a9 to your computer and use it in GitHub Desktop.
WP - register custom field for media
<?php
function be_attachment_field_credit( $form_fields, $post ) {
//
// ---- CS ---
//
$form_fields['media_date'] = array(
'label' => 'Datum',
'input' => 'text',
'value' => get_post_meta( $post->ID, 'media_date', true ),
// 'helps' => 'datace',
);
return $form_fields;
}
add_filter( 'attachment_fields_to_edit', 'be_attachment_field_credit', 10, 2 );
/**
* Save media values
*
* @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['media_date'] ) )
update_post_meta( $post['ID'], 'media_date', esc_attr( $attachment['media_date'] ) );
return $post;
}
add_filter( 'attachment_fields_to_save', 'be_attachment_field_credit_save', 10, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment