首先edit_user_profile
和show_user_profile
动作挂钩不必保存图像,只需在那里添加一个字段即可。所以
function image_up_gall(){
?>
Select image to upload:
<input type="file" name="fileToUpload" id="fileToUpload">
<input type="submit" value="Upload Image" name="submit">
<?php
}
add_action(\'edit_user_profile\', \'image_up_gall\');
add_action(\'show_user_profile\', \'image_up_gall\');
这是因为WordPress已经有了自己的表单标签,只要确保它有
enctype="multipart/form-data"
第二步,使用personal_options_update
和edit_user_profile_update
您可以保存表单/上载imag,为此,请使用以下代码:
function save_profile_fields( $user_id ) {
$target_dir = "uploads/"; // I recommend to use wp_upload_dir() to get the correct path
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Check if image file is a actual image or fake image
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
// here the image is uploaded and we can save it to user profile with:
update_usermeta( $user_id, \'profile_pic\', $target_file );
}
}
add_action( \'personal_options_update\', \'save_profile_fields\' );
add_action( \'edit_user_profile_update\', \'save_profile_fields\' );
但我建议您使用WordPress默认媒体库来实现这一点,因为有很多代码,所以我最好给您一个指向教程的链接:
https://rudrastyh.com/wordpress/customizable-media-uploader.html