wp_copy_parent_attachment_properties()
云策文档标注
概述
wp_copy_parent_attachment_properties() 函数用于将父附件(原始图像)的属性复制到新裁剪的图像附件中,生成一个包含标题、内容、MIME 类型等属性的数组,便于创建新附件记录。
关键要点
- 函数接受三个参数:$cropped(裁剪后图像文件路径)、$parent_attachment_id(父附件 ID)和 $context(调用上下文)。
- 返回一个数组,包含新附件的属性,如 post_title、post_content、post_mime_type、guid 等。
- 自动继承父附件的标题、描述、说明(caption)和 alt 文本(如果存在),否则使用默认值。
- 使用 wp_getimagesize() 检测图像 MIME 类型,并基于父附件 URL 生成新附件的 URL。
- 函数内部调用多个 WordPress 核心函数,如 get_post()、wp_get_attachment_url()、sanitize_file_name() 等。
代码示例
function wp_copy_parent_attachment_properties( $cropped, $parent_attachment_id, $context = '' ) {
$parent = get_post( $parent_attachment_id );
$parent_url = wp_get_attachment_url( $parent->ID );
$parent_basename = wp_basename( $parent_url );
$url = str_replace( wp_basename( $parent_url ), wp_basename( $cropped ), $parent_url );
$size = wp_getimagesize( $cropped );
$image_type = $size ? $size['mime'] : 'image/jpeg';
$sanitized_post_title = sanitize_file_name( $parent->post_title );
$use_original_title = (
( '' !== trim( $parent->post_title ) ) &&
( $parent_basename !== $sanitized_post_title ) &&
( pathinfo( $parent_basename, PATHINFO_FILENAME ) !== $sanitized_post_title )
);
$use_original_description = ( '' !== trim( $parent->post_content ) );
$attachment = array(
'post_title' => $use_original_title ? $parent->post_title : wp_basename( $cropped ),
'post_content' => $use_original_description ? $parent->post_content : $url,
'post_mime_type' => $image_type,
'guid' => $url,
'context' => $context,
);
if ( '' !== trim( $parent->post_excerpt ) ) {
$attachment['post_excerpt'] = $parent->post_excerpt;
}
if ( '' !== trim( $parent->_wp_attachment_image_alt ) ) {
$attachment['meta_input'] = array(
'_wp_attachment_image_alt' => wp_slash( $parent->_wp_attachment_image_alt ),
);
}
$attachment['post_parent'] = $parent_attachment_id;
return $attachment;
}注意事项
- 函数在 WordPress 6.5.0 版本中引入,主要用于图像裁剪场景,如 AJAX 裁剪或自定义头部图像处理。
- 确保 $parent_attachment_id 对应有效的附件帖子,否则 get_post() 可能返回错误。
- 标题继承逻辑基于父附件标题是否与文件名不同,以避免使用默认文件名作为标题。
- 返回的数组可直接用于 wp_insert_attachment() 或其他附件创建函数。
原文内容
Copy parent attachment properties to newly cropped image.
Parameters
$croppedstringrequired-
Path to the cropped image file.
$parent_attachment_idintrequired-
Parent file Attachment ID.
$contextstringrequired-
Control calling the function.
Source
function wp_copy_parent_attachment_properties( $cropped, $parent_attachment_id, $context = '' ) {
$parent = get_post( $parent_attachment_id );
$parent_url = wp_get_attachment_url( $parent->ID );
$parent_basename = wp_basename( $parent_url );
$url = str_replace( wp_basename( $parent_url ), wp_basename( $cropped ), $parent_url );
$size = wp_getimagesize( $cropped );
$image_type = $size ? $size['mime'] : 'image/jpeg';
$sanitized_post_title = sanitize_file_name( $parent->post_title );
$use_original_title = (
( '' !== trim( $parent->post_title ) ) &&
/*
* Check if the original image has a title other than the "filename" default,
* meaning the image had a title when originally uploaded or its title was edited.
*/
( $parent_basename !== $sanitized_post_title ) &&
( pathinfo( $parent_basename, PATHINFO_FILENAME ) !== $sanitized_post_title )
);
$use_original_description = ( '' !== trim( $parent->post_content ) );
$attachment = array(
'post_title' => $use_original_title ? $parent->post_title : wp_basename( $cropped ),
'post_content' => $use_original_description ? $parent->post_content : $url,
'post_mime_type' => $image_type,
'guid' => $url,
'context' => $context,
);
// Copy the image caption attribute (post_excerpt field) from the original image.
if ( '' !== trim( $parent->post_excerpt ) ) {
$attachment['post_excerpt'] = $parent->post_excerpt;
}
// Copy the image alt text attribute from the original image.
if ( '' !== trim( $parent->_wp_attachment_image_alt ) ) {
$attachment['meta_input'] = array(
'_wp_attachment_image_alt' => wp_slash( $parent->_wp_attachment_image_alt ),
);
}
$attachment['post_parent'] = $parent_attachment_id;
return $attachment;
}
Changelog
| Version | Description |
|---|---|
| 6.5.0 | Introduced. |