Redirecting User to Previous Page After Registration/Login

Posted in: Newspaper
Post count: 153

Hello developers,

I am trying to run a forum at my website, but there were difficulties with BBPress. Initially, i didn’t want to abandon Mobile Theme (super speed) for the sake of forum. I had previously asked questions about BBP compatibility but there were no solutions. However, i have made a simple solution: by directing new users (unlogged) to register/login page using the following code:

/**
* Redirect bbPress pages to registration page
*/
function kleo_page_template_redirect()
{
//if not logged in and on a bp page except registration or activation
if( ! is_user_logged_in() && is_bbpress() ) {
wp_redirect( home_url( '/register/' ) );
exit();
}
}
add_action( 'template_redirect', 'kleo_page_template_redirect' );

Problem? The new users are stuck on the same register/login page. I have been trying to find a solution to redirect users to the same topic, but haven’t so far. For now, m relying on Peter’s Redirect Plugin to redirect user to forum main page, but that is just “harassing” users.

If there’s any solution to that, will be highly obliged. It will also help me set up a new thread for Beginners Forum Guide.

Regards
Dr.Varun

Post count: 20688

Hi,

You could try to modify the function, try to redirect users to the previous page after registering. I found these somewhat similar topic that you can follow
https://wordpress.stackexchange.com/questions/16004/redirect-user-to-original-url-after-login
https://buddypress.org/support/topic/how-to-redirect-after-registration/#post-249621

Thanks

Post count: 153

Thanks a ton @simion . I was finally able to code the redirect after a lot of hit and trials. The following code has functionality to redirect users trying to access only bbPress Pages to Wp-Login page, and once they login using id or social profile, they will be redirected to the page they were trying to access.

Note: Make sure to paste the below code to Theme Functions.php , both main and mobile functions.php to make it work on desktop and mobile devices.

/**
* Redirect bbPress pages to registration page
*/
function redirect_login_page() {
$refer=urlencode($_SERVER["REQUEST_URI"]);
$isItExt = strpos(strtok($_SERVER["REQUEST_URI"],'?'), 'external');
if ( ! is_user_logged_in() && is_bbpress()) {
wp_redirect( 'https://medicforyou.in/wp-login.php' . '?redirect_to=' . $refer );
exit();
}
}
add_action( 'template_redirect', 'redirect_login_page' );

function redirect_after_login() {
global $redirect_to;
if (!isset($_GET['redirect_to'])) {
$redirect_to = get_option('siteurl');
}
}
add_action('login_form', 'redirect_after_login');

function redirect_login(){
wp_redirect( 'https://medicforyou.in/wp-login.php' );
exit();
}
add_action('wp_logout','redirect_login');

Post count: 20688

Glad you could solve it, also thank you for posting the solution. It may be helpful for other users.

Viewing 4 posts - 1 through 4 (of 4 total)
The forum ‘Newspaper’ is closed to new topics and replies.