Skip to content

Instantly share code, notes, and snippets.

@lukecav
Forked from eteubert/wordpress-passwort-reset-unmultisite.php
Last active November 15, 2023 18:36
Show Gist options
  • Star 15 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save lukecav/8531e0859eae028e3c989f0f396441a9 to your computer and use it in GitHub Desktop.
Save lukecav/8531e0859eae028e3c989f0f396441a9 to your computer and use it in GitHub Desktop.
WordPress Multisite: Password Reset on a Subsite.
/**
* Password reset on sub site (1 of 4)
* Replace login page "Lost Password?" urls.
*
* @param string $lostpassword_url The URL for retrieving a lost password.
* @param string $redirect The path to redirect to.
*
* @return string
*
* @since 1.0.0
*/
function custom_lostpassword_url( $lostpassword_url, $redirect ) {
$use_url = false;
$args = array( 'action' => 'lostpassword' );
if ( ! empty( $redirect ) ) {
$args['redirect_to'] = $redirect;
}
if ( $use_url ) {
return esc_url( add_query_arg( $args, $lostpassword_url ) );
}
return esc_url( add_query_arg( $args, site_url( 'wp-login.php' ) ) );
}
add_filter( 'lostpassword_url', 'custom_lostpassword_url', 10, 2 );
/**
* Password reset on sub site (2 of 4)
* Replace other "unknown" password reset urls.
*
* @param string $url The complete network site URL including scheme and path.
*
* @return string
*
* @since 1.0.0
*/
function replace_lostpassword_urls( $url ) {
if ( stripos( $url, 'action=lostpassword' ) !== false ) {
return site_url( 'wp-login.php?action=lostpassword' );
}
if ( stripos( $url, 'action=resetpass' ) !== false ) {
return site_url( 'wp-login.php?action=resetpass' );
}
return $url;
}
add_filter( 'network_site_url', 'replace_lostpassword_urls', 10, 3 );
/**
* Password reset on sub site (3 of 4)
* Fixes the URLs in emails that are sent.
*
* @param string $message Default mail message.
*
* @return string
*
* @since 1.0.0
*/
function retrieve_password_message_urls( $message ) {
return str_replace( get_site_url( 1 ), get_site_url(), $message );
}
add_filter( 'retrieve_password_message', 'retrieve_password_message_urls' );
/**
* Password reset on sub site (4 of 4)
* Fixes the title in emails that are sent.
*
* @return string
*
* @since 1.0.0
*/
function custom_retrieve_password_title() {
return sprintf( __( '[%s] Password Reset' ), wp_specialchars_decode( get_option( 'blogname' ), ENT_QUOTES ) );
}
add_filter( 'retrieve_password_title', 'custom_retrieve_password_title' );
@ronaldoscotti
Copy link

Thank you!

@sl18eu
Copy link

sl18eu commented Feb 8, 2021

Thank you very much. Works as intended on my multisite.

@arisahagun
Copy link

arisahagun commented Sep 9, 2022

Still working good in 2022. Added to my child theme's functions.php file with mrjackyliang's changes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment