wp_insert_attachment_datafilter-hookWP 3.9.0

Allows you to modify attachment (Media Library file) data before it is updated or added to the database.

Usage

add_filter( 'wp_insert_attachment_data', 'wp_kama_insert_attachment_data_filter', 10, 4 );

/**
 * Function for `wp_insert_attachment_data` filter-hook.
 * 
 * @param array $data                An array of slashed, sanitized, and processed attachment post data.
 * @param array $postarr             An array of slashed and sanitized attachment post data, but not processed.
 * @param array $unsanitized_postarr An array of slashed yet *unsanitized* and unprocessed attachment post data as originally passed to wp_insert_post().
 * @param bool  $update              Whether this is an existing attachment post being updated.
 *
 * @return array
 */
function wp_kama_insert_attachment_data_filter( $data, $postarr, $unsanitized_postarr, $update ){

	// filter...
	return $data;
}
$data(array)

Array of sanitized attachment data. Example data when adding a new image:

Array (
	[post_author] => 1
	[post_date] => 2019-05-27 17:11:32
	[post_date_gmt] => 2019-05-27 13:11:32
	[post_content] =>
	[post_content_filtered] =>
	[post_title] => w3hosK-d_yE
	[post_excerpt] =>
	[post_status] => inherit
	[post_type] => attachment
	[comment_status] => open
	[ping_status] => closed
	[post_password] =>
	[post_name] => w3hosk-d_ye
	[to_ping] =>
	[pinged] =>
	[post_modified] => 2019-05-27 17:11:32
	[post_modified_gmt] => 2019-05-27 13:11:32
	[post_parent] => 0
	[menu_order] => 0
	[post_mime_type] => image/jpeg
	[guid] => http://wp-test.com/wp-content/uploads/2019/05/w3hosK-d_yE-2.jpg
)
$postarr(array)

Array of slashed and serialized attachment data. Example data when adding a new image:

Array (
	[post_author] => 1
	[post_content] =>
	[post_content_filtered] =>
	[post_title] => w3hosK-d_yE
	[post_excerpt] =>
	[post_status] => draft
	[post_type] => attachment
	[comment_status] =>
	[ping_status] =>
	[post_password] =>
	[to_ping] =>
	[pinged] =>
	[post_parent] => 0
	[menu_order] => 0
	[guid] => http://wp-test.com/wp-content/uploads/2019/05/w3hosK-d_yE-2.jpg
	[import_id] => 0
	[context] =>
	[file] => F:\server\sites\wp-test.com/wp-content/uploads/2019/05/w3hosK-d_yE-2.jpg
	[post_mime_type] => image/jpeg
	[ID] => 0
	[filter] => db
)
$unsanitized_postarr(array) (WP 5.4.1)
Array of slashed but not yet serialized attachment data.
$update(true/false) (WP 6.0)
true when an existing attachment (post) is being updated; false when a new attachment is being added.

Examples

#1 Remove an image title when it is uploaded to the Media Library

add_filter( 'wp_insert_attachment_data', 'clear_image_title_when_loading', 10, 2 );

/**
 * Removes the image title when it is uploaded to the Media Library.
 *
 * @param array $data
 * @param array $postarr
 *
 * @return array
 */
function clear_image_title_when_loading( $data, $postarr ) {

	// Return the original data if this is not an image.
	if ( ! file_is_displayable_image( $postarr['file'] ) ) {
		return $data;
	}

	/*
	 * If there is no ID, this is a new attachment and the title must be cleared.
	 * If there is an ID, this is an update and the title must not be changed.
	 */
	if ( empty( $postarr['ID'] ) ) {
		$data['post_title'] = '';
	}

	return $data;
}

Changelog

Since 3.9.0 Introduced.
Since 5.4.1 The $unsanitized_postarr parameter was added.
Since 6.0.0 The $update parameter was added.

Where the hook is called

wp_insert_post()
wp_insert_attachment_data
wp-includes/post.php 4963
$data = apply_filters( 'wp_insert_attachment_data', $data, $postarr, $unsanitized_postarr, $update );

Where the hook is used in WordPress

Usage not found.