apply_filters( ‘attachment_fields_to_edit’, array $form_fields, WP_Post $post )

Filters the attachment fields to edit.

Parameters

$form_fieldsarray
An array of attachment form fields.
$postWP_Post
The WP_Post attachment object.

More Information

  • WordPress does not pass standard fields through this filter, though this can still be used for adding attachment form fields.
  • Note that the filter function must return an array of attachment form fields after it is finished processing; otherwise, no fields will be displayed when editing an attachment and other plugins also filtering the $form_fields array may generate errors.

Source

$form_fields = apply_filters( 'attachment_fields_to_edit', $form_fields, $post );

Changelog

VersionDescription
2.5.0Introduced.

User Contributed Notes

  1. Skip to note 4 content

    This is the way to add custom fields to attachments:

    // Add custom text/textarea attachment field
    function add_custom_text_field_to_attachment_fields_to_edit( $form_fields, $post ) {
        $text_field = get_post_meta($post->ID, 'text_field', true);
        $form_fields['text_field'] = array(
            'label' => 'Custom text field',
            'input' => 'text', // you may alos use 'textarea' field
            'value' => $text_field,
            'helps' => 'This is help text'
        );
        return $form_fields;
    }
    add_filter('attachment_fields_to_edit', 'add_custom_text_field_to_attachment_fields_to_edit', null, 2); 
    
    // Save custom text/textarea attachment field
    function save_custom_text_attachment_field($post, $attachment) {  
        if( isset($attachment['text_field']) ){  
            update_post_meta($post['ID'], 'text_field', sanitize_text_field( $attachment['text_field'] ) );  
        }else{
             delete_post_meta($post['ID'], 'text_field' );
        }
        return $post;  
    }
    add_filter('attachment_fields_to_save', 'save_custom_text_attachment_field', null, 2);
    
    
    // Add custom checkbox attachment field
    function add_custom_checkbox_field_to_attachment_fields_to_edit( $form_fields, $post ) {
        $checkbox_field = (bool) get_post_meta($post->ID, 'checkbox_field', true);
        $form_fields['checkbox_field'] = array(
            'label' => 'Checkbox',
            'input' => 'html',
            'html' => '<input type="checkbox" id="attachments-'.$post->ID.'-checkbox_field" name="attachments['.$post->ID.'][checkbox_field]" value="1"'.($checkbox_field ? ' checked="checked"' : '').' /> ',
            'value' => $checkbox_field,
            'helps' => ''
        );
    	return $form_fields;
    }
    add_filter('attachment_fields_to_edit', 'add_custom_checkbox_field_to_attachment_fields_to_edit', null, 2); 
    
    // Save custom checkbox attachment field
    function save_custom_checkbox_attachment_field($post, $attachment) {  
        if( isset($attachment['checkbox_field']) ){  
            update_post_meta($post['ID'], 'checkbox_field', sanitize_text_field( $attachment['checkbox_field'] ) );  
        }else{
             delete_post_meta($post['ID'], 'checkbox_field' );
        }
        return $post;  
    }
    add_filter('attachment_fields_to_save', 'save_custom_checkbox_attachment_field', null, 2);
  2. Skip to note 5 content

    Example migrated from Codex:

    The following example adds a “Location” field for all attachments. This example also uses the “edit_attachment” action hook to save the submitted custom attachment form fields value to the post meta of the post in which the attachment belongs.

    add_filter( 'attachment_fields_to_edit', 'my_add_attachment_location_field', 10, 2 );
    
    function my_add_attachment_location_field( $form_fields, $post ) {
        $field_value = get_post_meta( $post->ID, 'location', true );
        $form_fields['location'] = array(
            'value' => $field_value ? $field_value : '',
            'label' => __( 'Location' ),
            'helps' => __( 'Set a location for this attachment' )
        );
        return $form_fields;
    }
    
    add_action( 'edit_attachment', 'my_save_attachment_location' );
    
    function my_save_attachment_location( $attachment_id ) {
        if ( isset( $_REQUEST['attachments'][$attachment_id]['location'] ) ) {
            $location = $_REQUEST['attachments'][$attachment_id]['location'];
            update_post_meta( $attachment_id, 'location', $location );
        }
    }
  3. Skip to note 6 content

    If you add a custom field to attachments as described above, the field will be displayed and saved as well. But: There is no complete Ajax feedback to the user: the save icon appears in the top-right corner. “attachment-details” gets the class “save-waiting”, then again “save-ready”. The class “save-complete” is omitted. For the standard fields again this status is displayed. The text “Saved.” appears. This should be displayed for custom fields, too!

You must log in before being able to contribute a note or feedback.