apply_filters( 'attachment_fields_to_save', array $post, array $attachment )

Filters the attachment fields to be saved.


Description Description

See also See also


Top ↑

Parameters Parameters

$post

(array) An array of post data.

$attachment

(array) An array of attachment metadata.


Top ↑

More Information 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, any validation errors passed in via $post['errors'] array are silently unset and the attachment is processed without notifying the user


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 Collins Mbaka

    To insert a default caption for images:

    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' );
    
  2. Skip to note 2 content
    Contributed by Akira Tachibana

    (From Codex)
    To insert a default caption for images:

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

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