Notifications
Clear all

[Solved] How to prevent Guests from all access, and require login?

10 Posts
3 Users
0 Reactions
494 Views
Posts: 8
Topic starter
(@docdunning)
Active Member
Joined: 8 months ago

Hi - I'm just trying out wpForo, which looks good, and we shall probably be using it.

There's one thing I need a bit of help with, though - I only want logged-in users to be able to access the forums.  If a "Guest" tries to access the forum page ("/community"), they must see nothing at all, or maybe just a login page.

I can't see an easy option to do this in the plugin settings.  I understand that Accesses are used to manage permissions on each forum, rather than for all forums.

My alternative is to protect the "community" page itself, so that only logged-in users can see it.  Perhaps that's what I have to do - or is there a plugin setting that will do it?

9 Replies
Sofy
Posts: 4900
 Sofy
Admin
(@sofy)
Support Team
Joined: 7 years ago

Hi,

You can restrict accessfor guests by setting 'No Access' for 'Guest' usergroup. 

Please check out this FAQ to better understand the forum access and user group connections: https://wpforo.com/community/faq/wordpress-user-roles-wpforo-usergroups-and-forum-accesses/#post-39664

The solution provided by another community member should also be helpful for you: https://wpforo.com/community/how-to-and-troubleshooting-2/how-to-hide-forum-for-guests/#post-103677

Reply
Posts: 8
Topic starter
(@docdunning)
Active Member
Joined: 8 months ago

@sofy 

Thanks for that, and for the useful links.

My understanding is that I need to set "No Access" for "Guest" on every forum, and there is no single global setting for "Guests", to disallow all forums.  I can see that this would work.

I think however I will try protecting the "/community" page, so that it can only be reached by logged-in users.

Reply
4 Replies
dimalifragis
(@dimalifragis)
Joined: 5 years ago

Famed Member
Posts: 2611

Posted by: @docdunning

I think however I will try protecting the "/community" page, so that it can only be reached by logged-in users.

 

Nope, this is an open invitation for trouble.

 

Reply
(@docdunning)
Joined: 8 months ago

Active Member
Posts: 8

@dimalifragis 

OK, I appreciate the warning, although I'm not 100% sure what the problems will be. 

My requirement is this - if someone is not logged in, I don't want them to be aware that there is a forum, or to be able to access any forum pages / see member profiles etc.

The forum is exclusively for logged-in users.

What suggestions do you have for how this can be done?

Reply
dimalifragis
(@dimalifragis)
Joined: 5 years ago

Famed Member
Posts: 2611

Posted by: @docdunning

@dimalifragis 

OK, I appreciate the warning, although I'm not 100% sure what the problems will be. 

The problems will be that you try to control a page that wpForo controls. We have seen that happen here and the guys couldn't tell from where it was coming. wpForo or the block.

Anyways, you can try.

 

Reply
(@docdunning)
Joined: 8 months ago

Active Member
Posts: 8

@dimalifragis 

Thanks again.  I'm currently testing a solution using a custom theme per the docs at https://wpforo.com/docs/wpforo-v2/forum-themes/theme-customization/

By copying index.php into my own theme folder, I can add conditional logic that is used to hide some elements, and force the login form to be displayed, if the user is logged out.

Reply
Posts: 8
Topic starter
(@docdunning)
Active Member
Joined: 8 months ago

Just to add to this - I have been able to do what I needed, and block access to the forum for logged-out visitors.  I used the theme customization feature - as follows (in brief):

  • Create a wpforo directory in my theme folder
  • Copy \wp-content\plugins\wpforo\themes\2022\index.php into my wpforo directory
  • Add this code near the top of the copied file:
if (!is_user_logged_in()) {
    include __DIR__.'/my-login.php';
    return;
}
  • Copy \wp-content\plugins\wpforo\themes\2022\index.php into my wpforo directory again, but naming it "my-login.php"
  • Strip down "my-login.php" so that the only template it invokes is wpforo_template('login'), and remove the forum header / footer blocks.

I could have just made more changes to the original copy of index.php, but I wanted to keep the modifications there to a minimum.  It will be easier to copy again if I ever need to.

Reply
Posts: 8
Topic starter
(@docdunning)
Active Member
Joined: 8 months ago

Another update, in case it helps anyone else with this same need.  I've cut down on the customisation and used as much of WPForo as possible.

So, I create a "wpforo" folder in my own WP theme directory.  I add "index.php" to the "wpforo" folder, with this code, which overrides the WPF theme "index.php" file.

<?php
// Exit if accessed directly
if( ! defined( 'ABSPATH' ) ) exit;


if (!is_user_logged_in()) {
    $this->current_object['template'] = 'login';
    $this->settings->components['top_bar'] = false;
    $this->settings->components['footer'] = false;
    $this->settings->components['breadcrumb'] = false;
}


include $this->tpl->template_dir . '/index.php';
 
I first check for the user being logged in (a "guest").  If so, I tell WPF to use the "login" template, and switch off the top bar, footer and breadcrumb.  I can use $this to refer to the current instance of WPF - the plugin already set it up like that.
 
Then I include the original WPF "index.php" so that it renders the appropriate page.
 
This way, a guest user is not able to see any of the forum components except the login form, and the login form has none of the forum header/footer/breadcrumb elements.
 
Reply
1 Reply
(@docdunning)
Joined: 8 months ago

Active Member
Posts: 8

Oops - that code prevents a user from clicking the "Forgot your password" link 🙁

Of course, someone who is not logged-in must be able to access the login page and the lost password page.  So here's the code I'm using now, which doesn't override a request for the "lostpassword" template.

<?php
// Exit if accessed directly
if( ! defined( 'ABSPATH' ) ) exit;

if (!is_user_logged_in()) {
    if ($this->current_object['template'] != 'lostpassword') {
        $this->current_object['template'] = 'login';
    }
    $this->settings->components['top_bar'] = false;
    $this->settings->components['footer'] = false;
    $this->settings->components['breadcrumb'] = false;
}

include $this->tpl->template_dir . '/index.php';

 

Reply