过滤来自media_upload_form_handler()的输入,如果没有提供,则从文件名中分配默认的post_title。
原型
image_attachment_fields_to_save( array $post, array $attachment )
描述
说明’attachment_fields_to_save’过滤器的使用,该过滤器可用于在保存到DB之前将默认值添加到任何字段。
参数
$post
(array)
(Required)
WP_Post附件对象转换为数组。
$attachment
(array)
(Required)
一系列附件元数据。
返回值
(array)
过滤后的附件发布对象。
源文件
路径:wp-admin/includes/media.php
<?php
...
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;
}
...
?>
其他
英文文档:https://developer.wordpress.org/reference/functions/image_attachment_fields_to_save/