パスワードが正しく設定されていないことを除けば、ほぼ完璧に機能するフロントエンド登録フォームがあります。
私のfunctions.phpで私は持っています -
/**
* Registration form
*
*/
add_action('template_redirect', 'register_a_user');
function register_a_user(){
if(isset($_GET['do']) && $_GET['do'] == 'register'):
$errors = array();
if(empty($_POST['user']) || empty($_POST['email'])) $errors[] = 'provide a user and email';
if(!empty($_POST['spam'])) $errors[] = 'gtfo spammer';
if(!empty($_POST['pass1']) && !empty($_POST['pass2'])) $error[] = 'The passwords you entered do not match';
$account = esc_attr($_POST['account_type']);
$user_login = esc_attr($_POST['user']);
$user_email = esc_attr($_POST['email']);
$user_pass = esc_attr($_POST['pass1']);
$user_pass2 = esc_attr($_POST['pass2']);
require_once(ABSPATH.WPINC.'/registration.php');
$sanitized_user_login = sanitize_user($user_login);
$user_email = apply_filters('user_registration_email', $user_email);
if(!is_email($user_email)) $errors[] = 'invalid e-mail';
elseif(email_exists($user_email)) $errors[] = 'this email is already registered, bla bla...';
if(empty($sanitized_user_login) || !validate_username($user_login)) $errors[] = 'invalid user name';
elseif(username_exists($sanitized_user_login)) $errors[] = 'user name already exists';
if(empty($errors)):
if ( $_POST['pass1'] == $_POST['pass2'] ) {
$user_data = array (
'user_login' => $sanitized_user_login,
'user_password' => $user_pass,
'user_email' => $user_email,
'role' => $account
);
// Create the user
$user_id = wp_insert_user( $user_data );
} else {
$errors[] = 'passwords dont match';
}
if(!$user_id):
$errors[] = 'registration failed...';
else:
wp_new_user_notification($user_id);
endif;
endif;
if(!empty($errors)) define('REGISTRATION_ERROR', serialize($errors));
else define('REGISTERED_A_USER', $user_email);
endif;
}
そして私の登録ページのテンプレートに -
<?php
if(defined('REGISTRATION_ERROR'))
foreach(unserialize(REGISTRATION_ERROR) as $error)
echo "<div class=\"error\">{$error}</div>";
// errors here, if any
elseif(defined('REGISTERED_A_USER'))
echo 'a email has been sent to '.REGISTERED_A_USER;
?>
<h2>REGISTER</h2>
<!-- REGISTER FORM -->
<form method="post" id="register-form" action="<?php echo add_query_arg('do', 'register'); ?>">
<!-- CHOOSE ACCOUNT TYPE AKA USER ROLE -->
<h4>Choose Account Type</h4>
<select name="account_type" class="form-control">
<option value="agent">Agent</option>
<option value="broker">Broker</option>
</select>
<!-- ENTER ACCOUNT DETAILS -->
<h4>Account Details</h4>
<input type="text" name="email" class="form-control" value="" placeholder="Email" />
<input type="text" name="user" class="form-control" value="" placeholder="Username"/>
<input type="text" name="pass1" class="form-control" value="" placeholder="Password" />
<input type="text" name="pass2" class="form-control" value="" placeholder="Confirm Password" />
Delete this text
<input type="text" name="spam" class="form-control" value="spam_protection" />
<!-- REGISTER BUTTON -->
<input type="submit" value="REGISTER" />
</form>
$ user_dataには、パスワード用の配列キーが正しくありません。user_passwordではなく、user_passにする必要があります。詳しくは wp_insert_user() をチェックしてください。