Limit content by user registration date

I am looking for a code that will allow me to restrict access to content based on a User Registration Date. Therefore, if a user registers after content has already been posted then the user will not be able to view the content. Access to view post content will only be available if the post is made after the user is already registered.

1 Answer
1

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