Problem redirecting to forum after login when using MemberMouse

Thanks for doing this! I installed the Membermouse plugin in my development environment today. Getting the redirect back to Discourse is harder than I expected. I don’t think there’s any reason to change the way you are doing it, but here’s an alternate approach. It at least shows how the plugin expects things to work. Generally, it should be possible to do this by hooking into the WordPress login_redirect filter and getting the Discourse redirect_to parameter from that filter’s $request parameter. This can’t be done with Membermouse though.

This function depends on being able to get the HTTP_REFERER with wp_get_referer. The referer may not always be available. If it isn’t, the user will be redirected to the default Membermouse redirect. I have only tested this in my development environment.

add_filter( 'mm_login_redirect', 'wpdc_login_redirect_override' );
function wpdc_login_redirect_override() {
    $referer = wp_get_referer();
    if ( $referer ) {
	    $query_params = [];
	    parse_str( parse_url( $referer, PHP_URL_QUERY ), $query_params );
	    if ( isset( $query_params['redirect_to'] ) ) {

		    return home_url( $query_params['redirect_to'] );
	    }
    }

	return '';
}
5 Likes