我有一个脚本,允许我从前端将图像上传到 wordpress。然后我需要它将文件发布到 post_meta。现在它工作正常,但我最终得到了附件 ID,需要文件的链接。
这是处理此特定功能的代码。
if ($_FILES) {
foreach ($_FILES as $k => $v) {
if ($k != 'poster_has_paid' && $k != 'featured_image') {
if ($_FILES[$k]) {
wpo_poster_insert_attachment($k, $post_id, false, $k);
}
}
}
}
这是函数 wpo_poster_insert_attachment
function wpo_poster_insert_attachment($file_handler, $post_id, $setthumb = 'false', $post_meta = '') {
// check to make sure its a successful upload
if ($_FILES[$file_handler]['error'] !== UPLOAD_ERR_OK) {
__return_false();
}
require_once(ABSPATH . "wp-admin" . '/includes/image.php');
require_once(ABSPATH . "wp-admin" . '/includes/file.php');
require_once(ABSPATH . "wp-admin" . '/includes/media.php');
$attach_id = media_handle_upload($file_handler, $post_id);
if ($setthumb) {
update_post_meta($post_id, '_thumbnail_id', $attach_id);
}
if (!$setthumb && $post_meta != '') {
update_post_meta($post_id, $post_meta, $attach_id);
}
return $attach_id;
同样,它使用 attach_id 更新字段,我希望它更新 attach_url
PS 当我有足够的帖子时,我会感谢的。提前致谢。