Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

share|improve this question
add comment

2 Answers

up vote 3 down vote accepted

here's an example that hooks login_redirect and checks when their account was created, then redirects them to a url of your choice if it's been less than 48 hours:

function my_redirect( $to, $requested, $user ){
    if( !isset( $user->user_login ) ){ // we only want this to run when credentials have been supplied
        return $to;
    }
    $regtime = strtotime($user->user_registered);
    $now = strtotime("now");
    $diff = $now - $regtime;
    $hours = $diff / 60 / 60;
    if( $hours < 48 ){
        return "/somepage/"; // it's been less than 48 hours, redirect to message.
    } else {
        return admin_url();
    }
}
add_filter('login_redirect', 'my_redirect', 10, 3);
share|improve this answer
    
Excellent, it works great with the standard wp-login.php. However, I need to get it to work with Eric Martin's simple modal login. Are you familiar with it? –  Zach Shallbetter Aug 1 '11 at 16:29
    
I was able to get this working with Eric's Simple Modal Login. I'll update my post with my modifications. I probably need to change this to a hook. Also, I would like to make this a pluggable piece of code so I can force modals on certain pages for welcome info. Any idea how I can do that? –  Zach Shallbetter Aug 1 '11 at 22:09
    
Any thoughts on this Milo? –  Zach Shallbetter Aug 3 '11 at 20:18
add comment

You can use log-user-access plugin to log all user accesses.

Then, select the top row from each "login_id" from the plugin table sorted by date and time. That will be the user first login. With this data, you should be able to figure out the rest.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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