大概十年前就造访过露兜博客了,那时候wordpress遇到搞不定的事,老是去他的博客上留言求助,和知更鸟WP大学一样,都是一些愿意帮助小白站长们的大神。近期又使用了ludou-custom-register这款wordpress注册免邮箱验证的插件,发现已经升级到4.0版本了,而且跟以前的版本不同,这个版本已经是一个独立的php文件,可以说非常之简洁了。由于个人对注册表单有不同的理解,于是对这款插件进行了小修改,让他更贴合我的网站需求。
首先要说一下ludou-custom-register.4.0这款插件,功能很强大,主要功能如下:
一,不需要验证邮箱,对于用户注册体验更友好(虽然会导致用户随便填写一个邮箱来注册)
二,用户注册时可以选择订阅者和投稿者,投稿者就意味着可以写文章,管理审核通过后能发表。
三,注册时即让用户填写好昵称,启用本插件可关闭用户修改个人资料的权限了。
四,支持验证码防止机器人恶意注册(不过使用时发现这功能有BUG不显示了,但不影响正常注册)
因为我只需要这个免邮箱验证和让用户填写昵称的功能(填写昵称后可配合代码,实现让站内注册用评论时显示昵称隐藏用户名,实现安全性的目的)。所以我需要对这款插件的代码进行修改。
原代码如下:
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; // 你可以在此修改发送给用户的注册通知Email $message = sprintf(__('Username: %s'), $user_login) . "\r\n"; $message .= sprintf(__('Password: %s'), $plaintext_pass) . "\r\n"; $message .= '登陆网址: ' . wp_login_url() . "\r\n"; // sprintf(__('[%s] Your username and password'), $blogname) 为邮件标题 wp_mail($user_email, sprintf(__('[%s] Your username and password'), $blogname), $message); } endif; /* 修改注册表单 */ function ludou_show_password_field() { // 生成token,防止跨站攻击 $token = md5(uniqid(rand(), true)); $_SESSION['ludou_register_584226_token'] = $token; define('LCR_PLUGIN_URL', plugin_dir_url( __FILE__ )); ?>
add('spam_detect', "错误:请勿恶意注册"); if (!isset($_POST['user_nick']) || trim($_POST['user_nick']) == '') $errors->add('user_nick', "错误:昵称必须填写"); if(strlen($_POST['user_pass']) < 6) $errors->add('password_length', "错误:密码长度至少6位"); elseif($_POST['user_pass'] != $_POST['user_pass2']) $errors->add('password_error', "错误:两次输入的密码必须一致"); if($_POST['user_role'] != 'contributor' && $_POST['user_role'] != 'subscriber') $errors->add('role_error', "错误:不存在的用户身份"); } /* 保存表单提交的数据 */ 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']; $userdata['nickname'] = str_replace(array('<','>','&','"','\'','#','^','*','_','+','$','?','!'), '', $_POST['user_nick']); $pattern = '/[一-龥]/u'; if(preg_match($pattern, $_POST['user_login'])) { $userdata['user_nicename'] = $user_id; } 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); } function ludou_register_change_translated_text( $translated_text, $untranslated_text, $domain ) { if ( $untranslated_text === 'A password will be e-mailed to you.' || $untranslated_text === 'Registration confirmation will be emailed to you.' ) return ''; else if ($untranslated_text === 'Registration complete. Please check your e-mail.' || $untranslated_text === 'Registration complete. Please check your email.') return '注册成功!'; else return $translated_text; } add_filter('gettext', 'ludou_register_change_translated_text', 20, 3); 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');
经过我的修改优化后,代码是这样的:
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; // 你可以在此修改发送给用户的注册通知Email $message = sprintf(__('Username: %s'), $user_login) . "\r\n"; $message .= sprintf(__('Password: %s'), $plaintext_pass) . "\r\n"; $message .= '登陆网址: ' . wp_login_url() . "\r\n"; // sprintf(__('[%s] Your username and password'), $blogname) 为邮件标题 wp_mail($user_email, sprintf(__('[%s] Your username and password'), $blogname), $message); } endif; /* 修改注册表单 */ function ludou_show_password_field() { // 生成token,防止跨站攻击 $token = md5(uniqid(rand(), true)); $_SESSION['ludou_register_584226_token'] = $token; define('LCR_PLUGIN_URL', plugin_dir_url( __FILE__ )); ?>
add('spam_detect', "错误:请勿恶意注册"); if (!isset($_POST['user_nick']) || trim($_POST['user_nick']) == '') $errors->add('user_nick', "错误:昵称必须填写"); if(strlen($_POST['user_pass']) < 6) $errors->add('password_length', "错误:密码长度至少6位"); elseif($_POST['user_pass'] != $_POST['user_pass2']) $errors->add('password_error', "错误:两次输入的密码必须一致"); } /* 保存表单提交的数据 */ 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']; $userdata['nickname'] = str_replace(array('<','>','&','"','\'','#','^','*','_','+','$','?','!'), '', $_POST['user_nick']); $pattern = '/[一-龥]/u'; if(preg_match($pattern, $_POST['user_login'])) { $userdata['user_nicename'] = $user_id; } 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); } function ludou_register_change_translated_text( $translated_text, $untranslated_text, $domain ) { if ( $untranslated_text === 'A password will be e-mailed to you.' || $untranslated_text === 'Registration confirmation will be emailed to you.' ) return ''; else if ($untranslated_text === 'Registration complete. Please check your e-mail.' || $untranslated_text === 'Registration complete. Please check your email.') return '注册成功!'; else return $translated_text; } add_filter('gettext', 'ludou_register_change_translated_text', 20, 3); 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');
以上代码的效果图是这样的:
如果觉得上面的代码刚好符合需求,可以在安装原4.0插件后对代码进行更换,更换时先禁用插件,修改好再启用,这样如果跟主题或其他插件有冲突也不会造成影响。或者直接复制代码转存为php文件,再打包成zip文件上传插件安装也是可以的。
暂无评论内容