admin管理员组文章数量:1391976
I want to make validation of image during edit profile (wp-admin).
My code for validation is:
add_action( 'user_profile_update_errors', 'validate_steamid_field' , 10, 3);
function validate_steamid_field(&$validation_errors, $update = null, &$user = null){
if ( isset( $_FILES['custom_avatar'] ) && !empty($_FILES['custom_avatar']) && file_exists($_FILES['custom_avatar']['tmp_name']) ) {
$allowed_image_extension = array(
"png",
"jpg",
"jpeg"
);
$file_extension = substr($_FILES['custom_avatar']["name"], strrpos($_FILES['custom_avatar']["name"], '.') + 1);
if (! in_array(strtolower($file_extension), $allowed_image_extension)) {
$_FILES['custom_avatar'] == '';
$validation_errors->add( 'error', 'wrong format image');
}
$fileinfo = @getimagesize($_FILES["custom_avatar"]["tmp_name"]);
$width = $fileinfo[0];
$height = $fileinfo[1];
if($width < "450" || $height < "450"){
$_FILES['custom_avatar'] == '';
$validation_errors->add( 'error', 'image is too small');
}
}
return $validation_errors;
}
The code works well till moment I add the following code for save the value:
function save_custom_avatar_field( $user_id ) {
if ( current_user_can( 'edit_user', $user_id ) ) {
if ( isset( $_FILES['custom_avatar'] ) && !empty($_FILES['custom_avatar']) ) {
update_user_meta($user_id,'custom_avatar', $_FILES['custom_avatar']['name']);
}
}
}
add_action( 'personal_options_update', 'save_custom_avatar_field' );
add_action( 'edit_user_profile_update', 'save_custom_avatar_field' );
The problem is that even the file is wrong format (validation not passed) the field is updated.
Could somone help me with it? Thank you in advance.
I want to make validation of image during edit profile (wp-admin).
My code for validation is:
add_action( 'user_profile_update_errors', 'validate_steamid_field' , 10, 3);
function validate_steamid_field(&$validation_errors, $update = null, &$user = null){
if ( isset( $_FILES['custom_avatar'] ) && !empty($_FILES['custom_avatar']) && file_exists($_FILES['custom_avatar']['tmp_name']) ) {
$allowed_image_extension = array(
"png",
"jpg",
"jpeg"
);
$file_extension = substr($_FILES['custom_avatar']["name"], strrpos($_FILES['custom_avatar']["name"], '.') + 1);
if (! in_array(strtolower($file_extension), $allowed_image_extension)) {
$_FILES['custom_avatar'] == '';
$validation_errors->add( 'error', 'wrong format image');
}
$fileinfo = @getimagesize($_FILES["custom_avatar"]["tmp_name"]);
$width = $fileinfo[0];
$height = $fileinfo[1];
if($width < "450" || $height < "450"){
$_FILES['custom_avatar'] == '';
$validation_errors->add( 'error', 'image is too small');
}
}
return $validation_errors;
}
The code works well till moment I add the following code for save the value:
function save_custom_avatar_field( $user_id ) {
if ( current_user_can( 'edit_user', $user_id ) ) {
if ( isset( $_FILES['custom_avatar'] ) && !empty($_FILES['custom_avatar']) ) {
update_user_meta($user_id,'custom_avatar', $_FILES['custom_avatar']['name']);
}
}
}
add_action( 'personal_options_update', 'save_custom_avatar_field' );
add_action( 'edit_user_profile_update', 'save_custom_avatar_field' );
The problem is that even the file is wrong format (validation not passed) the field is updated.
Could somone help me with it? Thank you in advance.
Share Improve this question asked Apr 1, 2020 at 14:44 TereskaTereska 313 bronze badges1 Answer
Reset to default 0Unfortunately I didn't receive any reply. I did it a little different ... and it works.
I just combine them both and I have that result which works fine.
add_action( 'user_profile_update_errors', 'validate_steamid_field' , 10, 3);
function validate_steamid_field(&$validation_errors, $update = null, &$user){
if ( isset( $_FILES['custom_avatar'] ) && !empty($_FILES['custom_avatar']) && file_exists($_FILES['custom_avatar']['tmp_name']) ) { //we check if there file is updated
$file_extension = substr($_FILES['custom_avatar']["name"], strrpos($_FILES['custom_avatar']["name"], '.') + 1); //get a file extension from name
$user_id = $user->ID; //get user ID;
$allowed_image_extension = array(
"png",
"jpg",
"jpeg"
); //all allowed type files which we want accept in the form
$fileinfo = @getimagesize($_FILES["custom_avatar"]["tmp_name"]);
$width = $fileinfo[0]; //get width of uploaded image
$height = $fileinfo[1]; //get height of uploaded image
if (! in_array(strtolower($file_extension), $allowed_image_extension)) {
$_FILES['custom_avatar'] == '';
$validation_errors->add( 'error', 'wrong format image');
//validate if is wrong format image
} else ($width < "450" || $height < "450"){
$_FILES['custom_avatar'] == '';
$validation_errors->add( 'error', 'image is too small');
//validate if image is too small
} else {
// There is not any error so we can save them here
if ( current_user_can( 'edit_user', $user_id ) ) {
update_user_meta($user_id,'custom_avatar', $_FILES['custom_avatar']['name']);
}
}
return $validation_errors;
}
I hope it will help somone in the future.
本文标签: functionsHow to force field validation firstthen its values saved durning edit profile
版权声明:本文标题:functions - How to force field validation first, then its values saved durning edit profile? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744606703a2615399.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论