wp_lostpassword_url()WP 2.8.0

Returns the URL that allows the user to retrieve the lost password

Hooks from the function

Return

String. Lost password URL.

Usage

wp_lostpassword_url( $redirect );
$redirect(string)
Path to redirect to on login.
Default: ''

Examples

0

#1 Basic use:

<a href="<?php echo esc_url( wp_lostpassword_url() ); ?>">Forgot your password?</a>

Restore password with switching to the current page:

<a href="<?php echo esc_url( wp_lostpassword_url( get_permalink() ) ); ?>">Forgotten your password?</a>

Password recovery with switching to the home page:

<a href="<?php echo esc_url( wp_lostpassword_url( home_url() ) ); ?>">Forgotten your password?</a>
0

#2 Change the page for resetting the password with the hook

Let's say we created our password recovery page: /getpassword and now we need to change all the password recovery links on the site. This can be done quickly with the hook lostpassword_url:

add_filter( 'lostpassword_url', 'change_lostpassword_url', 10, 2 );

function change_lostpassword_url( $url, $redirect ){

	$new_url = home_url( '/getpassword' );

	return add_query_arg( array('redirect'=>$redirect), $new_url );
}

// Get it: http://example.com/getpassword?redirect=URL

Changelog

Since 2.8.0 Introduced.

wp_lostpassword_url() code WP 6.5.2

function wp_lostpassword_url( $redirect = '' ) {
	$args = array(
		'action' => 'lostpassword',
	);

	if ( ! empty( $redirect ) ) {
		$args['redirect_to'] = urlencode( $redirect );
	}

	if ( is_multisite() ) {
		$blog_details  = get_site();
		$wp_login_path = $blog_details->path . 'wp-login.php';
	} else {
		$wp_login_path = 'wp-login.php';
	}

	$lostpassword_url = add_query_arg( $args, network_site_url( $wp_login_path, 'login' ) );

	/**
	 * Filters the Lost Password URL.
	 *
	 * @since 2.8.0
	 *
	 * @param string $lostpassword_url The lost password page URL.
	 * @param string $redirect         The path to redirect to on login.
	 */
	return apply_filters( 'lostpassword_url', $lostpassword_url, $redirect );
}