Family Encyclopedia >> Electronics

How to Add Custom Fields to WordPress Media Uploader: Photographer Credits Guide

During a recent project building a stunning image gallery with WordPress attachments and custom post types, our team needed to add custom metadata to images—like photographer names and URLs—for proper crediting. WordPress treats attachments as posts, so adding fields works like custom post meta. Without a built-in UI for attachment fields, we enhanced the media uploader itself. In this guide, drawn from real-world experience, we'll walk you through adding these fields using proven hooks.

We leverage two key WordPress filters: attachment_fields_to_edit and attachment_fields_to_save.

For best practices, create a site-specific plugin (or add to your theme's functions.php for quick testing). Here's the battle-tested code:

/**
 * Add photographer name and URL fields to the media uploader
 *
 * @param array $form_fields Fields to include in attachment form
 * @param WP_Post $post      Attachment post record
 * @return array             Modified form fields
 */
function be_attachment_field_credit( $form_fields, $post ) {
    $form_fields['be-photographer-name'] = array(
        'label' => 'Photographer Name',
        'input' => 'text',
        'value' => get_post_meta( $post->ID, 'be_photographer_name', true ),
        'help'  => 'If provided, photo credit will be displayed',
    );

    $form_fields['be-photographer-url'] = array(
        'label' => 'Photographer URL',
        'input' => 'text',
        'value' => get_post_meta( $post->ID, 'be_photographer_url', true ),
        'help'  => 'Add photographer URL',
    );

    return $form_fields;
}
add_filter( 'attachment_fields_to_edit', 'be_attachment_field_credit', 10, 2 );

/**
 * Save photographer name and URL values from media uploader
 *
 * @param array $post       Post data for DB
 * @param array $attachment Attachment POST data
 * @return array            Modified post data
 */
function be_attachment_field_credit_save( $post, $attachment ) {
    if ( isset( $attachment['be-photographer-name'] ) ) {
        update_post_meta( $post['ID'], 'be_photographer_name', sanitize_text_field( $attachment['be-photographer-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 );

This adds 'Photographer Name' and 'Photographer URL' fields to the media uploader sidebar, as shown below:

How to Add Custom Fields to WordPress Media Uploader: Photographer Credits Guide

Code Breakdown: The first function builds an array defining each field's label, type, current value (from meta), and help text. The second checks submitted values and updates post meta securely with sanitization.

To display in an attachment template (inside the loop):

<?php echo get_post_meta( $post->ID, 'be_photographer_name', true ); ?>

For featured images in archives or other templates:

<?php echo get_post_meta( get_post_thumbnail_id(), 'be_photographer_url', true ); ?>

Enhance your galleries with proper credits—trusted by developers worldwide. Hat tip to Bill Erickson for the original technique. Stay tuned for our next guide on attachment templates.