I am using the following code to add a couple of custom fields to media attachments:

//Add custom fields for attachments

function kgal_add_custom_fields_to_artwork( $form_fields, $post ) {

    $form_fields['kgal_artwork_artist'] = array(
        'label'     => 'Artist',
        'input'     => 'html',
        'html'      => get_artists_dropdown( $post->ID ),
        'application'   => 'image',
        'exclusions'    => array( 'audio', 'video' ),

    $form_fields['kgal_artwork_medium'] = array(
        'label'     => 'Medium',
        'input'     => 'text',
        'value'     => get_post_meta( $post->ID, 'kgal_artwork_medium', true ),
        'application'   => 'image',
        'exclusions'    => array( 'audio', 'video' ),

    return $form_fields;

add_filter( 'attachment_fields_to_edit', 'kgal_add_custom_fields_to_artwork', 10, 2 );

function kgal_save_custom_fields_for_artwork( $post, $attachment ) {

    if( isset( $attachment['kgal_artwork_artist'] ) )
        update_post_meta( $post['ID'], 'kgal_artwork_artist', $attachment['kgal_artwork_artist'] );

    if( isset( $attachment['kgal_artwork_medium'] ) )
        update_post_meta( $post['ID'], 'kgal_artwork_medium', $attachment['kgal_artwork_medium'] );

    return $post;

add_filter( 'attachment_fields_to_save', 'kgal_save_custom_fields_for_artwork', 10, 2 );

// output artists dropdown
function get_artists_dropdown( $id ) {
        if( !$id )

        $artists = get_posts(
                        'post_type'             => 'artist',
                        'posts_per_page'        => -1,
                        'post_status'           => 'publish',

        if( !$artists )

        $output = "<select name='attachments[{$id}][kgal_artwork_artist]' id='attachments[{$id}][kgal_artwork_artist]'>";
        foreach( $artists as $artist )
                $output .= '<option ' . selected( get_post_meta( $id, "kgal_artwork_artist", true ), $artist->post_id, false ) . ' value="' . $artist->post_id . '">' . $artist->post_title . '</option>';
        $output .= '</select>';

        return $output;

Now when I go to media library to edit an image, the text field i.e. kgal_artwork_medium updates perfectly fine i.e. the saving gif spins and after navigating to another image and getting back to the one I've just edited, I see the correct value there. I've also displayed the field in my attachments page template, and it shows up just fine on the front end.

However, when it comes to the html field i.e. kgal_artwork_artist, things get messy. The dropdown shows up just fine, populated with all the artists by name as expected, and when I select an artist from it, the saving gif also spins just fine, but the actual data doesn't seem to get saved, and upon navigating to another image and getting back, the dropdown always reverts to displaying the last artist in the list. Also, nothing shows up for the artist at the frontend on the attachment's page.

There might be some issue with my selected() function that I'm missing out on here, or perhaps I'm not saving the right value...the dropdown needs to display the artist's name, but save its ID, so that I can utilize the ID to show whatever data I need in my template.

I'm totally lost at this point, but I know I'm really, REALLY close and I've been trying to get this to work for over a couple of months now, to the point when I'm completely frustrated. Any help in this regard will be IMMENSELY appreciated.

1 Answer 1


I think you need to change the value of id in select field

$output = "<select name='attachments[{$id}][kgal_artwork_artist]' id='attachments-'.$id.'-kgal_artwork_artist'>";

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.