image_attachment_fields_to_save

The timeline below displays how wordpress function image_attachment_fields_to_save has changed across different WordPress versions. If a version is not listed, refer to the next available version below.

WordPress Version: 6.1

/**
 * Was used to filter input from media_upload_form_handler() and to assign a default
 * post_title from the file name if none supplied.
 *
 * @since 2.5.0
 * @deprecated 6.0.0
 *
 * @param array $post       The WP_Post attachment object converted to an array.
 * @param array $attachment An array of attachment metadata.
 * @return array Attachment post object converted to an array.
 */
function image_attachment_fields_to_save($post, $attachment)
{
    _deprecated_function(__FUNCTION__, '6.0.0');
    return $post;
}

WordPress Version: 5.5

/**
 * Filters input from media_upload_form_handler() and assigns a default
 * post_title from the file name if none supplied.
 *
 * Illustrates the use of the {@see 'attachment_fields_to_save'} filter
 * which can be used to add default values to any field before saving to DB.
 *
 * @since 2.5.0
 *
 * @param array $post       The WP_Post attachment object converted to an array.
 * @param array $attachment An array of attachment metadata.
 * @return array Filtered attachment post object.
 */
function image_attachment_fields_to_save($post, $attachment)
{
    if ('image' === substr($post['post_mime_type'], 0, 5)) {
        if (strlen(trim($post['post_title'])) == 0) {
            $attachment_url = isset($post['attachment_url']) ? $post['attachment_url'] : $post['guid'];
            $post['post_title'] = preg_replace('/\.\w+$/', '', wp_basename($attachment_url));
            $post['errors']['post_title']['errors'][] = __('Empty Title filled from filename.');
        }
    }
    return $post;
}

WordPress Version: 4.6

/**
 * Filters input from media_upload_form_handler() and assigns a default
 * post_title from the file name if none supplied.
 *
 * Illustrates the use of the {@see 'attachment_fields_to_save'} filter
 * which can be used to add default values to any field before saving to DB.
 *
 * @since 2.5.0
 *
 * @param array $post       The WP_Post attachment object converted to an array.
 * @param array $attachment An array of attachment metadata.
 * @return array Filtered attachment post object.
 */
function image_attachment_fields_to_save($post, $attachment)
{
    if (substr($post['post_mime_type'], 0, 5) == 'image') {
        if (strlen(trim($post['post_title'])) == 0) {
            $attachment_url = isset($post['attachment_url']) ? $post['attachment_url'] : $post['guid'];
            $post['post_title'] = preg_replace('/\.\w+$/', '', wp_basename($attachment_url));
            $post['errors']['post_title']['errors'][] = __('Empty Title filled from filename.');
        }
    }
    return $post;
}

WordPress Version: 4.1

/**
 * Filters input from media_upload_form_handler() and assigns a default
 * post_title from the file name if none supplied.
 *
 * Illustrates the use of the attachment_fields_to_save filter
 * which can be used to add default values to any field before saving to DB.
 *
 * @since 2.5.0
 *
 * @param array $post       The WP_Post attachment object converted to an array.
 * @param array $attachment An array of attachment metadata.
 * @return array Filtered attachment post object.
 */
function image_attachment_fields_to_save($post, $attachment)
{
    if (substr($post['post_mime_type'], 0, 5) == 'image') {
        if (strlen(trim($post['post_title'])) == 0) {
            $attachment_url = isset($post['attachment_url']) ? $post['attachment_url'] : $post['guid'];
            $post['post_title'] = preg_replace('/\.\w+$/', '', wp_basename($attachment_url));
            $post['errors']['post_title']['errors'][] = __('Empty Title filled from filename.');
        }
    }
    return $post;
}

WordPress Version: 4.0

/**
 * Filters input from media_upload_form_handler() and assigns a default
 * post_title from the file name if none supplied.
 *
 * Illustrates the use of the attachment_fields_to_save filter
 * which can be used to add default values to any field before saving to DB.
 *
 * @since 2.5.0
 *
 * @param WP_Post $post       The WP_Post attachment object.
 * @param array   $attachment An array of attachment metadata.
 * @return array Filtered attachment post object.
 */
function image_attachment_fields_to_save($post, $attachment)
{
    if (substr($post['post_mime_type'], 0, 5) == 'image') {
        if (strlen(trim($post['post_title'])) == 0) {
            $attachment_url = isset($post['attachment_url']) ? $post['attachment_url'] : $post['guid'];
            $post['post_title'] = preg_replace('/\.\w+$/', '', wp_basename($attachment_url));
            $post['errors']['post_title']['errors'][] = __('Empty Title filled from filename.');
        }
    }
    return $post;
}

WordPress Version: 3.7

/**
 * Filters input from media_upload_form_handler() and assigns a default
 * post_title from the file name if none supplied.
 *
 * Illustrates the use of the attachment_fields_to_save filter
 * which can be used to add default values to any field before saving to DB.
 *
 * @since 2.5.0
 *
 * @param object $post
 * @param array $attachment {@internal $attachment not used}}
 * @return array
 */
function image_attachment_fields_to_save($post, $attachment)
{
    if (substr($post['post_mime_type'], 0, 5) == 'image') {
        if (strlen(trim($post['post_title'])) == 0) {
            $attachment_url = isset($post['attachment_url']) ? $post['attachment_url'] : $post['guid'];
            $post['post_title'] = preg_replace('/\.\w+$/', '', wp_basename($attachment_url));
            $post['errors']['post_title']['errors'][] = __('Empty Title filled from filename.');
        }
    }
    return $post;
}