首先为新输入字段添加:
<form id="file-form" enctype="multipart/form-data" action="<?php echo $_SERVER[\'REQUEST_URI\']; ?>" method="POST">
<p id="async-upload-wrap">
<label for="async-upload">upload</label>
<input type="file" id="async-upload" name="async-upload"> <input type="submit" value="Upload" name="html-upload">
</p>
<p id="image_title">
<label for="image_title">Image Title</label>
<input type="text" id="image_title" name="image_title" value="">
</p>
<p>
<input type="hidden" name="post_id" id="post_id" value="<?php echo $post_id ?>" />
<?php wp_nonce_field(\'client-file-upload\'); ?>
<input type="hidden" name="redirect_to" value="<?php echo $_SERVER[\'REQUEST_URI\']; ?>" />
</p>
<p>
<input type="submit" value="Save all changes" name="save" style="display: none;">
</p>
</form>
然后,在保存图像并获取附件id后,可以更新附件元数据:
if ($_FILES) {
foreach ($_FILES as $file => $array) {
$newupload = insert_attachment($file,$post_id);
//get the meta data of the attachment
$meta = wp_get_attachment_metadata( $newupload);
//set the title
$meta[\'image_meta\'][\'title\'] = wp_kses($_POST[\'image_title\']);
//update the attachment with the new title.
wp_update_attachment_metadata($newupload,$meta);
}
}