wp_update_image_subsizes()
云策文档标注
概述
wp_update_image_subsizes() 函数用于检查并创建缺失的已注册图像子尺寸,同时更新图像元数据。它处理附件ID,返回更新后的元数据数组或WP_Error对象。
关键要点
- 函数参数:必需参数 $attachment_id(整数类型),表示图像附件的文章ID。
- 返回值:成功时返回更新后的图像元数据数组,如果图像元数据和附件文件均缺失则返回WP_Error对象。
- 核心逻辑:首先获取图像元数据和原始文件路径;如果元数据为空或非数组,则尝试创建所有子尺寸;否则检查缺失尺寸并创建它们。
- 相关函数:涉及 wp_get_attachment_metadata()、wp_create_image_subsizes()、wp_get_missing_image_subsizes() 等用于元数据操作和尺寸生成。
- 过滤器:应用 wp_generate_attachment_metadata 过滤器,允许在元数据生成时进行自定义。
- 更新操作:最终通过 wp_update_attachment_metadata() 保存更新后的元数据。
代码示例
function wp_update_image_subsizes( $attachment_id ) {
$image_meta = wp_get_attachment_metadata( $attachment_id );
$image_file = wp_get_original_image_path( $attachment_id );
if ( empty( $image_meta ) || ! is_array( $image_meta ) ) {
if ( ! empty( $image_file ) ) {
$image_meta = wp_create_image_subsizes( $image_file, $attachment_id );
} else {
return new WP_Error( 'invalid_attachment', __( 'The attached file cannot be found.' ) );
}
} else {
$missing_sizes = wp_get_missing_image_subsizes( $attachment_id );
if ( empty( $missing_sizes ) ) {
return $image_meta;
}
$image_meta = _wp_make_subsizes( $missing_sizes, $image_file, $image_meta, $attachment_id );
}
$image_meta = apply_filters( 'wp_generate_attachment_metadata', $image_meta, $attachment_id, 'update' );
wp_update_attachment_metadata( $attachment_id, $image_meta );
return $image_meta;
}注意事项
- 函数在WordPress 5.3.0版本中引入,用于处理图像子尺寸的缺失问题。
- 如果附件文件丢失,函数会返回WP_Error,提示“The attached file cannot be found.”。
- 使用 wp_generate_attachment_metadata 过滤器可以在元数据更新前后进行干预。
- 相关函数如 wp_get_missing_image_subsizes() 和 _wp_make_subsizes() 是内部函数,通常通过此函数间接调用。
原文内容
If any of the currently registered image sub-sizes are missing, create them and update the image meta data.
Parameters
$attachment_idintrequired-
The image attachment post ID.
Source
function wp_update_image_subsizes( $attachment_id ) {
$image_meta = wp_get_attachment_metadata( $attachment_id );
$image_file = wp_get_original_image_path( $attachment_id );
if ( empty( $image_meta ) || ! is_array( $image_meta ) ) {
/*
* Previously failed upload?
* If there is an uploaded file, make all sub-sizes and generate all of the attachment meta.
*/
if ( ! empty( $image_file ) ) {
$image_meta = wp_create_image_subsizes( $image_file, $attachment_id );
} else {
return new WP_Error( 'invalid_attachment', __( 'The attached file cannot be found.' ) );
}
} else {
$missing_sizes = wp_get_missing_image_subsizes( $attachment_id );
if ( empty( $missing_sizes ) ) {
return $image_meta;
}
// This also updates the image meta.
$image_meta = _wp_make_subsizes( $missing_sizes, $image_file, $image_meta, $attachment_id );
}
/** This filter is documented in wp-admin/includes/image.php */
$image_meta = apply_filters( 'wp_generate_attachment_metadata', $image_meta, $attachment_id, 'update' );
// Save the updated metadata.
wp_update_attachment_metadata( $attachment_id, $image_meta );
return $image_meta;
}
Hooks
- apply_filters( ‘wp_generate_attachment_metadata’, array $metadata, int $attachment_id, string $context )
-
Filters the generated attachment meta data.
Changelog
| Version | Description |
|---|---|
| 5.3.0 | Introduced. |