最近在开发wordpress主题过程中,用代码制作了前端用户中心,同时前端用户中心中还需要用户密码重置,我们可以通过短代码在主题的任何位置插入重置密码的表单:第一步:在functions当中插入如下代码:function pippin_change_password_form() {global $post; if (…
最近在开发wordpress主题过程中,用代码制作了前端用户中心,同时前端用户中心中还需要用户密码重置,我们可以通过短代码在主题的任何位置插入重置密码的表单:
第一步:在functions当中插入如下代码:
function pippin_change_password_form() {global $post; if (is_singular()) : $current_url = get_permalink($post->ID); else : $pageURL = 'http'; if ($_SERVER["HTTPS"] == "on") $pageURL .= "s"; $pageURL .= "://"; if ($_SERVER["SERVER_PORT"] != "80") $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"]; else $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"]; $current_url = $pageURL; endif;$redirect = $current_url; ob_start(); // show any error messages after form submissionpippin_show_error_messages(); ?> <?php if(isset($_GET['password-reset']) && $_GET['password-reset'] == 'true') { ?><div class="pippin_message success"><span><?php _e('Password changed successfully', 'rcp'); ?></span></div><?php } ?><form id="pippin_password_form" method="POST" action="<?php echo $current_url; ?>"><fieldset><p><label for="pippin_user_pass"><?php _e('New Password', 'rcp'); ?></label><input name="pippin_user_pass" id="pippin_user_pass" class="required" type="password"/></p><p><label for="pippin_user_pass_confirm"><?php _e('Password Confirm', 'rcp'); ?></label><input name="pippin_user_pass_confirm" id="pippin_user_pass_confirm" class="required" type="password"/></p><p><input type="hidden" name="pippin_action" value="reset-password"/><input type="hidden" name="pippin_redirect" value="<?php echo $redirect; ?>"/><input type="hidden" name="pippin_password_nonce" value="<?php echo wp_create_nonce('rcp-password-nonce'); ?>"/><input id="pippin_password_submit" type="submit" value="<?php _e('Change Password', 'pippin'); ?>"/></p></fieldset></form><?phpreturn ob_get_clean();} // password reset formfunction pippin_reset_password_form() {if(is_user_logged_in()) {return pippin_change_password_form();}}add_shortcode('password_form', 'pippin_reset_password_form'); function pippin_reset_password() {// reset a users passwordif(isset($_POST['pippin_action']) && $_POST['pippin_action'] == 'reset-password') { global $user_ID; if(!is_user_logged_in())return; if(wp_verify_nonce($_POST['pippin_password_nonce'], 'rcp-password-nonce')) { if($_POST['pippin_user_pass'] == '' || $_POST['pippin_user_pass_confirm'] == '') {// password(s) field emptypippin_errors()->add('password_empty', __('Please enter a password, and confirm it', 'pippin'));}if($_POST['pippin_user_pass'] != $_POST['pippin_user_pass_confirm']) {// passwords do not matchpippin_errors()->add('password_mismatch', __('Passwords do not match', 'pippin'));} // retrieve all error messages, if any$errors = pippin_errors()->get_error_messages(); if(empty($errors)) {// change the password here$user_data = array('ID' => $user_ID,'user_pass' => $_POST['pippin_user_pass']);wp_update_user($user_data);// send password change email here (if WP doesn't)wp_redirect(add_query_arg('password-reset', 'true', $_POST['pippin_redirect']));exit;}}}}add_action('init', 'pippin_reset_password'); if(!function_exists('pippin_show_error_messages')) {// displays error messages from form submissionsfunction pippin_show_error_messages() {if($codes = pippin_errors()->get_error_codes()) {echo '<div class="pippin_message error">'; // Loop error codes and display errors foreach($codes as $code){ $message = pippin_errors()->get_error_message($code); echo '<span class="pippin_error"><strong>' . __('Error', 'rcp') . '</strong>: ' . $message . '</span><br/>'; }echo '</div>';}}} if(!function_exists('pippin_errors')) { // used for tracking error messagesfunction pippin_errors(){ static $wp_error; // Will hold global variable safely return isset($wp_error) ? $wp_error : ($wp_error = new WP_Error(null, null, null));}}
第二步:在你需要的地方插入:
[password_form]
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。
评论(0)