Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
WordPress Multisite: Password Reset on a Subsite.
<?php
/**
* Plugin Name: Multisite: Password Reset on subsite
* Plugin URI: https://gist.github.com/eteubert/293e07a49f56f300ddbb
* Description: By default, WordPress Multisite uses main site of a network for password resets. This plugin enables users to stay in their subsite during the whole reset process.
* Version: 1.0.0
* Author: Eric Teubert
* Author URI: http://ericteubert.de
* License: MIT
*/
// fixes "Lost Password?" URLs on login page
add_filter("lostpassword_url", function ($url, $redirect) {
$args = array( 'action' => 'lostpassword' );
if ( !empty($redirect) )
$args['redirect_to'] = $redirect;
return add_query_arg( $args, site_url('wp-login.php') );
}, 10, 2);
// fixes other password reset related urls
add_filter( 'network_site_url', function($url, $path, $scheme) {
if (stripos($url, "action=lostpassword") !== false)
return site_url('wp-login.php?action=lostpassword', $scheme);
if (stripos($url, "action=resetpass") !== false)
return site_url('wp-login.php?action=resetpass', $scheme);
return $url;
}, 10, 3 );
// fixes URLs in email that goes out.
add_filter("retrieve_password_message", function ($message, $key) {
return str_replace(get_site_url(1), get_site_url(), $message);
}, 10, 2);
// fixes email title
add_filter("retrieve_password_title", function($title) {
return "[" . wp_specialchars_decode(get_option('blogname'), ENT_QUOTES) . "] Password Reset";
});
@tamasverdes

This comment has been minimized.

Copy link

commented Mar 29, 2018

Could you please help me? I would like to try this code. Where I need to put this file within the WP installation?

@nerdissimo-de

This comment has been minimized.

Copy link

commented Apr 13, 2018

You just need to put it in a folder and zip it. Simply install as a plugin afterwards. You can also put the code in your themes functions.php,

@raulgalaviz

This comment has been minimized.

Copy link

commented Aug 1, 2018

It works perfect. thank you very much.

@k4mrul

This comment has been minimized.

Copy link

commented Oct 2, 2018

Thanks. Works great

@mrjackyliang

This comment has been minimized.

Copy link

commented Oct 6, 2018

I fully audited the code with my IDE, and did some changes:

/**
 * 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' );

Please reply if you disagree with the changes that I found:

  • Added recommended curly braces.
  • Removed $path and $scheme from network_site_url filter, because it wasn't used.
  • Removed $key from retrieve_password_message, because it wasn't being used.
  • Removed $title from retrieve_password_title, because it wasn't being used.
  • Added $use_url to custom_lostpassword_url, because it was not being used, and is required for the second argument to work.
  • Allows the email title to be translated by WordPress.
@SmoothMC

This comment has been minimized.

Copy link

commented Mar 6, 2019

on my multisite installation this plugin doesn't work. I try it in different ways..per functions.php as a plugin and as mu-plugin. but no luck
please help

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.