在Wordpress中通过自定义注册表单设置用户个人资料图片

时间:2018-09-15 08:06:01

标签: php mysql wordpress wordpress-theming

我希望用户在注册时上传图片,并且应该将这些图片设置为用户个人资料图片。

我有我的自定义注册表格。

在一个用于上传个人资料图像的字段中。

<div class="col-lg-4">
<div class="form-group">
<label>Upload your photo</label>
<input class="text-input" name="uploadphoto" type="file" id="uploadphoto" multiple="false"/>
</div>  
</div>

im通过以下代码将文件保存在后端介质中。这工作成功。

if ( 'POST' == $_SERVER['REQUEST_METHOD'] ) {
// These files need to be included as dependencies when on the front end.
require_once( ABSPATH . 'wp-admin/includes/image.php' );
require_once( ABSPATH . 'wp-admin/includes/file.php' );
require_once( ABSPATH . 'wp-admin/includes/media.php' );
// Let WordPress handle the upload.
$img_id = media_handle_upload( 'uploadphoto', $user_ID ); //getting image id here
// $image_attributes = wp_get_attachment_image_src( $attachment_id = $img_id );
//$img_url = $image_attributes[0]; //getting img url here
add_user_meta($user_ID, 'uploadphoto', $img_id, true);  //adding id as usermeta
//add_user_meta( $user_ID, 'uploadphoto', $img_url, true); //adding image path as usermeta
}

如果我将ID存储在数据库中作为usermeta,我的上载usermeta值出现以下错误

O:8:"WP_Error":2:{s:6:"errors";a:1:{s:12:"upload_error";a:1:{i:0;s:212:"File is empty. Please upload something more substantial. This error could also be caused by uploads being disabled in your php.ini or by post_max_size being defined as smaller than upload_max_filesize in php.ini.";}}s:10:"error_data";a:0:{}}

并且如果我将图像路径存储在usermeta uploadphoto中,那么它的值将显示为“ NULL”。

有人可以帮我吗?

0 个答案:

没有答案