I've been trying to decipher a way to display a message to a user on first login and can't seem to get it to work. I'm not sure what I'm missing, or if there's something else I need to do. Any help would be appreciated. I feel like I missing something here.
function ap_new_user_message() {
$current_user = wp_get_current_user();
$user_ID = $current_user->ID;
//global $user_ID;
if( $user_ID ) {
$user_info = get_userdata( $user_ID );
// If user_registered date/time is less than 48hrs from now
// Message will show for 48hrs after registration
if ( strtotime( $user_info->user_registered ) > ( time() - 172800 ) )
echo '<div>display a message</div>';
}
}
Alternatively, If I could find a way of determining how many days it's been since a user was created I could do something similar. Perhaps by adding to the user meta table?
function first_login($login) {
global $user_ID;
$user = get_userdatabylogin($login);
update_usermeta( $user->ID, 'first_login', date(), time() );
}
add_action('wp_login','first_login');
And then calling for it
if first_login(date, time) == (today, ago) {
#do something
}
Update: Milo's code worked great. However, I needed it to work with Eric Meyer's Simple Modal Login. I found this function within simplemodal-login.php and peter's login redirect.
function login_redirect($redirect_to, $req_redirect_to, $user) {
if (!isset($user->user_login) || !$this->is_ajax()) {
return $redirect_to;
}
if ($this->is_plugin_active('peters-login-redirect/wplogin_redirect.php')
&& function_exists('redirect_to_front_page')) {
$redirect_to = redirect_to_front_page($redirect_to, $req_redirect_to, $user);
}
echo "<div id='simplemodal-login-redirect'>$redirect_to</div>";
exit();
}
and added Milo's redirect to it like so:
function login_redirect($redirect_to, $req_redirect_to, $user) {
$regtime = strtotime($user->user_registered);
$now = strtotime("now");
$diff = $now - $regtime;
$hours = $diff / 60 / 60;
if (!isset($user->user_login) || !$this->is_ajax()) {
return $redirect_to;
}
if ($this->is_plugin_active('peters-login-redirect/wplogin_redirect.php')&& function_exists('redirect_to_front_page')) {
$redirect_to = redirect_to_front_page($redirect_to, $req_redirect_to, $user);
}
if( $hours < 48 ){
$redirect_to = "/somepage/"; // it's been less than 48 hours, redirect to message.
}
echo "<div id='simplemodal-login-redirect'>$redirect_to</div>";
exit();
}
Now I just need to determine how to force a model popup when a users reaches a certain page.