wordpress 自定义注册界面
程序员文章站
2022-04-29 23:42:03
...
首先在functions.php中建立如下函数
/** * start of custom registration module fucntion * **/ if (!isset($_SESSION)) { session_start(); session_regenerate_id(TRUE); } /** *Admin registration module,to add a registration form and to update the notification to the users */ if ( !function_exists('wp_new_user_notification') ) : /** * Notify the blog admin of a new user, normally via email. * * @since 2.0 * * @param int $user_id User ID * @param string $plaintext_pass Optional. The user's plaintext password */ function wp_new_user_notification($user_id, $plaintext_pass = '', $flag='') { if(func_num_args() > 1 && $flag !== 1) return; $user = new WP_User($user_id); $user_login = stripslashes($user->user_login); $user_email = stripslashes($user->user_email); // The blogname option is escaped with esc_html on the way into the database in sanitize_option // we want to reverse this for the plain text arena of emails. $blogname = wp_specialchars_decode(get_option('blogname'), ENT_QUOTES); $message = sprintf(__('New user registration on your site %s:'), $blogname) . "\r\n\r\n"; $message .= sprintf(__('Username: %s'), $user_login) . "\r\n\r\n"; $message .= sprintf(__('E-mail: %s'), $user_email) . "\r\n"; @wp_mail(get_option('admin_email'), sprintf(__('[%s] New User Registration'), $blogname), $message); if ( empty($plaintext_pass) ) return; // you can change the format in the Email $message = sprintf(__('Username: %s'), $user_login) . "\r\n"; $message .= sprintf(__('Password: %s'), $plaintext_pass) . "\r\n"; $message .= 'The login url is : ' . wp_login_url() . "\r\n"; // sprintf(__('[%s] Your username and password'), $blogname) is the email subject wp_mail($user_email, sprintf(__('[%s] Your username and password'), $blogname), $message); } endif; /* Update the registration form */ function ludou_show_password_field() { define('LCR_PLUGIN_URL', plugin_dir_url( __FILE__ )); ?> <p> <label>Password(at least 6 characters )<br/> <input id="user_pwd1" class="input" type="password" tabindex="31" size="20" value="<?php echo $_POST['user_pass']; ?>" name="user_pass"/> </label> </p> <p> <label>Confirm Password<br/> <input id="user_pwd2" class="input" type="password" tabindex="32" size="20" value="<?php echo $_POST['user_pass2']; ?>" name="user_pass2" /> </label> </p> <p style="margin:0 0 10px;"> <label>Choose your role: <select name="user_role" tabindex="33" id="user_role"> <option value="">Select your role</option> <option value="year7" <?php if($_POST['user_role'] == 'year7') echo 'selected="selected"';?>>Year 7</option> <option value="year8" <?php if($_POST['user_role'] == 'year8') echo 'selected="selected"';?>>Year 8</option> <option value="year9" <?php if($_POST['user_role'] == 'year9') echo 'selected="selected"';?>>Year 9</option> <option value="year10" <?php if($_POST['user_role'] == 'year10') echo 'selected="selected"';?>>Year 10</option> <option value="year11" <?php if($_POST['user_role'] == 'year11') echo 'selected="selected"';?>>Year 11</option> <option value="year12" <?php if($_POST['user_role'] == 'year12') echo 'selected="selected"';?>>Year 12</option> </select> </label> <br /> </p> <?php } /* to handle with the submitted data */ function ludou_check_fields($login, $email, $errors) { if(strlen($_POST['user_pass']) < 6) $errors->add('password_length', "<strong>ERROR</strong>: Password should be at least 6 characters"); elseif($_POST['user_pass'] != $_POST['user_pass2']) $errors->add('password_error', "<strong>ERROR</strong>: Please check your confirm password"); if($_POST['user_role'] != 'year7' && $_POST['user_role'] != 'year8' && $_POST['user_role'] != 'year9' && $_POST['user_role'] != 'year10' && $_POST['user_role'] != 'year11' && $_POST['user_role'] != 'year12') $errors->add('role_error', "<strong>ERROR</strong>: Please choose one correct role"); } /* TO save the submitted data */ function ludou_register_extra_fields($user_id, $password="", $meta=array()) { $userdata = array(); $userdata['ID'] = $user_id; $userdata['user_pass'] = $_POST['user_pass']; $userdata['role'] = $_POST['user_role']; wp_new_user_notification( $user_id, $_POST['user_pass'], 1 ); wp_update_user($userdata); } function remove_default_password_nag() { global $user_ID; delete_user_setting('default_password_nag', $user_ID); update_user_option($user_ID, 'default_password_nag', false, true); } add_action('admin_init', 'remove_default_password_nag'); add_action('register_form','ludou_show_password_field'); add_action('register_post','ludou_check_fields',10,3); add_action('user_register', 'ludou_register_extra_fields'); //end of custom registration module fucntion
然后下载如下插件
SI CAPTCHA Anti-Spam
设置生效后 就可以得到你想要的结果了