×

自定义一个wordpress个人信息页面

优畅主题 Wordpress技术 2013-06-18 10:57 5,550 2 条评论

今天翻看订阅的feed中发觉有国外高手已经可以做到wordpress更新密码的方法了,这个方法可是我之前一直无法解决的难题。
很久之前,有个朋友想做一个页面用来显示一些基本的用户信息,而不是wordpress系统中默认的那个很烦琐的个人信息页面。

要做一个页面显示和可以修改个人信息不难,但是要可以让用户修改密码,这个就是一个难题了,我当时google了很久也没有找到好的解决方法,没有想到现在这里看到了一个可行的方案。

下面的代码是一个简单的例子,创建了一个非常简单的带密码修改功能的个人信息页面。

新建一个文件到主题文件夹下,命名为account.php,把下面的代码放到文件中。
新建一个页面,选用模板为account Center。

<?php 
/*
* Template Name: Account Center
*/
if( $_POST['submit_info'] )
{
 // Set all variables
 $first_name = $_POST['first_name'];
 $last_name = $_POST['last_name'];
 $email = $_POST['email'];
 $password = $_POST['password'];
 $password2 = $_POST['password2']; 
 // 检验所有的必要的字段
 // 使用php再次检验数据的安全性
 $errors = array();
 if( $first_name == '' or $last_name == '' )
 {
 $errors[] = 'Please fill in a valid first and last name';
 }
 if( $email == '' or ! is_email( $email ) )
 {
 $errors[] = 'Please fill in a valid email address';
 }
 
if( $current_user->user_email != $email && email_exists( $email ) )
{
 $errors[] = 'The email you supplied is already in use. Please try a different email address';
 }
 if( $password != '' and $password != $password2 )
 {
 $errors[] = 'Please make sure your passwords match';
 $password_update = false;
 }
 else
 {
 $password_update = true;
 }
 
 // 如果系统没有出错,执行下面的代码
 if( ! $errors )
 {
 // 首先更新用户个人信息
 $data = array(
 'ID' => $current_user->ID,
 'user_email' => $email,
 'first_name' => $first_name,
 'last_name' => $last_name
 );
 
 // 如果设置了新密码,更新密码
 if( $password_update ){
 $data['user_pass'] = $password;
 }
 wp_update_user( $data );
 
 }
}
get_header();
?>
<div class="pamary"> 
 <?php
 //检验当前用户是否已经登陆,如果没有则显示登陆框
 if(!is_user_logged_in()){
 wp_login_form();
 }else{
 global $current_user;
 get_currentuserinfo(); 
 ?>
 <form action="">
 <p><label for="Name">昵称:<input type="text" name="" id="" value="<?php echo $current_user->user_nicename ; ?>" /></label></p>
 <p><label for="">邮件:<input type="text" name="" id="" value="<?php echo $current_user->user_email; ?>" /></label></p>
 <p><label for="">First Name:<input type="text" name="first_name" id="" value="<?php echo $current_user->user_firstname ; ?>" /></label></p>
 <p><label for="">Last Name:<input type="text" name="last_name" id="" value="<?php echo $current_user->user_lastname ; ?>" /></label></p>
 <p><label for="">密码:<input type="password" name="" id="" value="" /></label></p>
 <p><label for="">密码:<input type="password" name="" id="" value="" /></label></p>
 <p><input type="submit" name="submit_info" value="确定" /></p>
 </form>
 <?php 
 }
 ?> 
</div>
<?php get_footer();  ?>

如果本文对你有帮助,你可以扫描右边的二维码打赏,谢谢支持
联系优畅:uctheme#qq.com (#改为@)
微信订阅号:优畅主题(uctheme)
官网淘宝店:http://uctheme.taobao.com
版权声明:版权归 优畅主题 所有,转载请注明出处!
转载请保留链接: https://www.uctheme.com/technical/287.html
谢谢支付宝打赏
谢谢微信打赏

品牌创立:2012-11-18优畅主题

优畅主题成立于2012年11月18日,专业Wordpress导购主题开发商,拥有多年Wordpress主题设计经验,专门为淘宝客和导购客站长量身打造高端赚钱模板,我们注重细节,有着严谨的开发态度,一切从客户角度出发,如果你也喜欢 WordPress导购主题,欢迎和我们一起交流!


1 + 8 = ?

  1. skating507
    沙发!
    skating507 2013-09-22 下午 3:17 [回复]

    学习了,非常好的教程!有没有收藏文章的教程和分类标签和可以订阅标签的教程。就像虎嗅网那样的!

    • 优畅主题
      优畅主题 2013-09-22 下午 8:19

      @skating507 看看优畅主题出品的两款主题,都是带会员中心收藏的

切换注册

登录

忘记密码 ?

您也可以使用第三方帐号快捷登录

切换登录

注册