• Hi., I have developed a Q & A blog. Every thing works fine except forget password page, If the registered user wants to recover their password they want to enter their registered email id after entering it an automatic email will be generate with the new password. But its not working.
    The code i have used is

    <?php
    /**
     * @package WordPress
     * @subpackage Instant Q&A - WPQA
    
     Template Name: Forgot Password
    
     */
     ?>
    
    <?php
    
    if ( $_POST['forgot_pass_post'] == '1') {
    	$email_address = $_POST['email_address'];
    	$ip = $_SERVER['REMOTE_ADDR'];
    	$message = reset_user_password($email_address, $ip);
    }
    ?>
    
    <?php get_header(); ?>
    
    	<!-- Main Section -->
        <div id="main">
        	<div id="mainContent">
    
               	<?php include (TEMPLATEPATH . '/sidebar-left-categories.php'); ?>   
    
                <!-- Center Column -->
                <div id="centerCol" class="left">
    
                    <!-- Search Form -->
    				<?php get_search_form(); ?>
                    <!-- Search Form -->
    
                 	<!-- Main Box -->
                    <div class="greyBox3">
                        <div class="greyBoxInner3">
                        	<?php
    						// If logged in change nav to My Account and Log Out
                            if ( !is_user_logged_in() ) { ?>
    
                            <h2><?php the_title(); ?></h2>
                            <!-- Show Form Errors -->
                            <p class="introText">Enter your email address below to reset your password. Your new password will be emailed to you shortly after completeing the form.</p>
                            <?php display_message($message) ?>
                            <form class="message" name="forgotPassword" id="forgotPassword" method="post" action="">
                                <label>Email Address:</label>
                                <fieldset><input type="text" name="email_address" id="email_address" class="input" size="20"  maxlength="40" tabindex="10" /></fieldset>
                                <input type="hidden" name="forgot_pass_post" value="1"/>
                                <div class="forgotpassword"><input type="image" name="resetpass" id="resetpass" src="<?php bloginfo('template_url'); ?>/images/reset-password-btn.png" value="Reset Password" /></div>                        </form>
                            <div class="divider"></div>
                            <div class="post">
                                <div class="whiteBtn right"><a>/?page_id=13"><span>Log In Now</span></a></div>
                                <div class="clear"></div>
                            </div>
                            <?php
    						// If user is already logged in Redirect them to My Account Page
    						 } else { ?>
    
    							<?php redirect_to_myaccount_url(); ?>
    
    						<?php }; ?>
    
                        </div>
                    </div>
                	<div class="reset"></div>
                   	<!-- / Main Box -->
    
                </div>
                <!-- / Center Column -->
                <?php get_sidebar(); ?>
    
            </div>
            <?php include (TEMPLATEPATH . '/banner_728x90.php'); ?>   
    
        </div>
        <!-- / Main Section -->
    
    <?php get_footer(); ?>
    
    Function used:
    
    function reset_user_password($email_address, $ip) {
    
    	// Strip out tags from email field
    	$email_address_stripped = strip_tags($email_address);
    
    	// Get user data based on email address
    	$user = get_user_by_email($email_address_stripped);
    
    	// Generate new password 10 Characters Long
    	$newPass = wp_generate_password(10);
    
    	// Check to see if the email is assigned to a member
    	require ( ABSPATH . WPINC . '/registration.php' );
    
    	if (!username_exists($user->user_login)) {
    		return new WP_Error('email_doesnt_exist','There is no member with this email address.');
    	}
    	else {
    		// Check to see if email is an admin with ID of 1 (could add other ids here too with an OR (||) statement)
    		if ($user->ID == 1) {
    			return new WP_Error('cant_change_admin_pass','You are not allowed to change that user\'s password, Sorry.');
    		}
    		// If non-admin email address, change password and then email them the new password
    		else {
    			wp_set_password($newPass, $user->ID);
    
    			$site = get_bloginfo("name");
    			$sitename = str_replace("&", "&", $site);
    
    			$headers = 'From: '.$sitename.' <forgot_password@'.$sitename.'>' . "\r\n";
    			$to = $user->user_email;
    			$subject = 'Account Update - Your password has been reset';
    			$message = sprintf(__('Dear %s,'), $user->user_login) . "\r\n\r\n";
    			$message .= sprintf(__('Your password has been reset because you recently filled out the Forgot Password form on %s.'), $sitename) . "\r\n\r\n";
    			$message .= sprintf(__('-------------------------------------------------------------------------------------')). "\r\n\r\n";
    			$message .= sprintf(__('Username: %s'), $user->user_login) . "\r\n\r\n";
    			$message .= sprintf(__('New Password: %s'), $newPass) . "\r\n\r\n";
    			$message .= sprintf(__('-------------------------------------------------------------------------------------')). "\r\n\r\n";
    			$message .= sprintf(__('This request was submitted by the following IP address: %s.'), $ip) . "\r\n\r\n";
    			$message .= sprintf(__('Thank you,')) . "\r\n\r\n";
    			$message .= sprintf(__('Have a great day!')) . "\r\n\r\n";
    
    			wp_mail($to, $subject, $message, $headers);
    
    			// Set a flag on this user - Will show a message to the user once they log in for the first time on My Account. Then my-account.php will set it to 'no' so it wont show again.
    			update_usermeta( $user->ID, 'passwordreset', 'yes');
    
    			// Let the user know they have reset their password successfully
    			return $message = 'You have successfully reset your password. Your username and new password have been sent to your email:<span>'.$email_address. '</span>.';
    		}
    	}
    }

    [Moderator Note: Please post code or markup snippets between backticks or use the code button. Or better still – use the pastebin. As it stands, your code may now have been permanently damaged/corrupted by the forum’s parser.]

    Please help me out.

  • The topic ‘Forget Password Page’ is closed to new replies.