apply_filters( 'attachment_fields_to_edit', array $form_fields, WP_Post $post )

Filters the attachment fields to edit.


Parameters Parameters

$form_fields

(array) An array of attachment form fields.

$post

(WP_Post) The WP_Post attachment object.


Top ↑

Source Source

File: wp-admin/includes/media.php

View on Trac



Top ↑

Changelog Changelog

Changelog
Version Description
2.5.0 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

  1. Skip to note 1 content
    Contributed by Razon

    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);
    

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