image_attachment_fields_to_save

Filters input from media_upload_form_handler() and assigns a default post_title from the file name if none supplied.

Description

(array) image_attachment_fields_to_save( (array) $post, (array) $attachment ); 

Illustrates the use of the filter which can be used to add default values to any field before saving to DB.

Returns (array)

Filtered attachment post object.

Parameters (2)

0. $post (array)
The WP_Post attachment object converted to an array.
1. $attachment (array)
An array of attachment metadata.

Usage

  1. if ( !function_exists( 'image_attachment_fields_to_save' ) ) { 
  2. require_once ABSPATH . '/wp-admin/includes/media.php'; 
  3.  
  4. // The WP_Post attachment object converted to an array. 
  5. $post = array(); 
  6.  
  7. // An array of attachment metadata. 
  8. $attachment = array(); 
  9.  
  10. // NOTICE! Understand what this does before running. 
  11. $result = image_attachment_fields_to_save($post, $attachment); 
  12.  

Defined (1)

The function is defined in the following location(s).

/wp-admin/includes/media.php  
  1. function image_attachment_fields_to_save( $post, $attachment ) { 
  2. if ( substr( $post['post_mime_type'], 0, 5 ) == 'image' ) { 
  3. if ( strlen( trim( $post['post_title'] ) ) == 0 ) { 
  4. $attachment_url = ( isset( $post['attachment_url'] ) ) ? $post['attachment_url'] : $post['guid']; 
  5. $post['post_title'] = preg_replace( '/\.\w+$/', '', wp_basename( $attachment_url ) ); 
  6. $post['errors']['post_title']['errors'][] = __( 'Empty Title filled from filename.' ); 
  7.  
  8. return $post;