• Resolved fcd_jsilva

    (@fcd_jsilva)


    I know I can do this by editing the core files but I would prefer creating a plugin that overwrites a few functions. I am new to mucking with the core functions so please bare with me. I am using a multisite setup and I want to change the URL that are used for lost passwords and in lost password emails. Right now it uses network_site_url and I want both to use site_url and point to the individual blog and not the network master. I know I have to overwrite the exiting functions but evidently I am not doing it correctly since I get an error on password resets and it cannot find the action=lostpassword. Any help on doing this is appreciated.

    <?php
    
    /**
     * Returns the Lost Password URL and sets custom essage and URL
     *
     * Returns the URL that allows the user to retrieve the lost password
     *
     * @since 2.8.0
     * @uses site_url() To generate the lost password URL
     * @uses apply_filters() calls 'lostpassword_url' hook on the lostpassword url
     *
     * @param string $redirect Path to redirect to on login.
     * @return string Lost password URL.
     */
    
    add_filter('wp_lostpassword_url', 'fcd_wp_lostpassword_url', 1);
    
    function fcd_wp_lostpassword_url( $redirect = '' ) {
    	$args = array( 'action' => 'lostpassword' );
    	if ( !empty($redirect) ) {
    		$args['redirect_to'] = $redirect;
    	}
    
    	$lostpassword_url = add_query_arg( $args, site_url('wp-login.php', 'login') );
    	return apply_filters( 'lostpassword_url', $lostpassword_url, $redirect );
    }
    
    add_filter('retrieve_password', 'fcd_retrieve_password', 1);
    
    function fcd_retrieve_password() {
    	global $wpdb, $current_site;
    
    	$errors = new WP_Error();
    
    	if ( empty( $_POST['user_login'] ) ) {
    		$errors->add('empty_username', __('<strong>ERROR</strong>: Enter a username or e-mail address.'));
    	} else if ( strpos( $_POST['user_login'], '@' ) ) {
    		$user_data = get_user_by( 'email', trim( $_POST['user_login'] ) );
    		if ( empty( $user_data ) )
    			$errors->add('invalid_email', __('<strong>ERROR</strong>: There is no user registered with that email address.'));
    	} else {
    		$login = trim($_POST['user_login']);
    		$user_data = get_user_by('login', $login);
    	}
    
    	do_action('lostpassword_post');
    
    	if ( $errors->get_error_code() )
    		return $errors;
    
    	if ( !$user_data ) {
    		$errors->add('invalidcombo', __('<strong>ERROR</strong>: Invalid username or e-mail.'));
    		return $errors;
    	}
    
    	// redefining user_login ensures we return the right case in the email
    	$user_login = $user_data->user_login;
    	$user_email = $user_data->user_email;
    
    	do_action('retreive_password', $user_login);  // Misspelled and deprecated
    	do_action('retrieve_password', $user_login);
    
    	$allow = apply_filters('allow_password_reset', true, $user_data->ID);
    
    	if ( ! $allow )
    		return new WP_Error('no_password_reset', __('Password reset is not allowed for this user'));
    	else if ( is_wp_error($allow) )
    		return $allow;
    
    	$key = $wpdb->get_var($wpdb->prepare("SELECT user_activation_key FROM $wpdb->users WHERE user_login = %s", $user_login));
    	if ( empty($key) ) {
    		// Generate something random for a key...
    		$key = wp_generate_password(20, false);
    		do_action('retrieve_password_key', $user_login, $key);
    		// Now insert the new md5 key into the db
    		$wpdb->update($wpdb->users, array('user_activation_key' => $key), array('user_login' => $user_login));
    	}
    	$message = __('Someone requested that the password be reset for the following account:') . "\r\n\r\n";
    	$message .= site_url( '/' ) . "\r\n\r\n";
    	$message .= sprintf(__('Username: %s'), $user_login) . "\r\n\r\n";
    	$message .= __('If this was a mistake, just ignore this email and nothing will happen.') . "\r\n\r\n";
    	$message .= __('To reset your password, visit the following address:') . "\r\n\r\n";
    	$message .= '<' . site_url("wp-login.php?action=rp&key=$key&login=" . rawurlencode($user_login), 'login') . ">\r\n";
    
    	if ( is_multisite() )
    		$blogname = $GLOBALS['current_site']->site_name;
    	else
    		// The blogname option is escaped with esc_html on the way into the database in sanitize_option
    		// we want to reverse this for the plain text arena of emails.
    		$blogname = wp_specialchars_decode(get_option('blogname'), ENT_QUOTES);
    
    	$title = sprintf( __('[%s] Password Reset'), $blogname );
    
    	$title = apply_filters('retrieve_password_title', $title);
    	$message = apply_filters('retrieve_password_message', $message, $key);
    
    	if ( $message && !wp_mail($user_email, $title, $message) )
    		wp_die( __('The e-mail could not be sent.') . "<br />\n" . __('Possible reason: your host may have disabled the mail() function...') );
    
    	return true;
    }
    
    ?>
Viewing 2 replies - 1 through 2 (of 2 total)
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Overwrite CORE function for wp_lostpassword_url and message retrieve_password’ is closed to new replies.