很遗憾,无法添加“上传图片”来注册WordPress网站。在初始注册期间,WordPress无法保存该头像,因为没有用户ID。只有在用户确认电子邮件地址后,WordPress才能为该用户创建用户ID。
这是我在网上发现的其他人所做的:
使用以下代码将附件包括在wp媒体库中:
if ( $_FILES ) {
$files = $_FILES["file"];
foreach ($files['name'] as $key => $value) {
if ($files['name'][$key]) {
$file = array(
'name' => $files['name'][$key],
'type' => $files['type'][$key],
'tmp_name' => $files['tmp_name'][$key],
'error' => $files['error'][$key],
'size' => $files['size'][$key]
);
$_FILES = array ("file" => $file);
foreach ($_FILES as $file => $array) {
$newupload = frontend_handle_attachment( $file, $post_id );
}
}
}
}
哪个调用函数frontend_handle_attachment:
function frontend_handle_attachment($file_handler,$post_id) {
// 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 );
// Set featured image
set_post_thumbnail($post_id, $attach_id);
return $attach_id;
}
使用以下形式的简单输入即可正常工作:
<input type="file" name="file[]" multiple="multiple" />
注意:此代码用于从前端上传帖子中的图像。但不是注册表的解决方案。