Eine auf WordPress spezialiserte Suchmaschine für Entwickler und Theme-Autoren



image_attachment_fields_to_save ›

Seit2.5.0
Veraltetn/v
image_attachment_fields_to_save ( $post, $attachment )
Parameter: (2)
  • (array) $post The WP_Post attachment object converted to an array.
    Erforderlich: Ja
  • (array) $attachment An array of attachment metadata.
    Erforderlich: Ja
Gibt zurück:
  • (array) Filtered attachment post object.
Definiert in:
Codex:

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 Array filter which can be used to add default values to any field before saving to DB.


Quellcode

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