I’m trying to add a custom field to image and gallery edit screen.
I’m aware there are similar questions here, although answers either don’t work with the current version or they add field only to upload screen while I need a field to be visible in all edit screens (see below).
For this, I believe, you’ll have to mess with backbone.js as attachment_fields_to_edit
only adds to upload screen.
Adding field with attachment_fields_to_edit

↑ This is upload image screeen. Here Style is added with attachment_fields_to_edit
filter, and my_field is added with ACF plugin.
But they are missing in edit screen in the post

↑ Click edit on the actual post

↑ No style & my_field fields!
The question
How to add fields to still have them on the edit screen? Ideally answer will include adding fields to gallery edit screen if it’s a similar process
This question is really important for me so I’ll be adding a 100 rep bounty when it’s available.
Thanks!
Here is the working code (working fine for me), did you tried this? Just add to theme ‘functions.php’ and change the custom field names as needed.
//function to add custom media field
function custom_media_add_media_custom_field( $form_fields, $post ) {
$field_value = get_post_meta( $post->ID, 'custom_media_style', true );
$form_fields['custom_media_style'] = array(
'value' => $field_value ? $field_value : '',
'label' => __( 'Style' ),
'helps' => __( 'Enter your style' ),
'input' => 'textarea'
);
return $form_fields;
}
add_filter( 'attachment_fields_to_edit', 'custom_media_add_media_custom_field', null, 2 );
//save your custom media field
function custom_media_save_attachment( $attachment_id ) {
if ( isset( $_REQUEST['attachments'][ $attachment_id ]['custom_media_style'] ) ) {
$custom_media_style = $_REQUEST['attachments'][ $attachment_id ]['custom_media_style'];
update_post_meta( $attachment_id, 'custom_media_style', $custom_media_style );
}
}
add_action( 'edit_attachment', 'custom_media_save_attachment' );