2

How do I add custom field to the image editor, such as under the image 'Description' field? To be much clearer, it's where the settings for the images are after you upload an image in the attachment pop-up window.

1 Answer 1

7

To add a custom field to attachments, place the following code in your functions.php.

add_filter('attachment_fields_to_edit', 'edit_media_custom_field', 11, 2 );
add_filter('attachment_fields_to_save', 'save_media_custom_field', 11, 2 );

function edit_media_custom_field( $form_fields, $post ) {
    $form_fields['custom_field'] = array( 'label' => 'Custom Field', 'input' => 'text', 'value' => get_post_meta( $post->ID, '_custom_field', true ) );
    return $form_fields;
}

function save_media_custom_field( $post, $attachment ) {
    update_post_meta( $post['ID'], '_custom_field', $attachment['custom_field'] );
    return $post;
}

To output that data you can use the following in the loop:

get_post_meta( get_the_ID(), '_custom_field', true ) );

Your Answer

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

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