如何限制用户每24小时只能登录一次?在WordPress中

时间:2019-05-12 07:37:47

标签: wordpress woocommerce

我正试图限制内容的使用。我需要实现的是,一旦用户注销,他们将无法在接下来的24小时内登录。或者,如果那不可行,我该怎么做,用户只能在24小时内登录一次?使用WordPress

1 个答案:

答案 0 :(得分:0)

您可以通过为配置文件添加一个last_login_timestamp的自定义字段来解决这种情况(即,当用户注册时)。基本上,每当用户注销时,您都可以使用wp_logout挂钩来更新此自定义字段。像

function slug_update_user_last_login_hook() {
    if(! is_admin()) { // obviously not for admin
      // logic to update custom form field for user
    }
}
add_action('wp_logout', 'slug_update_user_last_login_hook');

当用户登录时,您可以检查上次登录时间是否小于或等于24小时。

function slug_24_hour_login_check() {
        if(! is_admin()) { // obviously not for admin
          //compare the the time of custom form field of logged in user's id
          //if not you can logout user and redirect to another page using wp_redirect
          wp_redirect( $url_of_24_hour_login_notice );
        }
}
add_action('wp_login', 'slug_24_hour_login_check');

让我知道它是如何工作的。