# 仅当用户在Memberpress拥有订阅计划时才允许访问

**URL:** https://meta.discourse.org/t/only-allow-access-when-user-has-a-subscription-plan-on-memberpress/188716
**Category:** WordPress
**Created:** [2021年四月30日 15:39 UTC](https://meta.discourse.org/t/only-allow-access-when-user-has-a-subscription-plan-on-memberpress/188716 "2021-04-30T15:39:32Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![Per\_Jacobsson1](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/per_jacobsson1/32/202417_2.png) [@Per\_Jacobsson1](https://meta.discourse.org/u/Per_Jacobsson1)
#### Post date: [2021年四月30日 15:39 UTC](https://meta.discourse.org/t/only-allow-access-when-user-has-a-subscription-plan-on-memberpress/188716/1 "2021-04-30T15:39:32Z")

</div>

我的客户使用 WordPress 安装并集成了 MemberPress，用户需在此登录以访问 Discourse。

是否有办法仅允许拥有活跃 MemberPress 计划的用户访问 Discourse？

谢谢，  
Per

---

<div class="post-metadata">

### Author: ![simon](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/simon/32/339122_2.png) [@simon](https://meta.discourse.org/u/simon)
#### Post date: [2021年四月30日 16:24 UTC](https://meta.discourse.org/t/only-allow-access-when-user-has-a-subscription-plan-on-memberpress/188716/2 "2021-04-30T16:24:21Z")

</div>

> [@Per\_Jacobsson1](#):
>
> 是否有办法仅允许拥有有效 Memberpress 计划的用户访问 Discourse？

请参阅我对该主题的首条回复，了解如何限制 Discourse 登录的详细信息：[https://meta.discourse.org/t/how-to-prevent-some-wp-users-from-being-able-to-login-to-discourse/93234。这假设](https://meta.discourse.org/t/how-to-prevent-some-wp-users-from-being-able-to-login-to-discourse/93234%E3%80%82%E8%BF%99%E5%81%87%E8%AE%BE) WordPress 站点作为 Discourse 的 SSO 提供商，并且您正在使用 [WP Discourse](https://github.com/discourse/wp-discourse) 插件。

---

<div class="post-metadata">

### Author: ![Per\_Jacobsson1](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/per_jacobsson1/32/202417_2.png) [@Per\_Jacobsson1](https://meta.discourse.org/u/Per_Jacobsson1)
#### Post date: [2021年四月30日 16:28 UTC](https://meta.discourse.org/t/only-allow-access-when-user-has-a-subscription-plan-on-memberpress/188716/3 "2021-04-30T16:28:26Z")

</div>

谢谢 Simon，太棒了！🙂

这样我就可以在将用户路由到 Discourse 之前检查其访问权限。  
请问你知道有没有好的方法可以检查用户是否拥有有效的订阅？  
这将是我决定是否将用户路由到 Discourse 的条件。

谢谢！！

---

<div class="post-metadata">

### Author: ![simon](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/simon/32/339122_2.png) [@simon](https://meta.discourse.org/u/simon)
#### Post date: [2021年四月30日 16:31 UTC](https://meta.discourse.org/t/only-allow-access-when-user-has-a-subscription-plan-on-memberpress/188716/4 "2021-04-30T16:31:59Z")

</div>

> [@Per\_Jacobsson1](#):
>
> 您是否知道有什么好方法可以检查用户是否有有效订阅？

我相信肯定有办法实现，但我不确定如何在 WordPress 中访问用户的 Memberpress 订阅等级。不过，我们的 #Support > WordPress 分类下的帖子中可能有相关示例。

---

<div class="post-metadata">

### Author: ![Per\_Jacobsson1](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/per_jacobsson1/32/202417_2.png) [@Per\_Jacobsson1](https://meta.discourse.org/u/Per_Jacobsson1)
#### Post date: [2021年四月30日 16:37 UTC](https://meta.discourse.org/t/only-allow-access-when-user-has-a-subscription-plan-on-memberpress/188716/5 "2021-04-30T16:37:51Z")

</div>

好的，谢谢 🙂  
祝您周末愉快！

---

<div class="post-metadata">

### Author: ![Per\_Jacobsson1](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/per_jacobsson1/32/202417_2.png) [@Per\_Jacobsson1](https://meta.discourse.org/u/Per_Jacobsson1)
#### Post date: [2021年四月30日 16:57 UTC](https://meta.discourse.org/t/only-allow-access-when-user-has-a-subscription-plan-on-memberpress/188716/6 "2021-04-30T16:57:02Z")

</div>

如果其他人也想实现同样的功能，我找到了解决方案：

> <https://stackoverflow.com/questions/29744672/how-can-i-detect-if-user-has-subscribed-memberpress-product-already>

Simon：最后一个问题：用户在 Discourse 中登录状态会持续多久？是直到点击“退出登录”按钮之前一直登录，还是会在一段时间后自动登出？

---

<div class="post-metadata">

### Author: ![simon](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/simon/32/339122_2.png) [@simon](https://meta.discourse.org/u/simon)
#### Post date: [2021年四月30日 20:26 UTC](https://meta.discourse.org/t/only-allow-access-when-user-has-a-subscription-plan-on-memberpress/188716/7 "2021-04-30T20:26:12Z")

</div>

> [@Per\_Jacobsson1](#):
>
> 用户在 Discourse 中保持登录状态多久后会被登出？

这由 Discourse 的“最大会话时长”（maximum session age）站点设置控制。该设置的默认值为 1440 小时。除非将其设置为非常小的值，否则它并不是登出用户的可靠方式。这是因为它设定的是用户自上次访问以来保持登录状态的时间。将“最大会话时长”设置为极小的数值也可能给您的网站用户带来不便。

我认为最好的方法是在会员资格到期时通过 API 将用户从 Discourse 登出。这里有相关的代码示例：[wp-discourse/lib/sso-provider/discourse-sso.php at main · discourse/wp-discourse · GitHub](https://github.com/discourse/wp-discourse/blob/master/lib/sso-provider/discourse-sso.php#L205%E3%80%82%E6%82%A8%E5%8F%AF%E8%83%BD%E9%9C%80%E8%A6%81%E5%AF%BB%E6%B1%82%E5%BC%80%E5%8F%91%E4%BA%BA%E5%91%98%E7%9A%84%E5%B8%AE%E5%8A%A9%EF%BC%8C%E4%BB%A5%E4%BE%BF%E5%9C%A8%E4%BC%9A%E5%91%98%E8%B5%84%E6%A0%BC%E5%88%B0%E6%9C%9F%E6%97%B6%E8%B0%83%E7%94%A8) API 将用户从 Discourse 登出。

---

<div class="post-metadata">

### Author: ![Per\_Jacobsson1](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/per_jacobsson1/32/202417_2.png) [@Per\_Jacobsson1](https://meta.discourse.org/u/Per_Jacobsson1)
#### Post date: [2021年四月30日 20:38 UTC](https://meta.discourse.org/t/only-allow-access-when-user-has-a-subscription-plan-on-memberpress/188716/8 "2021-04-30T20:38:01Z")

</div>

我可以直接调用 logout\_from\_discourse() 函数，还是必须复制所有那段代码？

---

<div class="post-metadata">

### Author: ![simon](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/simon/32/339122_2.png) [@simon](https://meta.discourse.org/u/simon)
#### Post date: [2021年四月30日 20:50 UTC](https://meta.discourse.org/t/only-allow-access-when-user-has-a-subscription-plan-on-memberpress/188716/9 "2021-04-30T20:50:55Z")

</div>

这并不只是简单地调用 `logout_from_discourse()` 函数，但你可以通过 WordPress 的动作钩子（action hook），在 WordPress 站点触发特定操作（例如用户会员等级被撤销）时调用该函数。Discourse 社区中可能有人能帮助你实现这一功能所需的语法。
