钩子文档

attachment_fields_to_edit

💡 云策文档标注

概述

attachment_fields_to_edit 是一个 WordPress 过滤器,用于自定义附件编辑表单中的字段。它允许开发者添加、修改或移除附件编辑界面中的表单字段,但标准字段不通过此过滤器传递。

关键要点

  • 过滤器用于修改附件编辑表单字段数组,参数包括 $form_fields(字段数组)和 $post(附件对象)。
  • 过滤器函数必须返回处理后的字段数组,否则可能导致无字段显示或插件错误。
  • 主要用于添加自定义字段,如文本、文本域、复选框或 HTML 类型字段。
  • 自定义字段的保存通常需要配合 attachment_fields_to_save 过滤器或 edit_attachment 动作钩子。
  • 注意自定义字段可能缺少完整的 Ajax 反馈(如“Saved.”状态显示)。

代码示例

// 添加自定义文本字段到附件编辑表单
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', // 也可使用 'textarea'
        '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);

注意事项

  • 确保过滤器函数返回数组,避免字段丢失。
  • 自定义字段的保存逻辑需单独实现,例如使用 attachment_fields_to_save 过滤器。
  • 使用 'html' 输入类型可以创建非标准字段(如按钮),但需注意界面一致性。
  • 自定义字段可能不会触发完整的 Ajax 保存状态反馈,需测试用户体验。

📄 原文内容

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

Version Description
2.5.0 Introduced.

User Contributed Notes

  1. Skip to note 5 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 6 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 7 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!

  4. Skip to note 8 content

    There are “text” and “textarea” field types that can be used by default. To have another field type, you can simply use the “html” type. Here is an example of how to add a button:

    // Add the custom meta field to the media file settings
    function wpdocs_button_attachment_ui( $form_fields ) {
        // Add your custom meta field
        $form_fields['custom_meta_field'] = array(
            'label' => __( 'My Button' ),
            'input' => 'html',
            'html'  => 'My Button Name',
            'helps' => __( 'Some help here if you need it.' ),
        );
    
        return $form_fields;
    }
    
    add_filter( 'attachment_fields_to_edit', 'wpdocs_button_attachment_ui' );