1

I'd like to know if it's possible to display a page only for users that registered before a specific date. Example : I want to display a page (or its content) only for users registered before 1st February 2016.

  • Do you mean show something different, or have a page that can only be accessed by logged in users that registered before that date? – kraftner Feb 29 '16 at 15:23
  • Best is only be accessed by logged in users that registered before that date. – Guenfood Feb 29 '16 at 15:25
4

This is a basic function to get the current user and compare it with a custom DateTime to see if the user registered earlier or later:

function wpse_219284_registered_before( $date_time ) {

    $user = wp_get_current_user();

    $registered_before = false;

    if ( $user instanceof WP_User ) {
        $registered = new DateTime( $user->data->user_registered );
        $limit = new DateTime( $date_time );
        return $registered < $limit;
    }

    return $registered_before;
}

Be aware that this defaults to false for e.g. unregistered users.

You can e.g. use it in your templates like this:

if ( wpse_219284_registeredBefore( '2015-06-25' ) ) {
    the_content();
} else {
    echo "Registered too late to read this. The early bird catches the worm.";
}
| improve this answer | |
  • 1
    Hope you don't mind the edit. wp_get_current_user() relies on global $current_user, which gets sets inside wp_set_current_user(), but relies on get_currentuserinfo(), which can return FALSE or simply NULL. On top of it, it is filterable, so the only reliable way is to check for an instanceof \WP_User. Also you might want to set the return value to FALSE instead of returning the date as else it might be TRUE in any case ;) – kaiser Feb 29 '16 at 16:34
  • Thanks for clearing up the global issue. I don't get the part with returning date though. – kraftner Feb 29 '16 at 16:37
  • Ah, sorry. That was me messing up $date_time and $registered_before. – kaiser Feb 29 '16 at 16:53
  • Thanks for your answer. My database prefix is different that wp. Must i change something in the code ? – Guenfood Mar 1 '16 at 12:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.