您可以使用wp_generate_attachment_metadata
:
此功能生成图像附件的元数据。它还根据Settings_Media_Screen上定义的尺寸创建缩略图和图像附件的其他中间尺寸。
过滤器的第二个参数是附件ID,因此添加post meta应该不是问题:
add_filter( 'wp_generate_attachment_metadata', 'manipulate_metadata_wpse_91177', 10, 2 );
function manipulate_metadata_wpse_91177( $metadata, $attachment_id )
{
// var_dump( $metadata['image_meta'] );
// Credit is inside $metadata['image_meta']['credit']
return $metadata;
}
$metadata['image_meta']
使用FirePHP 检查的内容,结果如下:
aperture: 4
camera: "DMC-FZ100"
caption: ""
copyright: ""
created_timestamp: 1352136868
credit: ""
focal_length: "9.2"
iso: "100"
shutter_speed: "0.003125"
title: "Double Dip"
[更新]
完整测试,在“媒体库”屏幕(/wp-admin/upload.php
)中添加自定义列以显示帖子元信息:
add_filter( 'wp_generate_attachment_metadata', 'manipulate_metadata_wpse_91177', 10, 2 );
add_filter( 'manage_upload_columns', 'camera_info_column_wpse_91177' );
add_action( 'manage_media_custom_column', 'camera_info_display_wpse_91177', 10, 2 );
function manipulate_metadata_wpse_91177( $metadata, $attachment_id )
{
update_post_meta( $attachment_id, 'photo_title', $metadata['image_meta']['title'] );
update_post_meta( $attachment_id, 'photo_camera', $metadata['image_meta']['camera'] );
return $metadata;
}
function camera_info_column_wpse_91177( $columns )
{
$columns['cam_info'] = 'Camera Info';
return $columns;
}
function camera_info_display_wpse_91177( $column_name, $post_id )
{
if( 'cam_info' != $column_name || !wp_attachment_is_image( $post_id ) )
return;
$title = get_post_meta( $post_id, 'photo_title', true );
$camera = get_post_meta( $post_id, 'photo_camera', true );
$echo_title = $title ? 'Title: ' . $title . '<br />' : '';
$echo_camera = $camera ? 'Camera: ' . $camera : '';
echo $echo_title . $echo_camera;
}
常见问题与解答:根据帖子信息保存后创建新类别