如何使访客用户在登录1.5小时后过期?

时间:2020-07-07 作者:Victor Vazquez

我想预先创建一个用户表1-200,然后在第一次登录后

编辑以进一步解释(&H);描述:我试图在第一次登录后的90分钟内为来宾用户发出对所有页面的访问权限。

我想创建N个来宾用户。每个用户首次登录后;用户可以在90分钟内自由浏览网页。时间结束后,可以删除/更改用户密码或其他更容易的操作

提前谢谢。

1 个回复
SO网友:ericek111

在您的single.php 模板文件:

<?php
if (has_post()) {
    the_post();

    $user = wp_get_current_user();
    $hasPermission = in_array(\'subscriber\', $user->roles);
    // or maybe instead of a role, you can check for your custom permission:
    // $hasPermission = current_user_can(\'view_access_codes\');

    $postTime = get_post_time(\'U\', true);
    $timeThreshold = time() - 60 * 60 * 1.5;
    $hasTimePassed = $postTime < $timeThreshold;

    if (!$hasPermission && $hasTimePassed) {
        status_header(\\WP_Http::FORBIDDEN);
        ?>
        Only registered users can view this page.
        <?php
    } else {
        // print the post
    }
}
或使用PHPDateTime object:

$postDate = new \\DateTime(get_post_time(DATE_W3C));
$postDate->modify(\'+90 minutes\');
$hasTimePassed = $postDate < new \\DateTime();

请注意,该帖子仍然可以通过其他方式访问,例如the REST API 或者你的RSS feed. 对于这些,我建议使用the_content filter.