Below is a complete, drop-in snippet for your theme’s functions.php (or a small must-use/plugin file). It adds a single text field (“Photographer Name”), shows it in the Media edit screen and modal, saves it safely, and makes it easy to output on the front end.
1) Add the field to the Media edit form
/**
* Add a custom field to the Media edit screen (and media modal “Attachment Details”).
*/
function mymedia_add_custom_field( $form_fields, $post ) {
$value = get_post_meta( $post->ID, ‘_mymedia_photographer’, true );
// Add a nonce field once per screen load (safe to repeat—WP will de-duplicate by name).
$form_fields[‘mymedia_nonce’] = array(
‘label’ => ”,
‘input’ => ‘html’,
‘html’ => wp_nonce_field( ‘mymedia_save_field_’ . $post->ID, ‘mymedia_nonce’, true, false ),
);
$form_fields[‘mymedia_photographer’] = array(
‘label’ => __( ‘Photographer’, ‘your-textdomain’ ),
‘input’ => ‘text’,
‘value’ => $value,
‘helps’ => __( ‘Enter the photographer or credit line.’, ‘your-textdomain’ ),
);
return $form_fields;
}
add_filter( ‘attachment_fields_to_edit’, ‘mymedia_add_custom_field’, 10, 2 );
2) Save the field securely
/**
* Save our custom field from Media edit screen.
*/
function mymedia_save_custom_field( $post, $attachment ) {
$attachment_id = isset( $post[‘ID’] ) ? (int) $post[‘ID’] : 0;
if ( ! $attachment_id || ! current_user_can( ‘edit_post’, $attachment_id ) ) {
return $post; // Respect capabilities
}
// Nonce check
if ( empty( $_POST[‘mymedia_nonce’] ) || ! wp_verify_nonce( $_POST[‘mymedia_nonce’], ‘mymedia_save_field_’ . $attachment_id ) ) {
return $post;
}
if ( isset( $attachment[‘mymedia_photographer’] ) ) {
$value = sanitize_text_field( $attachment[‘mymedia_photographer’] );
update_post_meta( $attachment_id, ‘_mymedia_photographer’, $value );
}
return $post;
}
add_filter( ‘attachment_fields_to_save’, ‘mymedia_save_custom_field’, 10, 2 );
3) Display the field on the front end
/**
* Output a credit below a featured image or any attachment.
*/
function mymedia_output_credit( $attachment_id ) {
$credit = get_post_meta( $attachment_id, ‘_mymedia_photographer’, true );
if ( $credit ) {
echo ‘<p class=”photo-credit”>Photo: ‘ . esc_html( $credit ) . ‘</p>’;
}
}
Use mymedia_output_credit( get_post_thumbnail_id() ); in templates, or call it when rendering galleries.
Show the credit automatically with images:
You can filter the markup generated by wp_get_attachment_image() attributes:
add_filter( ‘wp_get_attachment_image_attributes’, function( $attr, $attachment ) {
$credit = get_post_meta( $attachment->ID, ‘_mymedia_photographer’, true );
if ( $credit ) {
$attr[‘data-photo-credit’] = $credit; // handy for JS or tooltips
}
return $attr;
}, 10, 2 );
Optional: Add a column in the Media Library list
This helps editors see/use the field without clicking into each item.
// Register column
add_filter( ‘manage_upload_columns’, function( $cols ) {
$cols[‘mymedia_photographer’] = __( ‘Photographer’, ‘your-textdomain’ );
return $cols;
} );
// Render column
add_action( ‘manage_media_custom_column’, function( $column_name, $attachment_id ) {
if ( ‘mymedia_photographer’ === $column_name ) {
$val = get_post_meta( $attachment_id, ‘_mymedia_photographer’, true );
echo $val ? esc_html( $val ) : ‘—’;
}
}, 10, 2 );
You can also make it sortable with manage_upload_sortable_columns and a pre_get_posts modification, if desired.
Leave a Reply