按用户注册日期限制内容

时间:2016-06-11 作者:Bernie

我正在寻找一种代码,它允许我根据用户注册日期限制对内容的访问。因此,如果用户注册after content has already been posted 然后用户将not be able to view the content. 只有在用户注册后才可以查看帖子内容。

1 个回复
SO网友:Kim Vinberg

我刚刚编写了以下代码,您可以试用:

add_filter( \'the_content\', \'restrict_access\' );
function restrict_access( $content ) { 

    $user_info = wp_get_current_user(); // Get logged in user info
    $registered = $user_info->user_registered;

    if( !is_user_logged_in() ) {

        $content = __( "You are not logged in.", \'your_textdomain\' );

    } else if (new DateTime( get_the_date() ) < new DateTime( $registered )) {

        $content = __( "You are not allowed to view this content. Your user was registered ( ".date( "d-m-Y", strtotime( $registered ) )." ) after content was created ( ".get_the_date( "d-m-Y" )." ) , you are only allowed to view new content.", \'your_textdomain\' );

    }

    return $content;
}

相关推荐

UPDATE_TERM_META()在某些元键上只更新一次

我在自定义分类法上设置了4个自定义元字段,出于某种原因,我的2个文本字段的字段值仅在第一次添加到数据库时更新。将其包括在;添加(&Q);或者如果我创建了一个没有它们的分类法,然后将它们包含在;“编辑”;第一次形成。但只要我尝试更改这些值,数据库就不会得到更新。我做错了什么?这是我的代码:// Add the fields to the "groups" taxonomy, using our callback function add_action( \'groups_ad