membership login widget redirect

Good day,

When I enter login credentials that doesn't exists (maybe typing error) the user is being redirected to the regular WP login page, instead of just getting a message like: Wrong username or password! At any time, I want the user to stay on the front-end.

Is there a way to stay on the front-end using the membership login widget if the user enters wrong login credentials?

Best regards,

Joeri Marquerinck.

  • Patrick

    Hi there @joeri1977

    Welcome to the forums, glad to have you aboard!

    I looked through the code in class.shortcodes.php in Membership where the login shortcode used by the widget is defined.

    As it simply echoes the wp_login_form in the widget, it will naturally redirect to wp-login.php if a user enters incorrect info.

    So a custom function would be required here to keep the user on the same page, and pop up an alert in that case.

    Unfortunately, I'm not quite sure how to get that done, but perhaps my colleague @Ashok would have an idea here. Let's see what he has to say. :slight_smile:

  • Ash

    Hello @joeri1977

    I hope you are well today.

    Please use the following code:

    add_action( 'wp_login_failed', 'my_front_end_login_fail' );
    
    function my_front_end_login_fail( $username ) {
       $referrer = $_SERVER['HTTP_REFERER'];
       $findme   = '?login=failed';
       $pos = strpos( $referrer, $findme );
    
       if ( !empty($referrer) && !strstr($referrer,'wp-login') && !strstr($referrer,'wp-admin') ) {
    	if( $pos === false ) {
    		wp_redirect( $referrer . '?login=failed' );
    	}
    	else
    		wp_redirect( $referrer );
    	exit;
       }
    }
    
    add_action( 'wp_footer', 'show_error_msg' );
    function show_error_msg() {
    	if( isset( $_REQUEST['login'] ) && $_REQUEST['login'] == 'failed' ) {
    	?>
    	<script type="text/javascript">
    	jQuery(function($){
    		$('.membershiploginwidget #loginform').prepend('<p style="color:red">Your username or password is wrong. Please try again.</p>');
    	});
    	</script>
    	<?php
    	}
    }

    You can add those codes in your functions.php in the theme, if you think your theme won’t be changed. Otherwise mu-plugins is the best solution. To use mu-plugins, go to /wp-content/ and find the folder with name 'mu-plugins'. If there is no folder in that name, then create a folder, name it 'mu-plugins', create a file inside that, give any name you like and paste the code in there. You don't need to activate that plugin. Mu-plugins means must use plugins, so it will be activated automatically always. If you use mu-plugins then add a php start tag at the beginning of the code.

    You can find the code at gist: https://gist.github.com/bappi-d-great/c65d5e040f77f572f991

    Hope it helps :slight_smile: Please feel free to ask more question if you have.

    Cheers
    Ash

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.