如何在 Wordpress 中验证后保存自定义用户字段数据

How to save custom user fields data after validation in Wordpress

我正在尝试使用 user_profile_update_errors 挂钩验证自定义字段 phoneedit_user_profile_updatepersonal_options_update 运行 如此处所述 https://codex.wordpress.org/Plugin_API/Action_Reference/user_profile_update_errors 我尝试通过仅在 $errors 为空但函数不挂钩

时挂钩 save_profile_fields 来使用解决方法
function init() {
    //render profile fields
    add_action( 'show_user_profile', array( $this, 'render_profile_fields' ) );
    add_action( 'edit_user_profile', array( $this, 'render_profile_fields' ) );
    // validate profile fields
    add_action( 'user_profile_update_errors', array( $this, 'validate_profile_phone' ), 0, 3 );
    add_action( 'user_profile_update_errors', array( $this, 'validate_profile_address' ), 0, 3 );

}

function render_profile_fields( $user ) { ?>

    <h3>More Information</h3>
    <table class="form-table">
        <tr>
            <th><label for="phone">Phone</label></th>
            <td>
                <input type="tel" name="phone" id="phone" value="<?php echo esc_attr( get_the_author_meta( 'phone', $user->ID ) ); ?>" class="regular-text" ><br>
            </td>
        </tr>            
    </table>

<?php }

function validate_profile_phone( $errors, $update, $user ) {
    $error_check = false;
    $phone_regex = "/08[789]\d{7}/u";
// validate input fields
    if ( !empty( $_POST['phone'] ) && strlen( $_POST['phone'] ) > 10) {
        $errors->add( 'phone', "<strong>ERROR</strong>: The maximum phone length is 10 characters." );
        $error_check = true;
    }

    if ( preg_match( $phone_regex, $_POST['phone'] ) == 0 ) {
        $errors->add( 'phone', "<strong>ERROR</strong>: Not a valid phone number." );
        $error_check = true;
    }
    
    if( $error_check ) {
        return $errors;
    }

    add_action( 'edit_user_profile_update', array( $this, 'save_profile_fields' ) );
    add_action( 'personal_options_update', array( $this, 'save_profile_fields' ) );

}

function save_profile_fields( $id ) {
//  save input fields values
    if ( !current_user_can( 'edit_user', $id ) )
        return false;

    if ( isset( $_POST['phone'] ) )
        update_user_meta( $id, 'phone', $_POST['phone'] );
}

user_profile_update_errors 挂钩在返回用户配置文件更新错误之前触发。您可以为您的 errors/validation 与其他操作创建单独的函数,您可以保存所有自定义字段(例如:phone、地址等)并验证所有自定义字段(例如:phone、地址等)在一个函数中。

// validate profile field
add_action( 'user_profile_update_errors', array( $this, 'validate_profile_fields' ), 0, 3 );

// save profile fields to your own profile and others.
add_action( 'edit_user_profile_update', array( $this, 'save_profile_fields' ) );
add_action( 'personal_options_update', array( $this, 'save_profile_fields' ) );
    
function validate_profile_fields( &$errors, $update, &$user ) {
    $phone_regex = "/08[789]\d{7}/u";
    
    if ( !empty( $_POST['phone'] ) && strlen( $_POST['phone'] ) > 10) {
      
        $errors->add( 'phone', "<strong>ERROR</strong>: The maximum phone length is 10 characters." );

    }
    if ( preg_match( $phone_regex, $_POST['phone'] ) == 0 ) {
        
        $errors->add( 'phone', "<strong>ERROR</strong>: Not a valid phone number." );
    }
    
    return $errors;

}

function save_profile_fields( $id ) {
    
    // save input fields values
    $phone_regex = "/08[789]\d{7}/u";
    
    if ( !current_user_can( 'edit_user', $id ) )
        return false;

    if ( isset( $_POST['phone'] )) {

        if ( preg_match( $phone_regex, $_POST['phone'] ) == 1 ) {
        update_user_meta( $id, 'phone', sanitize_text_field( $_POST['phone'] ) );
}

据我了解,您不必在保存函数中执行所有相同的验证,因为它们已在 validation/errors 函数中设置 - 所以如果它们都传入错误函数, 然后保存功能触发。

祝你好运,编码愉快。

此外,不要忘记为您的函数添加一些独特的前缀,以确保它们不会与其他 themes/plugins 中的其他函数冲突。您可以使用随机字符或插件的缩写,例如:function abc1234_save_profile_fields() 或您喜欢的任何内容。

如果我的回答满意,请标记为您的答案。