AWA*_*AWA 3 php wordpress registration
这是一个非常简短的前端注册指南,但我在密码方面遇到了一个小问题。
我禁用了用户注册时发送的带有密码生成的电子邮件:
//Don't Send Notification Email To Registered User
if (!function_exists('wp_new_user_notification')) :
function wp_new_user_notification( $user_id, $notify = '' ) {
//Here's originally password generation + sending email
//Add greeting email later
}
endif;
Run Code Online (Sandbox Code Playgroud)
我的前端注册表(不要介意缺少“重复密码” - 仅用于测试):
<?php if( get_option('users_can_register') ) { ?>
<form name="registerform" id="registerform" action="<?php echo wp_registration_url(); ?>" method="post">
<div class="form-group">
<input type="text" name="user_login" id="user_login" class="form-control" placeholder="Username">
</div>
<div class="form-group">
<input type="text" name="user_email" id="user_email" class="form-control" placeholder="Email">
</div>
<div class="form-group">
<input type="password" name="user_pass" id="user_pass" class="form-control" placeholder="Password">
</div>
<input type="hidden" name="redirect_to" value="<?php echo site_url(); ?>?user-register=registered">
<input type="submit" name="wp-submit-registration" id="wp-submit-registration" value="<?php _e( 'Register', 'tt' ); ?>">
</form>
<?php } ?>
Run Code Online (Sandbox Code Playgroud)
问题:为什么它不保存用户注册时的密码?
如果有人提供了如何添加 WP 密码强度指示器的链接,我也会表示感谢,因为我没有找到任何东西。
无论是否有密码,您的代码根本不应该工作。首先,wp_registration_url返回默认注册表单的 url。admin-post.php对于自定义注册表单,您可以使用自定义操作名称提交表单,例如register_user:
<form name="registerform" id="registerform" action="<?php echo admin_url('admin-post.php?action=register_user'); ?>" method="post">
Run Code Online (Sandbox Code Playgroud)
为了安全起见,我强烈建议您在表单中添加此内容(它将生成一个带有令牌的隐藏输入,以检查用户是否已启动该操作:
wp_nonce_field('create-'.$_SERVER['REMOTE_ADDR'], 'user-front', false);
Run Code Online (Sandbox Code Playgroud)
然后在你的functions.php文件中你用admin_post_nopriv_register_user.
add_action('admin_post_nopriv_register_user', 'my_register_user');
function my_register_user() {
// Check the form validity
if (isset($_POST['user-front']) && wp_verify_nonce($_POST['user-front'], 'create-'.$_SERVER['REMOTE_ADDR'])) {
// Check the required field
if (!isset($_POST['user_login']) && !isset($_POST['user_email']) || !isset($_POST['user_pass']) || !isset($_POST['user_confirm_pass']) || !is_email($_POST['user_email'])
) {
wp_redirect(home_url() . '?message=wrong-input');
exit();
}
// Check if both password match
if ($_POST['user_pass'] != $_POST['user_confirm_pass']) {
wp_redirect(home_url() . '?message=pass-dif');
exit();
}
// Check if user exists
if (email_exists($_POST['user_email']) |- username_exists($_POST['user_login']) {
wp_redirect(home_url() . '?message=already-registered');
exit();
}
// Create the user
$user_id = wp_create_user($_POST['user_login'], $_POST['user_pass'], $_POST['email_user']);
$user = new WP_User($user_id);
$user->set_role('subscriber');
// Automatic loggin
$creds = array();
$creds['user_login'] = $_POST['user_login'];
$creds['user_password'] = $_POST['user_pass'];
$creds['remember'] = false;
$user = wp_signon($creds, false);
// Redirection
wp_redirect(home_url('account'));
exit();
}
}
Run Code Online (Sandbox Code Playgroud)
这是注册过程的完整示例,您可能需要根据您的需要(特别是如何处理错误)对此进行一些更改。