WordPress添加前台注册功能的方法
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>本文实例讲述了WordPress添加前台注册功能的方法。分享给大家供大家参考。具体分析如下:</p>
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
WordPress属于个人博客型的开源系统,是不是具备前台注册功能的,下面我来介绍自己给WordPress增加一个会员注册功能,有需要的朋友可参考.</p>
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
一、添加注册表单</p>
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
1、首先在当前主题的目录下新建一个php文件,命名为reg-page.php,然后将page.php中的所有代码复制到reg-page.php中;</p>
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
2、删除reg-page.php开头的所有注释,即 /* 与 */,以及它们之间的所有内容;</p>
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
3、搜索:the_content,可以查找到类似代码<?php the_content(); ?>,将其替换成代码一(注意使用UTF-8编码保存)</p>
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
如果你在reg-page.php中找不到the_content,那么你可以查找:get_template_part,可找到类似代码:<?php get_template_part( 'content', 'page' ); ?>,将content-page.php中的所有代码替换这部分代码即可。再用下面的代码替换<?php the_content(); ?></p>
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
html代码如下:<br>
</p>
<div style='margin: 3px auto 0px; padding: 0px 3px; outline: none; line-height: 21.6px; clear: both; border-width: 1px; border-style: solid; border-color: rgb(0, 153, 204); background: rgb(246, 251, 255); overflow: hidden; font-family: tahoma, arial, "Microsoft YaHei";'>
<div style="margin: 0px; padding: 0px; outline: none; float: right; line-height: 25.2px; font-size: 14px;">
<span style="line-height: 25.2px; cursor: pointer;"><u>复制代码</u></span>
</div>
<p>
代码如下:</p>
</div>
<div id="phpcode4" style='margin: 0px auto 3px; padding: 0px 3px; outline: none; line-height: 25.2px; font-size: 14px; clear: both; border-right: 1px solid rgb(0, 153, 204); background: rgb(221, 237, 251); overflow: hidden; border-left: 1px solid rgb(0, 153, 204); word-break: break-all; border-bottom: 1px solid rgb(0, 153, 204); word-wrap: break-word; font-family: tahoma, arial, "Microsoft YaHei";'>
<?php the_content(); ?> <br>
<?php if(!emptyempty($error)) { <br>
echo '<p>'.$error.'</p>'; <br>
} <br>
if (!is_user_logged_in()) { ?> <br>
<form name="registerform" method="post" action="<?php echo $_SERVER["REQUEST_URI"]; ?>"> <br>
<p> <br>
<label for="user_login">用户名<br><br>
<input type="text" name="user_login" id="user_login" value="<?php if(!empty($sanitized_user_login)) echo $sanitized_user_login; ?>" size="20" /> <br>
</label> <br>
</p> <br>
<p> <br>
<label for="user_email">电子邮件<br><br>
<input type="text" name="user_email" id="user_email" value="<?php if(!empty($user_email)) echo $user_email; ?>" size="25" /> <br>
</label> <br>
</p> <br><br>
<p> <br>
<label for="user_pwd1">密码(至少6位)<br><br>
<input id="user_pwd1" type="password" tabindex="21" size="25" value="" name="user_pass" /> <br>
</label> <br>
</p> <br><br>
<p> <br>
<label for="user_pwd2">重复密码<br><br>
<input id="user_pwd2" type="password" tabindex="21" size="25" value="" name="user_pass2" /> <br>
</label> <br>
</p> <br><br>
<p> <br>
<input type="hidden" name="ludou_reg" value="ok" /> <br>
<input type="submit" name="wp-submit" id="wp-submit" value="注册" /> <br>
</p> <br>
</form> <br>
<?php<br>
} else { <br>
echo '<p>您已注册成功,并已登录!</p>'; <br>
}<br>
?></div>
<p>
<br style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'><span style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'>添加表单处理代码:在reg-page.php开头处中,将第一个 <?php 改成代码二(注意使用UTF-8编码保存)</span></p>
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
</p>
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
php代码如下:<br>
</p>
<div style='margin: 3px auto 0px; padding: 0px 3px; outline: none; line-height: 21.6px; clear: both; border-width: 1px; border-style: solid; border-color: rgb(0, 153, 204); background: rgb(246, 251, 255); overflow: hidden; font-family: tahoma, arial, "Microsoft YaHei";'>
<div style="margin: 0px; padding: 0px; outline: none; float: right; line-height: 25.2px; font-size: 14px;">
<span style="line-height: 25.2px; cursor: pointer;"><u>复制代码</u></span>
</div>
<p>
代码如下:</p>
</div>
<div id="phpcode5" style='margin: 0px auto 3px; padding: 0px 3px; outline: none; line-height: 25.2px; font-size: 14px; clear: both; border-right: 1px solid rgb(0, 153, 204); background: rgb(221, 237, 251); overflow: hidden; border-left: 1px solid rgb(0, 153, 204); word-break: break-all; border-bottom: 1px solid rgb(0, 153, 204); word-wrap: break-word; font-family: tahoma, arial, "Microsoft YaHei";'>
<?php <br>
if( !emptyempty($_POST['ludou_reg']) ) { <br>
$error = ''; <br>
$sanitized_user_login = sanitize_user( $_POST['user_login'] ); <br>
$user_email = apply_filters( 'user_registration_email', $_POST['user_email'] ); <br>
// Check the username <br>
if ( $sanitized_user_login == '' ) { <br>
$error .= '<strong>错误</strong>:请输入用户名。<br>
'; <br>
} elseif ( ! validate_username( $user_login ) ) { <br>
$error .= '<strong>错误</strong>:此用户名包含无效字符,请输入有效的用户名<br>
。'; <br>
$sanitized_user_login = ''; <br>
} elseif ( username_exists( $sanitized_user_login ) ) { <br>
$error .= '<strong>错误</strong>:该用户名已被注册,请再选择一个。<br>
'; <br>
} <br>
// Check the e-mail address <br>
if ( $user_email == '' ) { <br>
$error .= '<strong>错误</strong>:请填写电子邮件地址。<br>
'; <br>
} elseif ( ! is_email( $user_email ) ) { <br>
$error .= '<strong>错误</strong>:电子邮件地址不正确。!<br>
'; <br>
$user_email = ''; <br>
} elseif ( email_exists( $user_email ) ) { <br>
$error .= '<strong>错误</strong>:该电子邮件地址已经被注册,请换一个。<br>
'; <br>
} <br><br>
// Check the password <br>
if(strlen($_POST['user_pass']) < 6) <br>
$error .= '<strong>错误</strong>:密码长度至少6位!<br>
'; <br>
elseif($_POST['user_pass'] != $_POST['user_pass2']) <br>
$error .= '<strong>错误</strong>:两次输入的密码必须一致!<br>
'; <br><br>
if($error == '') { <br>
$user_id = wp_create_user( $sanitized_user_login, $_POST['user_pass'], $user_email ); <br><br>
if ( ! $user_id ) { <br>
$error .= sprintf( '<strong>错误</strong>:无法完成您的注册请求... 请联系<a href="mailto:%s">管理员</a>!<br>
', get_option( 'admin_email' ) ); <br>
} <br>
else if (!is_user_logged_in()) { <br>
$user = get_userdatabylogin($sanitized_user_login); <br>
$user_id = $user->ID; <br><br>
// 自动登录 <br>
wp_set_current_user($user_id, $user_login); <br>
wp_set_auth_cookie($user_id); <br>
do_action('wp_login', $user_login); <br>
}<br>
}<br>
}</div>
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
</p>
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
希望本文所述对大家WordPress建站有所帮助。</p>
頁:
[1]