Only allow access when user has a subscription plan on Memberpress

My client has a Wordpress installation with Memberpress which is the place a user logs in in order to access Discourse.

Is there a way to only allow users access to Discourse if they have an active Memberpress plan?

Thanks
Per

Have a look at my first reply to this topic for details about how to restrict logins to Discourse: How to prevent some WP users from being able to login to Discourse. This is assuming that the WordPress site is functioning as the SSO provider for Discourse and that you are using the WP Discourse plugin.

1 Like

Thanks Simon, thats awesome! :slight_smile:

That allows me to check access before routing the user to Discourse.
Do you happen to know if there is a good way to check if a user has an active subscription?
This would be the condition on which I would choose to route them to Discourse or not.

Thanks!!

I’m sure that there is a way to do it, but I’m not sure how to access a user’s Memberpress subscription level on WordPress. It’s possible that there are examples of how to do that in posts in our #support:wordpress category though.

ok thank you :slight_smile:
have a great weekend!

1 Like

if anyone else is trying to achieve the same, I found the solution here:

Simon: one last question: for how long is a user logged into Discourse before being logged out? or is the user logged in forever until they click the “logout” button?

This is controlled by the Discourse maximum session age site setting. That setting defaults to 1440 hours. Unless it is set to a very small value, it is not a reliable way to log out users. That is because it sets the amount of time that a user will remain logged in since their last visit. Setting maximum session age to a very small number of hours could also cause some annoyance for your site users.

I think your best approach would be to log users out of Discourse through the API when memberships expire. There is a code example of how to do that here: https://github.com/discourse/wp-discourse/blob/master/lib/sso-provider/discourse-sso.php#L205. You might need to get some help from a developer to make an API call to log users out of Discourse when their membership expires.

1 Like

Can I just call the function logout_from_discourse() or do I have to copy all that code?

It’s not as simple as just calling the logout_from_discourse() function, but it should be possible to use a WordPress action hook to call that function when a specific action is called on your WordPress site. For example, when a user’s membership level is revoked. Possibly someone in the Discourse community can help you with the syntax that is needed to make that work.

1 Like