Notifications
Clear all

Script Redirects Not Working Properly

5 Posts
3 Users
0 Reactions
148 Views
Posts: 2
Topic starter
(@xavierrrrrr)
Active Member
Joined: 4 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.

4 Replies
Sofy
Posts: 4682
 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
Posts: 31
(@moptop)
Trusted Member
Joined: 5 years ago

I am very interested in this code as I am using a custom URL for my login page due to a membership plugin.

Can you confirm where I should add this code to allow the redirect to work on this page?

Thanks!

Reply
2 Replies
Sofy
 Sofy
Admin
(@sofy)
Joined: 7 years ago

Support Team
Posts: 4682

@moptop 

You should insert it in the active theme functions.php file.

This article should be helpful for you: https://www.wpbeginner.com/plugins/how-to-easily-add-custom-code-in-wordpress-without-breaking-your-site/

Reply
(@moptop)
Joined: 5 years ago

Trusted Member
Posts: 31

@sofy 

Thank you for the clarification. I added the code using WPCode as a PHP snippet, but unfortunately it does not seem to be working. When I paste in a forum URL like:

https://site.com/community/buy-and-sell/

My custom login screen appears, but the redirect URL is still the default I set in wpForo settings

https://site.com/membership-login/?redirect_to=/community/

It does not add the additional URL after community. I'm sure there is something I am doing wrong or missing.

I am also getting an error in WPcode when I deactivate and then reactivate the snippet.

Snippet not activated, the following error was encountered: Call to undefined function dd_filter()

Your further help would be appreciated.

Reply