Tweak your buddypress login form to redirect to the page user was viewing while login

If you are using buddypress, I am sure In most of the  cases you are going to have a login form on all the pages(I know It depends on theme ,but most of them have it in this way)

When a user logs in from an arbitrary page,what happens,If the login is successful,the user is redirected to site home page.Is it the behavior a user expects .Should It be considered a default behavior.

Of course not, If I am logging from a page, I want to be on that page after login.

So, here is a small tweak to make it happen.

Have a look at the code where the login form is put.

In case of default theme it is in bp-sn-parent/header.php

you will see something like this there

<form name="login-form" id="login-form" action="<?php echo site_url( 'wp-login.php' ) ?>" method="post">
<input type="text" name="log" id="user_login" value="<?php _e( 'Username', 'buddypress' ) ?>" onfocus="if (this.value == '<?php _e( 'Username', 'buddypress' ) ?>') {this.value = '';}" onblur="if (this.value == '') {this.value = '<?php _e( 'Username', 'buddypress' ) ?>';}" />
<input type="password" name="pwd" id="user_pass" value="" />
<input type="checkbox" name="rememberme" id="rememberme" value="forever" title="<?php _e( 'Remember Me', 'buddypress' ) ?>" />
<input type="submit" name="wp-submit" id="wp-submit" value="<?php _e( 'Log In', 'buddypress' ) ?>"/>
<?php if ( 'none' != bp_get_signup_allowed() &amp;amp;amp;amp;amp;&amp;amp;amp;amp;amp; 'blog' != bp_get_signup_allowed() ) : ?>
<input type="button" name="signup-submit" id="signup-submit" value="<?php _e( 'Sign Up', 'buddypress' ) ?>" onclick="location.href='<?php echo bp_signup_page() ?>'" />
<?php endif; ?>
<input type="hidden" name="redirect_to" value="<?php echo bp_root_domain() ?>" />
<input type="hidden" name="testcookie" value="1" />
<?php do_action( 'bp_login_bar_logged_out' ) ?>

So ,where to redirect is controlled by the line

<input type="hidden" name="redirect_to" value="<?php echo bp_root_domain() ?>" />

It is set to be redirected to the buddypress root site(you see the function bp_root_domain() )

Now let us make a small change there

replace the above one line in form code with this one

<input type="hidden" name="redirect_to" value="<?php echo $_SERVER['REQUEST_URI'];?>" />

Great, save the changes, upload to your server and yeh, try logging in from profile page or any other page.

It will redirect you back there.

Just another step towards better user experience :)

I will love to hear from you, did it work for you or not ?

Copy link
Powered by Social Snap