added_post_meta
看来是时候换个新形象了。不仅已经设置了默认元,而且该函数还为您提供$post_id
随着$meta_value
它保存附件元数据。从那里,您可以获取所有字段并设置所需的字段。
add_action(\'added_post_meta\', \'wpse_20151219_after_post_meta\', 10, 4);
function wpse_20151219_after_post_meta($meta_id, $post_id, $meta_key, $meta_value) {
// _wp_attachment_metadata added
if($meta_key === \'_wp_attachment_metadata\') {
// ----------------------------------------------------------------------
// POST
// ----------------------------------------------------------------------
// Change basic fields on attachment post
wp_update_post(array(
\'ID\' => $post_id,
\'post_title\' => "This is a TITLE for $post_id",
\'post_content\' => "This is the DESCRIPTION for $post_id",
\'post_excerpt\' => "This is the CAPTION for $post_id",
));
// ----------------------------------------------------------------------
// POST META
// ----------------------------------------------------------------------
// Change ALT Text
update_post_meta($post_id, \'_wp_attachment_image_alt\', "This is the ALT Text for $post_id");
// Add Custom Field
update_post_meta($post_id, \'_wpse_20121219_my_custom_meta\', \'MyCustomMetaValue\');
// ----------------------------------------------------------------------
// POST META ( ATTACHMENT METADATA )
// ----------------------------------------------------------------------
// Change Image Metadata
$meta_value[ \'image_meta\' ] = array_merge($meta_value[ \'image_meta\' ], array(
\'credit\' => \'https://black-buddha.com\',
\'camera\' => \'The Best Camera EVER!\',
\'copyright\' => date(\'Y\'),
\'title\' => "This is a META TITLE for $post_id",
\'caption\' => "This is a META CAPTION for $post_id",
));
// Update The Image Metadata
wp_update_attachment_metadata($post_id, $meta_value);
// _wp_attached_file
// _wp_attachment_metadata (serialized)
// _wp_attachment_image_alt
// _wpse_20121219_my_custom_meta
$attachment_meta = get_post_meta($post_id);
// width
// height
// file
// sizes
// image_meta
// aperture
// credit
// camera
// caption
// created_timestamp
// copyright
// focal_length
// iso
// shutter_speed
// title
// orientation
// title
// keywords
$attachment_metadata = wp_get_attachment_metadata($post_id);
}
}