Skip to content

Instantly share code, notes, and snippets.

@joshcanhelp
Created July 10, 2013 17:42
Show Gist options
  • Save joshcanhelp/5968442 to your computer and use it in GitHub Desktop.
Save joshcanhelp/5968442 to your computer and use it in GitHub Desktop.
Adding meta fields to attachments
/*
* Meta fields for media uploads
*/
/*
* @param array $form_fields
* @param object $post
*
* @return array
*/
function add_attachment_fields_to_edit ( $form_fields, $post )
{
$form_fields[ "name_for_this_field" ] = array(
'label' => __ ( "Title for this field", 'domain' ),
'input' => 'text',
'value' => get_post_meta ( $post->ID, 'name_for_this_field', TRUE ),
'helps' => 'Help text, if desired'
);
return $form_fields;
}
add_filter ( "attachment_fields_to_edit", "add_attachment_fields_to_edit", NULL, 2 );
/*
* @param array $post
* @param array $attachment
*
* @return array
*/
function add_attachment_fields_to_save ( $post, $attachment )
{
if ( isset( $attachment[ 'name_for_this_field' ] ) ) {
update_post_meta ( $post[ 'ID' ], 'name_for_this_field', $attachment[ 'name_for_this_field' ] );
}
return $post;
}
add_filter ( 'attachment_fields_to_save', 'add_attachment_fields_to_save', NULL, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment