Kriuchko
1/8/2019 - 2:12 PM

User usermeta custom registration form

Wordpress usermeta

в functions.php добавляем кастомное поле, сохраняем его значения и функция валидации и создания пользователя

add_action( 'edit_user_profile', 'show_user_additional_fields');
add_action( 'show_user_profile', 'show_user_additional_fields');

function show_user_additional_fields($user){
$user_phone = get_user_meta($user->ID, 'user_phone', true);
?>

<table class="form-table">
<tbody>
<tr>
<th><label for="description">Phone Number</label></th>
<td><input type="text" id="user_phone" name="user_phone" value="<?php echo $user_phone ?>" /></td>
</tr>
</tbody>
</table>
<?}

add_action( 'personal_options_update', 'save_user_additional_fields' );
add_action( 'edit_user_profile_update', 'save_user_additional_fields' );

function save_user_additional_fields( $user_id ) {

if ( !current_user_can( 'edit_user', $user_id ) )
return false;
update_usermeta( $user_id, 'user_phone', $_POST['user_phone'] );
}

function theme_register_new_user( $user_login, $user_email, $user_phone ) {
$errors = new WP_Error();
$user_pass = null;

$sanitized_user_login = sanitize_user( $user_login );
$user_email = apply_filters( 'user_registration_email', $user_email );

// Check the username
if ( $sanitized_user_login == '' ) {
$errors->add( 'empty_username', __( '<strong>ERROR</strong>: Please enter a username.' ) );
} elseif ( ! validate_username( $user_login ) ) {
$errors->add( 'invalid_username', __( '<strong>ERROR</strong>: This username is invalid because it uses illegal characters. Please enter a valid username.' ) );
$sanitized_user_login = '';
} elseif ( username_exists( $sanitized_user_login ) ) {
$errors->add( 'username_exists', __( '<strong>ERROR</strong>: This username is already registered. Please choose another one.' ) );
}

// Check the e-mail address
if ( $user_email == '' ) {
$errors->add( 'empty_email', __( '<strong>ERROR</strong>: Please type your e-mail address.' ) );
} elseif ( ! is_email( $user_email ) ) {
$errors->add( 'invalid_email', __( '<strong>ERROR</strong>: The email address isn&#8217;t correct.' ) );
$user_email = '';
} elseif ( email_exists( $user_email ) ) {
$errors->add( 'email_exists', __( '<strong>ERROR</strong>: This email is already registered, please choose another one.' ) );
}

do_action( 'register_post', $sanitized_user_login, $user_email, $errors );

$errors = apply_filters( 'registration_errors', $errors, $sanitized_user_login, $user_email );

if ( $errors->get_error_code() )
return $errors;

$user_pass = wp_generate_password( 12, false);
$user_id = wp_create_user( $sanitized_user_login, $user_pass, $user_email);
if ( ! $user_id ) {
$errors->add( 'registerfail', sprintf( __( '<strong>ERROR</strong>: Couldn&#8217;t register you... please contact the <a href="mailto:%s">webmaster</a> !' ), get_option( 'admin_email' ) ) );
return $errors;
}

update_user_option( $user_id, 'default_password_nag', true, true ); //Set up the Password change nag.

wp_new_user_notification( $user_id, $user_pass );

update_usermeta( $user_id, 'user_phone', $user_phone );

return $user_id;
}

--------- В шаблоне где надо вывести форму в самом верху до хедер это важно!

<?php
if($_POST['register']){
	$user_login = $_POST['user_login'];
	$user_email = $_POST['user_email'];
	$user_phone = $_POST['phone'];
	$errors = theme_register_new_user($user_login, $user_email, $user_phone);
	if ( !is_wp_error($errors) ) {
		$redirect_to = !empty( $_POST['redirect_to'] ) ? $_POST['redirect_to'] : 'wp-login.php?checkemail=registered';
		wp_safe_redirect( $redirect_to );
		exit();
	}
}
?>

------------ Далее выводим саму форму и ошибки валидации

<?php if(is_wp_error($errors)):?>
		<ul class="errors">
				<?php 
				foreach($errors->get_error_codes() as $code){
					foreach($errors->get_error_messages($code) as $error){
						echo '<li>'.$error.'</li>';
					}
				}
				?>
		</ul>
<?php endif; ?>
<div class="container-box">
		<div class="contact-block">
				<strong class="text">BECOME A <span>FREE MEMBER</span></strong>
				<form name="registerform" id="registerform" class="contact-form" action="<?php echo get_permalink(); ?>" method="post">
						<fieldset>
								<input type="text" name="user_login" id="user_login" value="Your Name" />
								<input type="text" name="phone" id="phone"  value="Phone Number" />
								<input type="text" name="user_email" id="user_email" value="Email Address" />
								<input type="hidden" name="redirect_to" value="<?php echo add_query_arg('action', 'success', get_page_link(ThankYouPage)); ?>" />
								<input type="hidden" name="register" value="1" />
								<input type="submit" name="wp-submit" value="Submit" />
								<span class="tel">214 692 5307</span>
								<span class="info">*password will be sent to this email</span>
						</fieldset>
				</form>
		</div>
</div>