Using WordPress ‘image_attachment_fields_to_save()’ PHP function

The image_attachment_fields_to_save() WordPress PHP function was used to filter input from media_upload_form_handler() and to assign a default post_title from the file name if none supplied.

Usage

image_attachment_fields_to_save($post, $attachment);

Parameters

  • $post (array) – Required. The WP_Post attachment object converted to an array.
  • $attachment (array) – Required. An array of attachment metadata.

More information

See WordPress Developer Resources: image_attachment_fields_to_save

Examples

Filtering attachment metadata

This example filters the attachment metadata and assigns a default post_title if none is supplied.

// Attachment metadata
$attachment = array(
    'post_mime_type' => 'image/jpeg',
    'post_title' => '',
    'post_content' => '',
    'post_status' => 'inherit'
);

// Convert WP_Post object to an array
$post = (array) get_post($attachment_id);

// Use the function
$result = image_attachment_fields_to_save($post, $attachment);

Updating attachment metadata

This example updates attachment metadata using image_attachment_fields_to_save() and wp_update_attachment_metadata().

// Existing attachment ID
$attachment_id = 123;

// New metadata
$new_metadata = array(
    'post_title' => 'New Title',
    'post_content' => 'New description'
);

// Get attachment post data as an array
$post = (array) get_post($attachment_id);

// Filter and update metadata
$filtered_metadata = image_attachment_fields_to_save($post, $new_metadata);
wp_update_attachment_metadata($attachment_id, $filtered_metadata);

Assigning a default title based on the file name

This example assigns a default post_title based on the file name if none is supplied.

// Attachment metadata with file name
$attachment = array(
    'post_mime_type' => 'image/jpeg',
    'post_title' => '',
    'post_content' => '',
    'post_status' => 'inherit',
    'file' => 'path/to/image-file.jpg'
);

// Convert WP_Post object to an array
$post = (array) get_post($attachment_id);

// Use the function
$result = image_attachment_fields_to_save($post, $attachment);

Adding custom metadata to an attachment

This example adds custom metadata to an attachment using image_attachment_fields_to_save().

// Custom metadata
$custom_metadata = array(
    'custom_key' => 'Custom Value'
);

// Get attachment post data as an array
$post = (array) get_post($attachment_id);

// Filter and update metadata
$filtered_metadata = image_attachment_fields_to_save($post, $custom_metadata);
update_post_meta($attachment_id, 'custom_key', $filtered_metadata['custom_key']);

Saving attachment metadata during upload

This example saves attachment metadata during the upload process by hooking into the attachment_fields_to_save filter.

function my_custom_attachment_fields_to_save($post, $attachment) {
    // Add custom metadata
    $attachment['custom_key'] = 'Custom Value';

    // Use image_attachment_fields_to_save() to filter metadata
    return image_attachment_fields_to_save($post, $attachment);
}
add_filter('attachment_fields_to_save', 'my_custom_attachment_fields_to_save', 10, 2);