Title: attachment_fields_to_save
Published: April 25, 2014
Last modified: August 20, 2026

---

# apply_filters( ‘attachment_fields_to_save’, array $post, array $attachment )

## In this article

 * [Description](https://developer.wordpress.org/reference/hooks/attachment_fields_to_save/?output_format=md#description)
    - [See also](https://developer.wordpress.org/reference/hooks/attachment_fields_to_save/?output_format=md#see-also)
 * [Parameters](https://developer.wordpress.org/reference/hooks/attachment_fields_to_save/?output_format=md#parameters)
 * [More Information](https://developer.wordpress.org/reference/hooks/attachment_fields_to_save/?output_format=md#more-information)
 * [Source](https://developer.wordpress.org/reference/hooks/attachment_fields_to_save/?output_format=md#source)
 * [Related](https://developer.wordpress.org/reference/hooks/attachment_fields_to_save/?output_format=md#related)
 * [Changelog](https://developer.wordpress.org/reference/hooks/attachment_fields_to_save/?output_format=md#changelog)
 * [User Contributed Notes](https://developer.wordpress.org/reference/hooks/attachment_fields_to_save/?output_format=md#user-contributed-notes)

[ Back to top](https://developer.wordpress.org/reference/hooks/attachment_fields_to_save/?output_format=md#wp--skip-link--target)

Filters the attachment fields to be saved.

## 󠀁[Description](https://developer.wordpress.org/reference/hooks/attachment_fields_to_save/?output_format=md#description)󠁿

### 󠀁[See also](https://developer.wordpress.org/reference/hooks/attachment_fields_to_save/?output_format=md#see-also)󠁿

 * [wp_get_attachment_metadata()](https://developer.wordpress.org/reference/functions/wp_get_attachment_metadata/)

## 󠀁[Parameters](https://developer.wordpress.org/reference/hooks/attachment_fields_to_save/?output_format=md#parameters)󠁿

 `$post`array

An array of post data.

`$attachment`array

An array of attachment metadata.

## 󠀁[More Information](https://developer.wordpress.org/reference/hooks/attachment_fields_to_save/?output_format=md#more-information)󠁿

The `attachment_fields_to_save` filter is used to filter the associated data of 
images.

By default, it receives the input from the Media Upload screen and provides default
values to the `post_title`, in case the user hasn’t done so.

It returns the `$post` array to be handled by the `media_upload_form_handler` function.

A plugin (or theme) can register as a content filter with the code:

`<?php add_filter( 'attachment_fields_to_save', 'filter_function_name', 10, 2 ) ?
>`

Where ‘filter_function_name’ is the function WordPress should call when an attachment
is being saved.

Note that the filter function **must** return the `$post` array after it is finished
processing.

**NOTE:** per [ticket #30687](https://core.trac.wordpress.org/ticket/30687), any
validation errors passed in via `$post['errors']` array are silently unset and the
attachment is processed without notifying the user

## 󠀁[Source](https://developer.wordpress.org/reference/hooks/attachment_fields_to_save/?output_format=md#source)󠁿

    ```php
    $post = apply_filters( 'attachment_fields_to_save', $post, $attachment );
    ```

[View all references](https://developer.wordpress.org/reference/files/wp-admin/includes/media.php/)
[View on Trac](https://core.trac.wordpress.org/browser/tags/7.1/src/wp-admin/includes/media.php#L810)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/7.1/src/wp-admin/includes/media.php#L810-L810)

## 󠀁[Related](https://developer.wordpress.org/reference/hooks/attachment_fields_to_save/?output_format=md#related)󠁿

| Used by | Description | 
| [media_upload_form_handler()](https://developer.wordpress.org/reference/functions/media_upload_form_handler/)`wp-admin/includes/media.php` |

Handles form submissions for the legacy media uploader.

  | 
| [edit_post()](https://developer.wordpress.org/reference/functions/edit_post/)`wp-admin/includes/post.php` |

Updates an existing post with values provided in `$_POST`.

  | 
| [wp_ajax_save_attachment_compat()](https://developer.wordpress.org/reference/functions/wp_ajax_save_attachment_compat/)`wp-admin/includes/ajax-actions.php` |

Handles saving backward compatible attachment attributes via AJAX.

  |

## 󠀁[Changelog](https://developer.wordpress.org/reference/hooks/attachment_fields_to_save/?output_format=md#changelog)󠁿

| Version | Description | 
| [2.5.0](https://developer.wordpress.org/reference/since/2.5.0/) | Introduced. |

## 󠀁[User Contributed Notes](https://developer.wordpress.org/reference/hooks/attachment_fields_to_save/?output_format=md#user-contributed-notes)󠁿

 1.   [Skip to note 3 content](https://developer.wordpress.org/reference/hooks/attachment_fields_to_save/?output_format=md#comment-content-4617)
 2.    [Collins Mbaka](https://profiles.wordpress.org/collinsmbaka/)  [  6 years ago  ](https://developer.wordpress.org/reference/hooks/attachment_fields_to_save/#comment-4617)
 3.  [You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Fhooks%2Fattachment_fields_to_save%2F%23comment-4617)
     Vote results for this note: 0[You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Fhooks%2Fattachment_fields_to_save%2F%23comment-4617)
 4.  To insert a default caption for images:
 5.      ```php
         function wpdocs_insert_custom_default_caption( $post ) {
             if ( 'image' === substr( $post['post_mime_type'], 0, 5 ) ) {
                 if ( 0 === strlen( trim( $post['post_title'] ) ) ) {
                     $post['post_title'] = preg_replace( '/\.\w+$/', '', basename( $post['guid'] ) );
                     $post['errors']['post_title']['errors'][] = __( 'Empty Title filled from filename.' );
                 }
     
                 // captions are saved as the post_excerpt, so we check for it before overwriting
                 // if no captions were provided by the user, we fill it with our default
                 if ( 0 === strlen( trim( $post['post_excerpt'] ) ) ) {
                     $post['post_excerpt'] = 'default caption';
                 }
             }
     
             return $post;
         }
     
         add_filter( 'attachment_fields_to_save', 'wpdocs_insert_custom_default_caption' );
         ```
     
 6.   [Log in to add feedback](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Fhooks%2Fattachment_fields_to_save%2F%3Freplytocom%3D4617%23feedback-editor-4617)
 7.   [Skip to note 4 content](https://developer.wordpress.org/reference/hooks/attachment_fields_to_save/?output_format=md#comment-content-4627)
 8.    [Akira Tachibana](https://profiles.wordpress.org/atachibana/)  [  6 years ago  ](https://developer.wordpress.org/reference/hooks/attachment_fields_to_save/#comment-4627)
 9.  [You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Fhooks%2Fattachment_fields_to_save%2F%23comment-4627)
     Vote results for this note: 0[You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Fhooks%2Fattachment_fields_to_save%2F%23comment-4627)
 10. (From Codex)
      To insert a default caption for images:
 11.     ```php
         function insert_custom_default_caption($post, $attachment) {
         if ( substr($post['post_mime_type'], 0, 5) == 'image' ) {
             if ( strlen(trim($post['post_title'])) == 0 ) {
                 $post['post_title'] = preg_replace('/\.\w+$/', '', basename($post['guid']));
                 $post['errors']['post_title']['errors'][] = __('Empty Title filled from filename.');
             }
             // captions are saved as the post_excerpt, so we check for it before overwriting
             // if no captions were provided by the user, we fill it with our default
             if ( strlen(trim($post['post_excerpt'])) == 0 ) {
                 $post['post_excerpt'] = 'default caption';
             }
         }
     
         return $post;
         }
     
         add_filter('attachment_fields_to_save', 'insert_custom_default_caption', 10, 2);
         ```
     
 12.  [Log in to add feedback](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Fhooks%2Fattachment_fields_to_save%2F%3Freplytocom%3D4627%23feedback-editor-4627)

You must [log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Fhooks%2Fattachment_fields_to_save%2F)
before being able to contribute a note or feedback.