Notifications
Clear all

[Solved] Show login page when not logged in, but community page when logged in

6 Posts
2 Users
1 Reactions
154 Views
Posts: 7
Topic starter
(@isitjustme)
Active Member
Joined: 4 weeks ago

I've searched the forums but can't find this specific issue I'm having, apologies if I've missed something obvious.

I am using Ultimate Member for registration and login, all working ok.  I have disabled guest access using the recommended method so users need to be registered and logged in to view the forums, also all working ok.

The issue I have is I can't work out which URL to link to from our main website menu so that if a user is not logged in they see the UM login page (/login), but if they are already logged in they go straight to the /community page.

If going to /community and not logged in, all they see is the forum title, header and footer (no login page).

If going to /login when already logged in, they see their profile name and a couple of links (account and logout).

Is there something clever I can do with redirections to achieve the desired result?


5 Replies
Posts: 7
Topic starter
(@isitjustme)
Active Member
Joined: 4 weeks ago
Reply
4 Replies
Sofy
 Sofy
Admin
(@sofy)
Joined: 8 years ago

Support Team
Posts: 5523

@isitjustme 

If guests should not see any forums, please use this hook code:

add_filter('wpforo_after_init_current_object', function ($current_object){
    if( in_array($current_object['template'], array('forum', 'topic')) ){
        if( !WPF()->current_userid ){
	        wp_redirect( wpforo_login_url() );
	        exit();
        }
    }
    return $current_object;
});

Reply
(@isitjustme)
Joined: 4 weeks ago

Active Member
Posts: 7

@sofy I've added this to wpforo/themes/classic/functions.php, is that correct?  (I am using the classic theme).

It doesn't appear to be working.  When I am not logged in and go to /community/ it does not redirect to the login page.  I assume that is what it is supposed to do?


Reply
Sofy
 Sofy
Admin
(@sofy)
Joined: 8 years ago

Support Team
Posts: 5523

@isitjustme 

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

Helpful article: https://www.wpbeginner.com/plugins/how-to-easily-add-custom-code-in-wordpress-without-breaking-your-site/


Reply
(@isitjustme)
Joined: 4 weeks ago

Active Member
Posts: 7

@sofy Thank you so much, I now have it working.


Reply