不使用插件为wordpress添加用户中心功能

如果你在网络上查询如何为wordpress添加一个用户中心功能,那么得到的答案大部分是“去安装一个插件吧”这样敷衍的回答。在能用代码实现的方式下,尽量不使用插件,这是我做wordpress开发的核心思路。那么,如何在不使用插件的情况下为wordpress添加用户中心功能呢。我将撰写一个教程教大家实现。


先确定思路,用户中心分为两个大部分

  1. 用户信息
  2. 用户设置

第一部分我们来学习如何在前端实现用户信息功能。

用户中心功能基于wordpress的author模板实现,它为所有已注册用户生成一个作者页面(我们要制作的用户页),并且链接是根据与用户slug生成的。我们先新建一个author.php文件。

在模板文件的开始部分先定义当前用户页的用户变量$curauth

$curauth = (isset($_GET['author_name'])) ? get_user_by('slug', $author_name) : get_userdata(intval($author));

有了用户变量以后,我们可以获取该用户的一系列信息然后呈现出来。

获取用户ID

echo $curauth->ID;

获取用户头像

echo get_avatar( $curauth->user_email , '180');

获取用户的网站显示名

echo $curauth->display_name;

获取当前用户信息页面的链接

echo get_author_posts_url($current_user->ID);

我们也可以基于wp_usermeta这个数据表,提取其中的用户信息,不仅仅包括wordpress自带的usermeta信息,还可以提取我们自己添加的usermeta。
例如:

获取用户签名:(wordpress自带usermeta)

echo $curauth->description;

获取用户生日:(这里是我们自己添加的usermeta,在后面的用户设置页面会讲到如何添加自定义usermeta)

echo $curauth->birthday;

上面都是获取一些基础信息,那么我们用户中心还需要添加一个该用户的评论,我们也是可以获取并呈现的。具体方式这里就不详细撰写了,php长代码在wordpress文章中不好嵌入,会被转义。

对于用户信息我们的思路就是根据模板开始时定义的$curauth变量获取用户的一系列信息然后呈现出来,至于页面外观什么的可以自己去定义。


第二部分我们来学习如何实现用户设置功能

在学习这部分之前,我们需要给每个用户中心一个单独的用户设置链接。这需要用到wordpress的自定义author端点功能。
将以下代码添加到functions.php中

function wpa_author_endpoints(){
    add_rewrite_endpoint( 'settings-basic', EP_AUTHORS );
    add_rewrite_endpoint( 'settings-email', EP_AUTHORS );
    add_rewrite_endpoint( 'settings-password', EP_AUTHORS );
}
add_action( 'init', 'wpa_author_endpoints' );
function wpa_author_template( $template = '' ){
    global $wp_query;
    if( array_key_exists( 'settings-basic', $wp_query->query_vars ) )
        $template = locate_template( array( 'custom-part/author-endpoint/settings-basic.php', $template ), false );
    if( array_key_exists( 'settings-email', $wp_query->query_vars ) )
        $template = locate_template( array( 'custom-part/author-endpoint/settings-email.php', $template ), false );
    if( array_key_exists( 'settings-password', $wp_query->query_vars ) )
        $template = locate_template( array( 'custom-part/author-endpoint/settings-password.php', $template ), false );
    return $template;
}
add_filter( 'author_template', 'wpa_author_template' );

其中custom-part/author-endpoint/settings-basic.php这种路径是相对于主题根目录而言,具体应该是 主题目录/custom-part/author-endpoint/settings-basic.php 这样。你可以自己定义文件位置。

在上面函数定义的路径新建对应的几个PHP文件,这样我们就有了独立的基本设置邮件设置密码修改模板。

我们的用户设置功能有三个模块,对应了三个页面,那么我们需要一个Tabs标签,来进行选择。
在functions.php中添加一个功能,用于获取当前页面的链接

function curPageURL() 
{
  $pageURL = 'http';
  if ($_SERVER["HTTPS"] == "on") 
  {
    $pageURL .= "s";
  }
  $pageURL .= "://";
  $pageURL .= $_SERVER["SERVER_NAME"] . $_SERVER["REQUEST_URI"];
  return $pageURL;
}

新建一个settings-tabs.php,这是tabs模板。具体代码如下。

<?php
$current_url= curPageURL();
$c_url=get_author_posts_url($current_user->ID);
$basic = implode("",array($c_url,'settings-basic/'));
$email = implode("",array($c_url,'settings-email/'));
$password = implode("",array($c_url,'settings-password/'));
switch ($current_url) {
case $basic:
echo '<ul class="p-tabs-wrap">
		<li class="p-tab current-tab">
			<a href="javascript:void(0);">基本设置</a>
		</li>
		<li class="p-tab">
			<a href="'.$c_url.'settings-email/">邮箱设置</a>
		</li>
		<li class="p-tab">
			<a href="'.$c_url.'settings-password/">修改密码</a>
		</li>
		<li class="p-tab">
			<a href="'.$c_url.'settings-avatar/">修改头像</a>
		</li>
		<li class="p-tab">
			<a href="'.$c_url.'settings-social/">绑定社媒</a>
		</li>
	</ul>';
break;
case $email:
echo '<ul class="p-tabs-wrap">
		<li class="p-tab">
			<a href="'.$c_url.'settings-basic/">基本设置</a>
		</li>
		<li class="p-tab current-tab">
			<a href="javascript:void(0);">邮箱设置</a>
		</li>
		<li class="p-tab">
			<a href="'.$c_url.'settings-password/">修改密码</a>
		</li>
		<li class="p-tab">
			<a href="'.$c_url.'settings-avatar/">修改头像</a>
		</li>
		<li class="p-tab">
			<a href="'.$c_url.'settings-social/">绑定社媒</a>
		</li>
	</ul>';
break;
case $password:
echo '<ul class="p-tabs-wrap">
		<li class="p-tab">
			<a href="'.$c_url.'settings-basic/">基本设置</a>
		</li>
		<li class="p-tab">
			<a href="'.$c_url.'settings-email/">邮箱设置</a>
		</li>
		<li class="p-tab current-tab">
			<a href="javascript:void(0);">修改密码</a>
		</li>
		<li class="p-tab">
			<a href="'.$c_url.'settings-avatar/">修改头像</a>
		</li>
		<li class="p-tab">
			<a href="'.$c_url.'settings-social/">绑定社媒</a>
		</li>
	</ul>';
break;
 ?>

然后为settings-basic.php,settings-email.php,settings-password.php的模板文件都引入这个tabs模板:

<?php get_template_part( 'custom-part/author-endpoint/settings', 'tabs'); ?>

下一步我们来写基础设置模板(settings-basic.php)的代码

用户数据分为三种情况,一种是可以直接输入的字符串,一种是需要下拉选择的字符串,还有一种是单选按钮选择的字符串。我们为三种情况都做一个说明。

1.比如我现在需要设置用户的姓氏和名称,这是wordpress自带的usermeta,叫做last_name和first_name。它们是可以直接输入的字段,那么这个用户表单应该这样写。

<!-- Last Name --> 
<tr class="user-wrap user-last_name-wrap"> 
<th valign="top"><label for="last_name" maxlength="16">姓氏</label></th> 
<td valign="top"> <input type="text" name="last_name" id="last_name" value="<?php echo trim($current_user->last_name); ?>" > </td> 
</tr> <!-- First Name --> 
<tr class="user-wrap user-first_name-wrap"> 
<th valign="top">
<label for="first_name" maxlength="16">名字</label>
</th> 
<td valign="top"> <input type="text" name="first_name" id="first_name" value="<?php echo trim($current_user->first_name); ?>" > 
</td> 
</tr>

2. 第二种情况是select,现在我需要用户可以选择他们的职业类型,代码如下

<!-- career --> 
<tr class="user-wrap user-career-wrap"> 
<th valign="top">
<label for="career">职业</label>
</th> 
<td valign="top"> 
<select name="career" id="career"> 
<?php $careers= array( '计算机/互联网/通信', '生产/工艺/制造', '学生', '医疗/护理/制药', '金融/银行/投资/保险', '商业/服务业/个体经营', '文化/广告/传媒', '娱乐/艺术/表演', '律师/法务', '教育/培训', '公务员/行政/事业单位', '空姐', '模特', '其他', '未设置' ); $current=trim($current_user->career); if($careers&&$current){ foreach ($careers as $career) { if($career==$current){ echo '<option selected="selected">'.$career.'</option>'; }else{ echo '<option>'.$career.'</option>'; } } }else{ echo '<option>计算机/互联网/通信</option><option>生产/工艺/制造</option><option>学生</option><option>医疗/护理/制药</option><option>金融/银行/投资/保险</option><option>商业/服务业/个体经营</option><option>文化/广告/传媒</option><option>娱乐/艺术/表演</option><option>律师/法务</option><option>教育/培训</option><option>公务员/行政/事业单位</option><option>空姐</option><option>模特</option><option>其他</option><option selected="selected">未设置</option>'; } ?> 
</select> 
</td> 
</tr>

3.第三种情况一般用于性别选择,我们在很多网站上都能见到了,一般是三个单选按钮,男、女、保密

<!-- gender -->
<tr class="user-wrap user-gender-wrap">
	<th valign="top"><label for="gender">性别</label></th>
	<td valign="top">
		<div class="gender-radio-list">
			<?php 
			$genders=array('男','女','保密');
			$current=trim($current_user->gender);
			if($genders&&$current){
				foreach ($genders as $gender) {
					if (!empty($current)&&$gender==$current) {
						# code...
						echo '<label class="check"><input name="gender" type="radio" class="u-rdi" checked="true" value="'.$gender.'">'.$gender.'</label>';

						}else{
							echo '<label class="check"><input name="gender" type="radio" class="u-rdi" value="'.$gender.'">'.$gender.'</label>';
						}
					}
				}else{
					echo '<label class="check"><input name="gender" type="radio" class="u-rdi" value="男">男</label>
					<label class="check"><input name="gender" type="radio" class="u-rdi" value="女">女</label>
					<label class="check"><input name="gender" type="radio" class="u-rdi" checked="true" value="保密">保密</label>';
				}
			 ?>
		</div>
	</td>
</tr>

以上就是三种字段表单的范例,表单写完之后我们需要进行一个提交表单之后,用户数据库的更改,这样用户设置页面就会提交之后,刷新页面,然后显示已更改的数据。以下是范例代码。

<?php 
//先判断form表单提交
if(isset( $_POST['update_user'] )&&isset($_POST['submit']) && isset($_POST['action']) && $_POST['action']=='update_user' ){
//获取表单数据,把它保存在数组中,当然,也可不保存为数组
$data['uid'] = isset($_POST['user_id']) ? $_POST['user_id'] : '';
$data['last_name'] = isset($_POST['last_name']) ? $_POST['last_name'] : '';
$data['first_name'] = isset($_POST['first_name']) ? $_POST['first_name'] : '';
$data['nickname'] = isset($_POST['nickname']) ? $_POST['nickname'] : '';
$data['display_name'] = isset($_POST['display_name']) ? $_POST['display_name'] : '';
$data['user_url'] = isset($_POST['user_url']) ? $_POST['user_url'] : '';
$data['phone'] = isset($_POST['phone']) ? $_POST['phone'] : '';
$data['gender'] = isset($_POST['gender']) ? $_POST['gender'] : '';
$data['birthday'] = isset($_POST['birthday']) ? $_POST['birthday'] : '';
$data['school'] = isset($_POST['school']) ? $_POST['school'] : '';
$data['career'] = isset($_POST['career']) ? $_POST['career'] : '';
//这里用update_user_meta()来更新用户字段,要一一对应
update_user_meta($data['uid'],'last_name',$data['last_name']);
update_user_meta($data['uid'],'first_name',$data['first_name']);
update_user_meta($data['uid'],'nickname',$data['nickname']);
wp_update_user( 
	array( 
		'ID' => $data['uid'], 
		'display_name' => $data['display_name']
	) 
);
wp_update_user( 
	array( 
		'ID' => $data['uid'], 
		'user_url' => $data['user_url'] 
	) 
);
update_user_meta($data['uid'],'description',$data['description']);

update_user_meta($data['uid'],'phone',$data['phone']);
update_user_meta($data['uid'],'gender',$data['gender']);
update_user_meta($data['uid'],'birthday',$data['birthday']);
update_user_meta($data['uid'],'school',$data['school']);
update_user_meta($data['uid'],'career',$data['career']);


echo '<script>alert("保存成功。");  history.back(); </script>';
}?>

把基本设置页面的思路总结一下

  1. 需要修改的usermeta或者user数据表单
  2. 提交表单之后的php执行函数

以下贴出用户基本设置页面完整代码

<?php 
if(!is_user_logged_in()){
   wp_redirect(home_url());
}
get_header(); ?>
<link rel="stylesheet" href="<?php echo get_bloginfo('template_directory') ?>/css/page-css/author-settings.css">
<div class="custom_container profile">
<h3 class="setting-title">账户设置</h3>
<div class="settings-top-func-wrap">
<div class="back-to-profile-wrap">
	<a href="<?php echo get_author_posts_url($current_user->ID); ?>">< 返回个人中心</a>
</div>
<div class="settings-logout-wrap">
	<a href="<?php echo wp_logout_url(home_url()); ?>">退出账号</a>
</div>
</div>
<div class="settings-content">
<?php get_template_part( 'custom-part/author-endpoint/settings', 'tabs'); ?><!-- 引入TABS -->

<div class="user_option">
<form id="your-profile" action="" method="post" novalidate="novalidate">
<table class="form-table" style = "border-collapse:separate; border-spacing:15px;">
<!-- Username -->
<tr class="user-wrap user-user-login-wrap">
<th valign="top"><label for="user_login">用户名</label></th>
<td valign="top">
	<input type="text" name="user_login" id="user_login" value="<?php echo $current_user->user_login; ?>" disabled="disabled" >
	<div class="description">用户名不可更改。</div>
</td>
</tr>
<!-- Last Name -->
<tr class="user-wrap user-last_name-wrap">
<th valign="top"><label for="last_name" maxlength="16">姓氏</label></th>
<td valign="top">
	<input type="text" name="last_name" id="last_name" value="<?php echo trim($current_user->last_name); ?>" >
</td>	
</tr>
<!-- First Name -->
<tr class="user-wrap user-first_name-wrap">
<th valign="top"><label for="first_name" maxlength="16">名字</label></th>
<td valign="top">
	<input type="text" name="first_name" id="first_name" value="<?php echo trim($current_user->first_name); ?>" >
</td>	
</tr>
<!-- Nickname -->
<tr class="user-wrap user-nickname-wrap">
<th valign="top"><label for="nickname" maxlength="30">昵称</label></th>
<td valign="top">
	<input type="text" name="nickname" id="nickname" value="<?php echo trim($current_user->nickname); ?>" >
</td>
</tr>
<!-- Display Name -->
<tr class="user-wrap user-display_name-wrap">
<th valign="top"><label for="display_name">网站显示名</label></th>
<td valign="top"><select name="display_name" id="display_name">
<?php
$public_display                     = array();
$public_display['display_nickname'] = $current_user->nickname;
$public_display['display_username'] = $current_user->user_login;

if ( ! empty( $current_user->first_name ) ) {
$public_display['display_firstname'] = $current_user->first_name;
}

if ( ! empty( $current_user->last_name ) ) {
$public_display['display_lastname'] = $current_user->last_name;
}

if ( ! empty( $current_user->first_name ) && ! empty( $current_user->last_name ) ) {
$public_display['display_firstlast'] = $current_user->first_name . ' ' . $current_user->last_name;
$public_display['display_lastfirst'] = $current_user->last_name . ' ' . $current_user->first_name;
}

if ( ! in_array( $current_user->display_name, $public_display ) ) { // Only add this if it isn't duplicated elsewhere
$public_display = array( 'display_displayname' => $current_user->display_name ) + $public_display;
}

$public_display = array_map( 'trim', $public_display );
$public_display = array_unique( $public_display );

foreach ( $public_display as $id => $item ) {
?>
<option <?php selected( $current_user->display_name, $item ); ?>><?php echo $item; ?></option>
<?php
}
?>
</select></td>
</tr>
<!-- User Url -->
<tr class="user-wrap user-user_url-wrap">
	<th valign="top"><label for="user_url">个人网站</label></th>
	<td valign="top"><input type="text" maxlength="50" name="user_url" id="user_url" value="<?php echo trim($current_user->user_url); ?>" ></td>
</tr>
<!-- Description -->
<tr class="user-wrap user-description-wrap">
<th valign="top"><label for="description">个人说明</label></th>
<td valign="top">
<textarea name="description" maxlength="300" rows="5" cols="30"><?php echo $current_user->description; ?></textarea>
<div class="description">分享关于您的一些信息,可能会被公开。</div>
</td>
</tr>
<!-- phone -->
<tr class="user-wrap user-phone-wrap">
	<th valign="top"><label for="phone">电话</label></th>
	<td valign="top"><input type="text" maxlength="30" name="phone" id="phone" value="<?php echo trim($current_user->phone); ?>" ></td>
</tr>
<!-- gender -->
<tr class="user-wrap user-gender-wrap">
	<th valign="top"><label for="gender">性别</label></th>
	<td valign="top">
		<div class="gender-radio-list">
			<?php 
			$genders=array('男','女','保密');
			$current=trim($current_user->gender);
			if($genders&&$current){
				foreach ($genders as $gender) {
					if (!empty($current)&&$gender==$current) {
						# code...
						echo '<label class="check"><input name="gender" type="radio" class="u-rdi" checked="true" value="'.$gender.'">'.$gender.'</label>';

						}else{
							echo '<label class="check"><input name="gender" type="radio" class="u-rdi" value="'.$gender.'">'.$gender.'</label>';
						}
					}
				}else{
					echo '<label class="check"><input name="gender" type="radio" class="u-rdi" value="男">男</label>
					<label class="check"><input name="gender" type="radio" class="u-rdi" value="女">女</label>
					<label class="check"><input name="gender" type="radio" class="u-rdi" checked="true" value="保密">保密</label>';
				}
			 ?>
		</div>
	</td>
</tr>
<!-- birthday -->
<tr class="user-wrap user-birthday-wrap">
	<th valign="top"><label for="birthday">生日</label></th>
	<td valign="top"><input name="birthday" id="birthday" type="date" value="<?php echo trim($current_user->birthday); ?>"></td>
</tr>
<!-- school -->
<tr class="user-wrap user-birthday-wrap">
	<th valign="top"><label for="school">学校</label></th>
	<td valign="top"><input type="text" maxlength="85" name="school" id="school" value="<?php echo trim($current_user->school); ?>" ></td>
</tr>
<!-- career -->
<tr class="user-wrap user-career-wrap">
	<th valign="top"><label for="career">职业</label></th>
	<td valign="top">
		<select name="career" id="career">
		<?php $careers= array(
			'计算机/互联网/通信',
			'生产/工艺/制造',
			'学生',
			'医疗/护理/制药',
			'金融/银行/投资/保险',
			'商业/服务业/个体经营',
			'文化/广告/传媒',
			'娱乐/艺术/表演',
			'律师/法务',
			'教育/培训',
			'公务员/行政/事业单位',
			'空姐',
			'模特',
			'其他',
			'未设置'
		);
		$current=trim($current_user->career);
		if($careers&&$current){
			foreach ($careers as $career) {
				if($career==$current){
				echo '<option selected="selected">'.$career.'</option>';
			}else{
				echo '<option>'.$career.'</option>';
			}
			}
		}else{
			echo '<option>计算机/互联网/通信</option><option>生产/工艺/制造</option><option>学生</option><option>医疗/护理/制药</option><option>金融/银行/投资/保险</option><option>商业/服务业/个体经营</option><option>文化/广告/传媒</option><option>娱乐/艺术/表演</option><option>律师/法务</option><option>教育/培训</option><option>公务员/行政/事业单位</option><option>空姐</option><option>模特</option><option>其他</option><option selected="selected">未设置</option>';
			}
		 ?>
		</select>
	</td>
</tr>
<!-- baisc-user-avatar -->
<!-- Submit Button -->
<tr class="user-wrap user-submit-wrap">
<th valign="top"><label for="submit"></label></th>
<td valign="top">
<br/>
<?php wp_nonce_field( 'update_user', 'update_user', false ); ?>
<input type="hidden" name="action" value="update_user">
<input type="hidden" name="user_id" id="user_id" value="<?php echo $current_user->ID; ?>">
<input type="submit" name="submit" value="保存" class="submit-btn">
</td>
</tr>

</table>
</form>
</div>
<?php 
//先判断form表单提交
if(isset( $_POST['update_user'] )&&isset($_POST['submit']) && isset($_POST['action']) && $_POST['action']=='update_user' ){
//获取表单数据,把它保存在数组中,当然,也可不保存为数组
$data['uid'] = isset($_POST['user_id']) ? $_POST['user_id'] : '';
$data['last_name'] = isset($_POST['last_name']) ? $_POST['last_name'] : '';
$data['first_name'] = isset($_POST['first_name']) ? $_POST['first_name'] : '';
$data['nickname'] = isset($_POST['nickname']) ? $_POST['nickname'] : '';
$data['display_name'] = isset($_POST['display_name']) ? $_POST['display_name'] : '';
$data['user_url'] = isset($_POST['user_url']) ? $_POST['user_url'] : '';
$data['phone'] = isset($_POST['phone']) ? $_POST['phone'] : '';
$data['gender'] = isset($_POST['gender']) ? $_POST['gender'] : '';
$data['birthday'] = isset($_POST['birthday']) ? $_POST['birthday'] : '';
$data['school'] = isset($_POST['school']) ? $_POST['school'] : '';
$data['career'] = isset($_POST['career']) ? $_POST['career'] : '';
//这里用update_user_meta()来更新用户字段,要一一对应
update_user_meta($data['uid'],'last_name',$data['last_name']);
update_user_meta($data['uid'],'first_name',$data['first_name']);
update_user_meta($data['uid'],'nickname',$data['nickname']);
wp_update_user( 
	array( 
		'ID' => $data['uid'], 
		'display_name' => $data['display_name']
	) 
);
wp_update_user( 
	array( 
		'ID' => $data['uid'], 
		'user_url' => $data['user_url'] 
	) 
);
update_user_meta($data['uid'],'description',$data['description']);

update_user_meta($data['uid'],'phone',$data['phone']);
update_user_meta($data['uid'],'gender',$data['gender']);
update_user_meta($data['uid'],'birthday',$data['birthday']);
update_user_meta($data['uid'],'school',$data['school']);
update_user_meta($data['uid'],'career',$data['career']);


echo '<script>alert("保存成功。");  history.back(); </script>';
}?>
</div>

</div>
<?php get_footer(); ?>

用户Email设置页面的完整代码

<?php 
if(!is_user_logged_in()){
   wp_redirect(home_url());
}
get_header(); ?>
<link rel="stylesheet" href="<?php echo get_bloginfo('template_directory') ?>/css/page-css/author-settings.css">
<div class="custom_container profile">
<h3 class="setting-title">账户设置</h3>
<div class="settings-top-func-wrap">
<div class="back-to-profile-wrap">
    <a href="<?php echo get_author_posts_url($current_user->ID); ?>">< 返回个人中心</a>
</div>
<div class="settings-logout-wrap">
    <a href="<?php echo wp_logout_url(home_url()); ?>">退出账号</a>
</div>
</div>
<div class="settings-content">
<?php get_template_part( 'custom-part/author-endpoint/settings', 'tabs'); ?><!-- 引入TABS -->
<div class="user_option">
<form id="your-profile" action="" method="post" novalidate="novalidate">
<table class="form-table" style = "border-collapse:separate; border-spacing:15px;">
<!-- email -->
<tr class="user-wrap user-email-wrap">
	<th valign="top"><label for="email">Email</label></th>
	<td valign="top"><input class="text-input" name="email" type="text" id="email" value="<?php the_author_meta( 'user_email', $current_user->ID ); ?>" /></td>
</tr>
<!-- Submit Button -->
<tr class="user-wrap user-submit-wrap">
<th valign="top"><label for="submit"></label></th>
<td valign="top">
<br/>
<?php wp_nonce_field( 'update_user', 'update_user', false ); ?>
<input type="hidden" name="action" value="update_user">
<input type="hidden" name="user_id" id="user_id" value="<?php echo $current_user->ID; ?>">
<input type="submit" name="submit" value="保存" class="submit-btn">
</td>
</tr>
</table>
</form>
<?php 
//先判断form表单提交
if(isset( $_POST['update_user'] )&&isset($_POST['submit']) && isset($_POST['action']) && $_POST['action']=='update_user' ){
//获取表单数据,把它保存在数组中,当然,也可不保存为数组
$data['uid'] = isset($_POST['user_id']) ? $_POST['user_id'] : '';
$data['email']=isset($_POST['email']) ? $_POST['email'] : '';
//这里用update_user_meta()来更新用户字段,要一一对应
$the_email_user = email_exists(esc_attr($data['email']));
if ( empty($data['email'])){
	echo '<div class="error-warning">邮箱地址不能为空!</div>';
}else{
        if (!is_email(esc_attr($data['email']))){
            echo '<div class="error-warning">您输入的电子邮箱地址无效,请重试。</div>';
        }
        elseif($the_email_user && $the_email_user != $current_user->id ){
            echo '<div class="error-warning">您输入的电子邮箱已被占用,请更换后重试。</div>';
        }
        else{
            wp_update_user( array ('ID' => $current_user->ID, 'user_email' => esc_attr($data['email'])));
			echo '<script>alert("保存成功。");  history.back(); </script>';
        }
    }
}
?>
</div>
</div>
</div>
<?php get_footer(); ?>

用户密码修改页面的完整代码

<?php 
if(!is_user_logged_in()){
   wp_redirect(home_url());
}
get_header(); ?>
<link rel="stylesheet" href="<?php echo get_bloginfo('template_directory') ?>/css/page-css/author-settings.css">
<div class="custom_container profile">
<h3 class="setting-title">账户设置</h3>
<div class="settings-top-func-wrap">
<div class="back-to-profile-wrap">
  <a href="<?php echo get_author_posts_url($current_user->ID); ?>">< 返回个人中心</a>
</div>
<div class="settings-logout-wrap">
  <a href="<?php echo wp_logout_url(home_url()); ?>">退出账号</a>
</div>
</div>
<div class="settings-content">
<?php get_template_part( 'custom-part/author-endpoint/settings', 'tabs'); ?><!-- 引入TABS -->
<div class="user_option">
<form id="your-profile" action="" method="post" novalidate="novalidate">
<table class="form-table" style = "border-collapse:separate; border-spacing:15px;">
<!-- password -->
<tr class="user-wrap user-current_password-wrap">
	<th valign="top"><label for="current_password">原密码</label></th>
	<td valign="top"><input id="current_password" type="password" name="current_password" title="current_password" placeholder="" required></td>
</tr>
<tr class="user-wrap user-new_password-wrap">
	<th valign="top"><label for="new_password">新密码</label></th>
	<td valign="top"><input id="new_password" type="password" name="new_password" title="new_password" placeholder="" required></td>
</tr>
<tr class="user-wrap user-confirm_new_password-wrap">
	<th valign="top"><label for="confirm_new_password">确认新密码</label></th>
	<td valign="top"><input id="confirm_new_password" type="password" name="confirm_new_password" title="confirm_new_password" placeholder="" required></td>
</tr>
<!-- Submit Button -->
<tr class="user-wrap user-submit-wrap">
<th valign="top"><label for="submit"></label></th>
<td valign="top">
<br/>
<?php wp_nonce_field( 'update_user', 'update_user', false ); ?>
<input type="hidden" name="action" value="update_user">
<input type="hidden" name="user_id" id="user_id" value="<?php echo $current_user->ID; ?>">
<input type="submit" name="submit" value="保存" class="submit-btn">
</td>
</tr>
</table>
</form>
<?php 
//先判断form表单提交
if(isset( $_POST['update_user'] )&&isset($_POST['submit']) && isset($_POST['action']) && $_POST['action']=='update_user' ){
if(isset($_POST['current_password'])){
          $_POST = array_map('stripslashes_deep', $_POST);
          $current_password = sanitize_text_field($_POST['current_password']);
          $new_password = sanitize_text_field($_POST['new_password']);
          $confirm_new_password = sanitize_text_field($_POST['confirm_new_password']);
          $user_id = get_current_user_id();
          $errors = array();
          $current_user = get_user_by('id', $user_id);
    }
// Check for errors
if (empty($current_password) && empty($new_password) && empty($confirm_new_password) ) {
$errors[] = '所有表单都必须输入';
}
if($current_user && wp_check_password($current_password, $current_user->data->user_pass, $current_user->ID)){
//match
} else {
    $errors[] = '原密码输入错误';
}
if($new_password != $confirm_new_password){
    $errors[] = '确认新密码错误';
}
if(strlen($new_password) < 8){
    $errors[] = '新密码太短,至少8个字符';
}
// Changing the Password
if(empty($errors)){
    wp_set_password( $new_password, $current_user->ID );
    echo '<script>alert("密码修改成功!");history.back();</script>';
} else {
    // Echo Errors
    foreach($errors as $error){
        echo '<div class="error-warning">'.$error.'</div>';
    }
}
}?>
</div>
</div>
</div>
<?php get_footer(); ?>

到此我们的用户中心功能就已经全部基本实现了,如果你还有一些想要的额外功能,在此基础上进行扩展即可。

全部评论 (0条评论)

发表评论

正在加载验证码......

请先完成验证