Limit content by user registration date

I have just made this code you can try out:

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;
}

Leave a Comment