• Resolved Matthias Honert

    (@matthias-honert)


    Your plugin looks very good and makes easy for front end registration/login. But I miss the function to redirect from the current page.
    My case is: If the user is on page X and logs in, he should return to page X.

    My current scenario:

    I use your shortcode [user_registration_login] on custom page called “login”.

    This function for login in the sidebar:
    wp_login_url( get_permalink() );

    I get back to the wp standard login page.
    Okay, I can handle this with add_filter. And I put this function to functions.php:

    if ( !function_exists('my_login_page') ) :
    	add_filter( 'login_url', 'my_login_page', 10, 3 );
    	function fa_login_page( $login_url, $redirect, $force_reauth ) {
    		$login_page = home_url( '/login/' );
    		if ( ! empty( $redirect ) ) {
    			$login_url = add_query_arg( 'redirect_to', urlencode( $redirect ), $login_page );
    		}
    		return $login_url;
    	}
    endif;

    Now I’m back on the right login page. I see in your form that the hidden field
    <input type="hidden" name="_wp_http_referer" value="/login/?redirect_to=http%3A%2F%2Ftestwebsite.local%2Fhello-world%2F">
    contains the value /login/?redirect_to=http%3A%2F%2Ftestwebsite.local%2Fhello-world%2F
    It should be without /login/?redirect_to=.

    How can I handle this or do you have any other way I can handle this?

    Best Matthias

Viewing 8 replies - 1 through 8 (of 8 total)
  • Hello Matthias,

    Can you please be more clear? I could not quite understand the scenario.

    If the user is on page X and logs in, he should return to page X.

    Yes. If you use the shortcode [user_registration_login], the user will not be redirected anywhere. If you would like to redirect, you can use the redirect_url parameter along with the shortcode [user_registration_login redirect_url=”sample-page”].

    Also, to let you know about that hidden field. That is generated automatically by the wp_nonce_field() and the value is always esc_attr( wp_unslash( $_SERVER['REQUEST_URI'] ) ), for details see wp_referer_field().

    The another hidden field you can see https://github.com/wpeverest/user-registration/blob/master/templates/myaccount/form-login.php#L73 will handle the redirect depending upon the redirect_url passed via the shortcode.

    Let me know if you have any further confusion.

    [ Signature deleted ]

    • This reply was modified 6 years, 3 months ago by Jan Dembowski.
    Thread Starter Matthias Honert

    (@matthias-honert)

    Hey sanzeeb3,

    thank you for your reply. Indeed my statement isn’t accurate. I’ll try again.
    My intention is: after successfully logging in, the user should be redirected to the last page where the user came from.
    The User visit the Post Hello World. On that page the user clicking the link login. After successfuly logging in the user returned to Hello World.

    Extended Version:

    1. The user is on Post Hello World
      https://test.local/hello-world/

    2. The user click the login link
      Reference: https://codex.www.ads-software.com/Function_Reference/wp_login_url
      For login and redirect to current page i use the permalink of current post hello-world
      <a href="<?php echo wp_login_url( get_permalink() ); ?>" title="Login">Login</a>

    3. This function redirects the user to the login page. and not to wp-login.php

      if ( !function_exists('my_login_page') ) :
      	add_filter( 'login_url', 'my_login_page', 10, 3 );
      	function fa_login_page( $login_url, $redirect, $force_reauth ) {
      		$login_page = home_url( '/login/' );
      		if ( ! empty( $redirect ) ) {
      			$login_url = add_query_arg( 'redirect_to', urlencode( $redirect ), $login_page );
      		}
      		return $login_url;
      	}
      endif;

      The URL in adress bar is:
      https://test.local/login/?redirect_to=https://test.local/hello-world/

    4. If the user is successfully logged in with your login form, the user will not be redirected to the last page.
      https://test.local/hello-world/

      Because the hidden field with the attribute name=”_wp_http_referer” contains in the value this piece, which blocks the successful redirecting to the old page /login/?redirect_to=

    I don’t know how to handle this redirection or do you have any other way I can handle this? Sorry for the long long explanation.

    Hello Matthias Honert,

    Thank you for the clear explaination. Since, you have passed the argument redirect_to with the login form. You can get this value and redirect accordingly. Assuming your login form url something like: https://example.com/login?redirect_to=sample-page

    add_filter( 'user_registration_login_redirect', 'ur_login_redirect', 10, 2 );
    
    function ur_login_redirect( $redirect, $user ) {
    	$redirect = isset( $_GET['redirect_to'] ) ? $_GET['redirect_to'] : wp_get_raw_referer();
    	return $redirect;
    }

    Let me know if you have any further confusion. BTW, Only to let you know, in your point no 3 my_login_page should’ve been fa_login_page or vice versa.

    Thank You!

    Thread Starter Matthias Honert

    (@matthias-honert)

    @sanzeeb3 thank you so much! Your filter function works! Now the user will be redirected.

    Yes and I forgot to rename “fa_login_page” to “my_login_page” only for this topic.

    dannyogolo

    (@dannyogolo)

    There are too many codes here to know which to implement or not.
    What is the final code to get the same done on a normal wp install?

    Hello, I believe my situation is similar but with a twist…

    We use User Registration as well as WP Ceber, a security plugin.

    WP Ceber blocks both the wp-admin folder as well as the wp-login.php file. They do this by blocking the wp-admin folder with a random text string (which we provide to the plugin) as well as a switch to block the wp-login.php file. This secures our site from multiple attacks hourly.

    The issue is the verification email provides the wp-login.php with a token to finalize the registration process. Replacing the ‘wp-login.php’ section of the URL with the random text string only provides a 404 error (expected when the url is not present in our system).

    Unfortunately, I am not savvy enough to come up with coding for the functions.php file but savvy enough to copy and paste it properly. Is there a code that will permit this workaround?

    The site is https://decisiveliberty.news

    Thank in advance – User Registration is awesome, planning on upgrading to pro once the financial support kicks in later this week.

    Thread Starter Matthias Honert

    (@matthias-honert)

    @decisiveliberty why don’t you use reCaptcha? This option is integrated in the plugin. It is in my opinion the better, more consistent solution than masking files with another plugin that break this plugin or functions. I think if you have attacs please contact your hosting service.

    Hi Matthias – we are using reCaptcha. Our aim is to keeo all users out of the backend. The security has us setup a string that is uses to redirect away from wp-admin.php. The issue comes up when anything is hard-coded with “wp-login.php”. We are replacing the string in the emails with ‘wp_login_url()’ instead and will be testing shortly.

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Redirect from custom login to current page’ is closed to new replies.