WordPress教程

WordPress主题实现前台用户重置密码功能

阿里云

最近在开发 wordpress 主题过程中,用代码制作了前端用户中心,同时前端用户中心中还需要用户密码重置,我们可以通过短代码在主题的任何位置插入重置密码的表单:

第一步:

在 functions 当中插入如下代码:

也想出现在这里?联系我们
创客主机
  1. function pippin_change_password_form() {
  2. 	global $post;	
  3.  
  4.    	if (is_singular()) :
  5.    		$current_url = get_permalink($post->ID);
  6.    	else :
  7.    		$pageURL = 'http';
  8.    		if ($_SERVER["HTTPS"] == "on") $pageURL .= "s";
  9.    		$pageURL .= "://";
  10.    		if ($_SERVER["SERVER_PORT"] != "80") $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
  11.    		else $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
  12.    		$current_url = $pageURL;
  13.    	endif;		
  14. 	$redirect = $current_url;
  15.  
  16. 	ob_start();
  17.  
  18. 		// show any error messages after form submission
  19. 		pippin_show_error_messages(); ?>
  20.  
  21. 		<?php if(isset($_GET['password-reset']) && $_GET['password-reset'] == 'true') { ?>
  22. 			<div class="pippin_message success">
  23. 				<span><?php _e('Password changed successfully', 'rcp'); ?></span>
  24. 			</div>
  25. 		<?php } ?>
  26. 		<form id="pippin_password_form" method="POST" action="<?php echo $current_url; ?>">
  27. 			<fieldset>
  28. 				<p>
  29. 					<label for="pippin_user_pass"><?php _e('New Password', 'rcp'); ?></label>
  30. 					<input name="pippin_user_pass" id="pippin_user_pass" class="required" type="password"/>
  31. 				</p>
  32. 				<p>
  33. 					<label for="pippin_user_pass_confirm"><?php _e('Password Confirm', 'rcp'); ?></label>
  34. 					<input name="pippin_user_pass_confirm" id="pippin_user_pass_confirm" class="required" type="password"/>
  35. 				</p>
  36. 				<p>
  37. 					<input type="hidden" name="pippin_action" value="reset-password"/>
  38. 					<input type="hidden" name="pippin_redirect" value="<?php echo $redirect; ?>"/>
  39. 					<input type="hidden" name="pippin_password_nonce" value="<?php echo wp_create_nonce('rcp-password-nonce'); ?>"/>
  40. 					<input id="pippin_password_submit" type="submit" value="<?php _e('Change Password', 'pippin'); ?>"/>
  41. 				</p>
  42. 			</fieldset>
  43. 		</form>
  44. 	<?php
  45. 	return ob_get_clean();	
  46. }
  47.  
  48. // password reset form
  49. function pippin_reset_password_form() {
  50. 	if(is_user_logged_in()) {
  51. 		return pippin_change_password_form();
  52. 	}
  53. }
  54. add_shortcode('password_form', 'pippin_reset_password_form');
  55.  
  56.  
  57. function pippin_reset_password() {
  58. 	// reset a users password
  59. 	if(isset($_POST['pippin_action']) && $_POST['pippin_action'] == 'reset-password') {
  60.  
  61. 		global $user_ID;
  62.  
  63. 		if(!is_user_logged_in())
  64. 			return;
  65.  
  66. 		if(wp_verify_nonce($_POST['pippin_password_nonce'], 'rcp-password-nonce')) {
  67.  
  68. 			if($_POST['pippin_user_pass'] == '' || $_POST['pippin_user_pass_confirm'] == '') {
  69. 				// password(s) field empty
  70. 				pippin_errors()->add('password_empty', __('Please enter a password, and confirm it', 'pippin'));
  71. 			}
  72. 			if($_POST['pippin_user_pass'] != $_POST['pippin_user_pass_confirm']) {
  73. 				// passwords do not match
  74. 				pippin_errors()->add('password_mismatch', __('Passwords do not match', 'pippin'));
  75. 			}
  76.  
  77. 			// retrieve all error messages, if any
  78. 			$errors = pippin_errors()->get_error_messages();
  79.  
  80. 			if(empty($errors)) {
  81. 				// change the password here
  82. 				$user_data = array(
  83. 					'ID' => $user_ID,
  84. 					'user_pass' => $_POST['pippin_user_pass']
  85. 				);
  86. 				wp_update_user($user_data);
  87. 				// send password change email here (if WP doesn't)
  88. 				wp_redirect(add_query_arg('password-reset', 'true', $_POST['pippin_redirect']));
  89. 				exit;
  90. 			}
  91. 		}
  92. 	}	
  93. }
  94. add_action('init', 'pippin_reset_password');
  95.  
  96. if(!function_exists('pippin_show_error_messages')) {
  97. 	// displays error messages from form submissions
  98. 	function pippin_show_error_messages() {
  99. 		if($codes = pippin_errors()->get_error_codes()) {
  100. 			echo '<div class="pippin_message error">';
  101. 			    // Loop error codes and display errors
  102. 			   foreach($codes as $code){
  103. 			        $message = pippin_errors()->get_error_message($code);
  104. 			        echo '<span class="pippin_error"><strong>' . __('Error', 'rcp') . '</strong>: ' . $message . '</span><br/>';
  105. 			    }
  106. 			echo '</div>';
  107. 		}	
  108. 	}
  109. }
  110.  
  111. if(!function_exists('pippin_errors')) { 
  112. 	// used for tracking error messages
  113. 	function pippin_errors(){
  114. 	    static $wp_error; // Will hold global variable safely
  115. 	    return isset($wp_error) ? $wp_error : ($wp_error = new WP_Error(null, null, null));
  116. 	}
  117. }

第二步:

在你需要的地方插入:

  1. [password_form]

WordPress 主题实现前台用户重置密码功能

已有 511 人购买
查看演示升级 VIP立刻购买

收藏
(0)

发表回复

热销模板

Ashade - 作品展示摄影相册WordPress汉化主题
LensNews

本站承接 WordPress / PbootCMS / DedeCMS 等
系统建站、仿站、开发、定制等业务!