Instantly share code, notes, and snippets.

Embed
What would you like to do?
Multisite: Passwort Reset on Local Blog
<?php
/**
* Plugin Name: Multisite: Passwort Reset on Local Blog
* Plugin URI: https://gist.github.com/eteubert/293e07a49f56f300ddbb
* Description: By default, WordPress Multisite uses the main blog for passwort resets. This plugin enables users to stay in their blog 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";
});
@Kellylise

This comment has been minimized.

Kellylise commented Jun 30, 2015

This is brilliant! Thanks very much.

@jdozierezell

This comment has been minimized.

jdozierezell commented Jul 10, 2015

This is great. Solves so many hassles and works right out of the box. Thanks!

@Surbma

This comment has been minimized.

Surbma commented Aug 24, 2015

Works great, as expected. I hope WordPress will fix it soon, so we won't need these fixes anymore!

Thank you @eteubert!

@ghost

This comment has been minimized.

ghost commented Sep 10, 2015

Can't believe wpmu doesn't support this already. Thanks a ton!

@LouisHoude

This comment has been minimized.

LouisHoude commented Nov 9, 2015

Thanks a lot! Work out of the box.

@GoGross

This comment has been minimized.

GoGross commented Nov 26, 2015

Here is a new error that I am getting:

Your password reset link appears to be invalid. Please request a new link below.
@dipaknuveda

This comment has been minimized.

dipaknuveda commented Dec 30, 2015

You saved my day buddy. Working just perfect. Thanks :)

@tylercollier

This comment has been minimized.

tylercollier commented Jan 7, 2016

Awesome!

It still shows the Network Title in the email subject, like this: "[My Network Title] Password Reset". Do you know how to change it so it uses my site's title, and not the network's?

@LyndiLeighWK

This comment has been minimized.

LyndiLeighWK commented Jan 22, 2016

Works great - thank you!!

@edlefebvre

This comment has been minimized.

edlefebvre commented Jul 25, 2016

Hi, thanks for this!

For those who install WordPress in a separate folder (https://codex.wordpress.org/Giving_WordPress_Its_Own_Directory), you should use:
return str_replace(network_site_url(), get_site_url().'/', $message);
instead of return str_replace(get_site_url(1), get_site_url(), $message); (line 37)

@csisson

This comment has been minimized.

csisson commented Feb 22, 2017

WP is throwing a parse error after trying to activate the plugin: "syntax error, unexpected T_FUNCTION on line 13".

Using WP Multiuser 4.7.2.

UPDATE Mar 7, 2017 - Bug solved

Added this line to my htaccess file:

AddHandler application/x-httpd-php55 php

@jonpasquier

This comment has been minimized.

jonpasquier commented Aug 30, 2017

Works like a charm, thanks!

WP multisite 4.8.1

@13pixlar

This comment has been minimized.

13pixlar commented Sep 26, 2017

You should replace the hard coded site id on line 37 get_site_url(1) with get_network()->site_id to make:

return str_replace(get_site_url(get_network()->site_id), get_site_url(), $message);

in case the main site is not id 1

@cooperhoward

This comment has been minimized.

cooperhoward commented Oct 12, 2017

This works great on the "lost password" link underneath the log in details, however, if they type in an incorrect password... the error box shows with the link saying... forgotten your password... click "lost your password" to change it. This link (that appears in the error box) is still taking them back to the main site and not the sub-site that was intended... any thoughts?

@mehranhzh

This comment has been minimized.

mehranhzh commented Jan 13, 2018

It works without this plugin but having redirect to main site, it means reset password function works fine without using this plugin but to solve url redirection to related subdomain I'd like to use this plugins so after activating the plugin, I'm getting the following error:
The email could not be sent.
Possible reason: your host may have disabled the mail() function.

PHP 7.0.25
Wordpress multi site version: 4.9.1-0
Server: Debian 9
Mail: SendGrid

Any idea how to fix this?

@blue928

This comment has been minimized.

blue928 commented Jun 1, 2018

Hello. Great plugin, but I can confirm mehranhzh's issue. As of the current WP version 4.9.6, something seems to have changed in how Wordpress works that breaks its "drop in and go" functionality. Mail is correctly setup sending email with no problem until I activate this plugin. I get the same error:

The email could not be sent.
Possible reason: your host may have disabled the mail() function.

Does this error reference the PHP mail() function? wp_mail() function? Or is it talking about linux mail command?

Thanks!

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