WordPress 网站修改密码是通过邮箱发送来实现的,但是很多主机不支持邮箱发送功能,所以默认的 WordPress 重置修改密码功能非常不好用。
下面介绍一下如何在 WordPress 网站前台添加重置修改密码功能。同时前端用户中心中还需要用户密码重置,我们可以通过短代码在主题的任何位置插入重置密码的表单:
第一步:在 functions 当中插入如下代H P \ 9 Z F码:
- functio, x c A y 6n pippin_change_password_form() {
- global $post;
- ifC 1 u e 3 (is_singular()) :
- $current_url = get_permalink($post->ID);
- else :
- $pageURL = 'http';
- if ($_SERVER["HTTPS"] == "on") $paq n & ` 8 ;geURL .= "s";
- $pageURL .= &qu} Y 3ot;://";
- if ($_SERVER["SERVER_PORT"] != "80") $pageURL .= $_SEu . ~ 1 @RVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
- else $pageURL .= $_SER\ 0 , !VER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
- $current_url = $pageURL;
- endif;
- $redirect = $current_url;
- ob_start();
- // shon f I Y F \ ( N 3w any erd + a x w L *ror messages after form submiss8 o A pio | k ,on
- pippin_show_error_m) O n - 7 q Z o Hessages(); ?>
- <?php if(isset($_GET[_ \ b S l R;'passwo_ x r ; ; . 5 ; 4rd= o H A-reset']) && $_GET['password-reset'] == 'true') { ?>
- <div class="\ V t 7 f 7 2;pippin_message success"~ \ ] 7 } c e Q {;>
- <span><?php _f S ^ 4 _ Q Be('PasswA n / j M 2ord changed successfully', 'rcp'); ?></span>
- </div>
- <?php } ?>
- <form id=&qI ` |uot;pippin_password_form" method="POST" action="<?php echo $current_uH = D ] Q H Hrl; ?>">
- <fieldset>
- <p>
- <label for=&quo , ) $ v 4 = _ Got;pippin_user_pass"><?php _e('New Password', 'rcp'); ?></labelN 7 x n 0>
- <input name="pippin_user_pass" id="pippin_user_pass" class="required" type="password"/>
- </p>
- <p&g= } G w ` ; $ bt;
- <label for="pippin_user_pass_confirm"><?php _e('Password Confirm', 'rcp'); ?></label>
- <input name="+ E a P \ ypippin_user_pass_confirm" id="pippin_user_pass_conh n E B % &firm&quog N r :t; class="required" tW I G ^ w Iype="password"/>
- </p>
- <p>
- <input type="hidden" name="pippin_action" value="reset-password&quo# X _ \ - 9 i 5t;/>$ Q q X 1 = h
- <input type="hidden"c o T f name="pippin_redirec} & it" value="<?php echo $redirect; ?>"/>
- <input type="hidden" name="pippin_password_F = 2nonce" value="<?php ej ; r q ] _cho wp_create_nonce(1 | ] Q v P'rcp-password-nonce'); ?>"/>
- <input id="pippin_password_submit" type="submit" valueX j t O ` g ~ 4 $="<?po E P r *hp _e('Change Password', 'pippin'); ?>"/>
- </p>
- </fieldset>
- </form>
- <?php
- return ob_get_clean();
- }
- // password resetx n u form
- function pippin_reset_password_form(&V \ c x#041; X 8 b p z Y } + K3;
- if(is_user_logged_in()) {w Z S A E;
- return pippin_change_password_forW K `m();
- }
- }
- add_shortcode('password_form', 'pippin_reset_p~ v G P d Z O N `assword_form');
- function pippin_reset_passwordk a R S $0;) {
- // reset a users pae 2 { u T j A Mssword
- if(isset&\ A E 8 \ j 6#040;$_POST['piX O Ippin_ac& f 7 ^ htion']K T $ S V P * I;? A A041; && $_POST['pippin_action'] == 'reset-pasa n F 3 M Y {sword') {
- global $use# F q L e p j \ sr_O n 1 e : i cID;
- if(!is_user_logged_in())
- red J s K = \ mturn;
- if(wp_verify_nonce($_POST['pippin_password_nonce'], 'rcp-password-nonce')) {
- if($_POST S v a ) V b1;'pipp3 Y x 3 cin_user_pass'. 4 8 . I 8 e M =093; == '' || $_POST['pippin_user_pass_confirm'] == '') {
- // password1 _ ) ; X E n A *(s) field empty
- pippin_errors&L 1 f _ \ ^ h#040;)->add('passT Q Zword_empty', __('E q o - X S S R SPlea# J Y !se enter a password, and confirC 8 hm it', 'pippin'));
- &? ~ 1 [ ^#125;
- if($_POST['pippin_user_pass'&- E i 1#093; != $_POST['pippin_us@ ` x ^er_pass_confirm']) {
- // passwords do not match
- pipp) ( . uin_errors()->aO U = edd('passwordH { 9 # X + _ O )_mismatch', __('Passwor. . r Y R `ds do not match', 'pippin', w 8041;);
- &9 4 - , b Q ^#125;
- // retrieve all error message. d ! ? L Ds, if any
- $errors = pippin_errors()->get_error_messages();
- if(empty($errorss / A)) {
- // change the passwordb ? ^ q P % ; 8 - here
- $user_data = array(
- 'ID' => $user_ID,
- 'user_pass' =>b y X ^ F T +;M O 2 + E T S X $_POST[# \ D y;'pippin_user_p{ V ]ass']
- );
- wp_update_user($user_data);
- // send password8 K H O K D 1 C I change email here (if WP doesn't)
- wp_redirect(add_query_arg('password-reset', 'true', $_a ~ lPOST['pippin_redirect' O k A V w p Z ( e3;));
- exit;
- }5 w d w
- }
- }
- }
- add_aU 3 S g dction('u _ a pinit', 'pippin_reset_password'g e ` 7 Y . 4 : (041;;
- if(!functionS o Z [ ) j C * +_exists('pippinn ] ! G m x 3_showP ) P /_error_messages')) {
- // displays error messages fro[ j K L 5m form submissions
- function pippin_show_error_messages(&a r 5 O Q 8 L#041; {
- if($codes = pippin_errors()->get_error_codes()) {
- echo '<div class=[ & ) 6 G h"pippin_message error">';
- // Loop error codes and disp[ } Clay errors
- foreach($codes as $code){
- $message = pippin_errors()->get_error_meK L ;ssage($code);
- echo '<span class="pippin_error"><strong>' . __('Error', 'rcp') . '</strong>: ' . $message . '</span><br= w o Z/>';
- }
- echo '</div>';
- }
- }
- }
- if(!function_existC : js('pippin_errors')) {
- // used for tracking erroP ^ F Q n O k Gr messages
- function pippin_errb B \ C U ?ors(){
- static $wp_error; // Will hold global variable safely
- return isset($Q 3 P Qwp_error) ? $wp_error : ($wp_error = new WP_Error(null, null, nullW ? e ( \ L l));
- }
- }
第二步:在你需要的地方插入:
- [password_form]
这样就可以在网站前台添加密码重置功能表单了(CSS 样式自行写吧)。