短代码实现前台用户重置密码功能

前端之家收集整理的这篇文章主要介绍了短代码实现前台用户重置密码功能前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

在为客户直至用户中心的时候,需要实现已经登录用户重置密码的功能,我们可以通过短代码主题的任何位置插入重置密码的表单:

第一步:在functions当中插入如下代码

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 submission
    pippin_show_error_messages(); ?>

    <?php if(isset($_GET['password-reset']) &amp;&amp; $_GET['password-reset'] == 'true') { ?>
        <div class="pippin_message success"&gt;
            <span><?php _e('Password changed successfully','rcp'); ?></span>
        </div>
    <?php } ?>
    <form id="pippin_password_form" method="POST" action="<?php echo $current_url; ?>"&gt;
        <fieldset>
            <p>
                <label for="pippin_user_pass"&gt;<?php _e('New Password','rcp'); ?></label>
                <input name="pippin_user_pass" id="pippin_user_pass" class="<a href="https://www.jb51.cc/tag/required/" target="_blank" class="keywords">required</a>" type="password"/&gt;
            </p>
            <p>
                <label for="pippin_user_pass_confirm"&gt;<?php _e('Password Confirm','rcp'); ?></label>
                <input name="pippin_user_pass_confirm" id="pippin_user_pass_confirm" class="<a href="https://www.jb51.cc/tag/required/" target="_blank" class="keywords">required</a>" type="password"/&gt;
            </p>
            <p>
                <input type="hidden" name="pippin_action" value="reset-password"/&gt;
                <input type="hidden" name="pippin_redirect" value="<?php echo $redirect; ?>"/&gt;
                <input type="hidden" name="pippin_password_nonce" value="<?php echo wp_create_nonce('rcp-password-nonce'); ?>"/&gt;
                <input id="pippin_password_submit" type="submit" value="<?php _e('Change Password','pippin'); ?>"/&gt;
            </p>
        </fieldset>
    </form>
<?php
return ob_get_clean();  

}

// password reset form
function 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 password
if(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 empty
            pippin_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 match
            pippin_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 submissions
function 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">' . __('Error','rcp') . ': ' . $message . '
';
}
echo '

';
}
}
}

if(!function_exists('pippin_errors')) {
// used for tracking error messages
function pippin_errors(){
static $wp_error; // Will hold global variable safely
return isset($wp_error) ? $wp_error : ($wp_error = new WP_Error(null,null,null));
}
}

第二步:在你需要的地方插入:

原文链接:https://www.f2er.com/wordpress/67649.html

猜你在找的wordpress相关文章