Adding fields
首先,在注册公式中添加一些字段
add_action( \'register_form\', \'extended_register_form\', 10, 0 );
function extended_register_form() {
// if an error occurs, we are here again and the
// values that are entered in the formular are setup in the $_POST array
$age = filter_input( INPUT_POST, \'age\', FILTER_SANITIZE_NUMBER_INT );
echo \'<p>\';
echo \'<label for="age">Your age:& </label>\';
printf( \'<input type="text" id="age" name="age" size="2" value ="%s" />\', $age );
echo \'</p>\';
echo \'<p>\';
echo \'<input type="checkbox" id="tos_check" name="tos_check" />\';
echo \'<label for="tos_check"> I agree to the terms of service</label>\';
echo \'</p>\';
}
Verifying, validating & sanitazion
现在我们必须验证用户的输入。如果一个或多个值与我们的条件不匹配,我们希望抛出一条错误消息。要执行此操作,请钩住
registration_errors 并将一个或多个错误添加到错误对象。如果错误对象不为空,将停止注册并打印错误。
add_filter( \'registration_errors\', \'show_errors\', 10, 3 );
function show_errors( $errors, $login, $email ) {
$tos_check = ( isset( $_POST[\'tos_check\'] ) && true == $_POST[\'tos_check\'] ) ?
true : false;
if ( false == $tos_check )
$errors->add( \'termsnotaccepted\', \'You have to agree to the TOS\' );
$age_check = filter_input( INPUT_POST, \'age\', FILTER_SANITIZE_NUMBER_INT );
if ( $age_check < 21 )
$errors->add( \'toyoungerror\', \'Sorry, you are to young\' );
return $errors;
}
Storing the data
最后,钩住用户注册例程,检查是否设置了(已验证和已验证)值。如果这些值存在,请将它们存储在users元中。
add_action( \'user_register\', \'user_register\', 10, 1 );
function user_register( $user_id ) {
$data = array();
$age = filter_input( INPUT_POST, \'age\', FILTER_SANITIZE_NUMBER_INT );
if ( ! empty( $age ) )
$data[\'age\'] = $age;
update_user_meta( $user_id, \'choose-a-key\', $data );
return $user_id;
}
Summary
<将您的字段添加到注册公式中,以验证、验证和清理这些值。如果未设置必需值(或无效),请添加错误。这将停止注册过程并打印错误消息将所需的值存储在用户元中