Notifications
Clear all

Script Redirects Not Working Properly

2 Posts
2 Users
0 Reactions
103 Views
Posts: 2
Topic starter
(@xavierrrrrr)
Active Member
Joined: 2 weeks ago

Hello guys I am trying to setup the forum on my site. I have some issues. I have woocomerce, wp foro and wp-login.php. These three have login methods. I have already worked out the way to redirect people to wp-login.php for woocomerce. but in wp foro there is only an option to add the cutom login and register page url. The issue with approach is users are not being redirected back to the page they were on.

I want users to be redirected back to the page they were one earlier:

function redirect_to_login_if_not_logged_in() {
    // Only apply redirect logic if the user is not logged in
    if ( ! is_user_logged_in() ) {
        // Capture the requested URL
        $redirect_to = esc_url($_SERVER['REQUEST_URI']);
        
        // Check for WPForo login/signup pages and redirect to WordPress login
        if ( is_page(array('sign-in', 'sign-up')) ) {
            wp_redirect( wp_login_url($redirect_to) );
            exit;
        }
    }
}
add_action('template_redirect', 'redirect_to_login_if_not_logged_in');

This is how I am trying to do but it don't seem to be working. This way the code is not redirecting properly.

Basic option given in plugin does not redirect users to their original page where they were.

1 Reply
Sofy
Posts: 4651
 Sofy
Admin
(@sofy)
Support Team
Joined: 7 years ago

Hi,

Please try this code: 

dd_filter( 'wpforo_login_url', function( $login_url ){
    if( wpforo_setting( 'authorization', 'login_url' ) ){
        $login_url .= wpforo_get_redirect_to_url_path();
    }
    
    return $login_url;
} );

add_filter( 'wpforo_register_url', function( $register_url ){
    if( wpforo_setting( 'authorization', 'register_url' ) ){
        $register_url .= wpforo_get_redirect_to_url_path();
    }
    
    return $register_url;
} );
Reply