如何先强制字段验证,然后将其值保存在编辑配置文件中?

时间:2020-04-01 作者:Tereska

我想在编辑配置文件(wp admin)期间对图像进行验证。

我的验证代码是:

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;  
}
代码运行良好,直到我添加以下代码以保存值时为止:

    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.

有人能帮我吗?提前谢谢你。

1 个回复
SO网友:Tereska

很遗憾,我没有收到任何回复。我做的有点不同。。。这是可行的。

我只是把两者结合起来,我得到了一个很好的结果。

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;  
}
我希望这对将来的某个人有帮助。