我正在尝试上载图像,图像应插入到新帖子中,但即使图像上载成功,我也无法在帖子编辑器中显示图像。这是我的上传按钮:
<input type="file" name="file" id="file">
这是我在php操作表单中添加的代码:
$target = "/www/opt/wp-content/uploads/user-uploads/";
if ($_FILES["file"]["error"] == UPLOAD_ERR_OK) {
$tmp_name = $_FILES["file"]["tmp_name"];
$name = $_FILES["file"]["name"];
move_uploaded_file($tmp_name, "$target/$name");
}
update_field($field_5559871286b9b, $_FILES, $new_id);
我做错了什么?
SO网友:clap
使用这个代码对我来说很好
/*
* Function to upload feature image for post on front end form
*/
function insert_attachment( $file_handler,$post_id,$setthumb=\'false\' ) {
if ( $_FILES[$file_handler][\'error\'] !== UPLOAD_ERR_OK ) {
return __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 );
//set post thumbnail if setthumb is 1
if ( $setthumb == 1 ) {
update_post_meta( $post_id, \'_thumbnail_id\', $attach_id );
}
return $attach_id;
}
“您将在中使用此按钮”
<input type="file" name="file" id="file">