Skip to:
Content
Pages
Categories
Search
Top
Bottom

Hook for when a user first logs in


  • ndh01
    Participant

    @ndh01

    Is anyone aware of a hook that I can use when a user logs in for the first time?

    Thank you

Viewing 19 replies - 1 through 19 (of 19 total)

  • Henry Wright
    Moderator

    @henrywright

    Hi @ndh01

    I’m not aware of a hook that fires on first login only. A way around that might be to add a meta key to the user’s meta on registration and then check for that on each login attempt. You’d need to remove the key after you’ve performed the check (if the key exists). If no key exists, then the user has logged in before. Just thinking out loud here… 🙂


    shanebp
    Moderator

    @shanebp

    You might be able to use the login_redirect filter hook.
    And then check to see if a last_activity entry exists for the user.
    If not, then it’s their first login.
    Untested:

    function ndh_first_login_check( $redirect_to, $not_used, $user ) {
      if( bp_get_user_last_activity( $user->ID ) == '' )
        echo ' first login '; 
    
        return $redirect_to;
    }
    add_filter( 'login_redirect', 'ndh_first_login_check', 20, 3 );

    ndh01
    Participant

    @ndh01

    @shanebp I haven’t tested yet. There is an activity entry for ‘new_member’ that gets created. Not sure if that is when they first login or or first activate. Unfortunately, that entry might already be there when I check. Will take a look.


    ndh01
    Participant

    @ndh01

    Looks like I need to create new meta because the new_member activity is created when the account is activated.

    Thanks for your help!


    valuser
    Participant

    @valuser

    Hi @ndh01,

    I think you could do this just with buddypress.

    (just please backup database before….. etc)

    Go to Profile Fields—> (Click) Add New Field Group

    Add New Field (text) say Field Name virgin

    Then add the following to your functions.php

    function vub_login_redirect($redirect_to, $set_for, $user){
    
    	global $current_user, $bp, $wpdb;
    	$val = 'no';
    	if (bp_get_profile_field_data (virgin, $user -> ID )!=""){
    	$redirect_to = bp_core_get_user_domain($user->ID) .'/groups/';	
    }else{
    	xprofile_set_field_data( 'virgin', $user -> ID, $val);
    	$redirect_to = bp_core_get_user_domain($user->ID) .'/profile/change-avatar/';
    }
    
    	wp_redirect( $redirect_to );
    	exit();
    
    }
    
    add_action('login_redirect', 'vub_login_redirect', 20, 3);

    Alternatively you could as you have suggested create a new meta.

    A useful plugin for that would be User Meta Manager

    Add Custom Meta say a text field Name — firsttimer

    then add the following (instead of the above) to your functions.php

    function vu_login_redirect($redirect_to, $set_for, $user){
    
    	global $current_user, $wpdb;
    	$firsttimer = get_usermeta($user -> ID, 'firsttimer',single);
    	$val = 'no';
    	if (!empty ($firsttimer)) {	
    	$redirect_to = bp_core_get_user_domain($user->ID) .'/groups/';
    }else{
    	update_user_meta( $user -> ID,'firsttimer',$val);
    	$redirect_to = bp_core_get_user_domain($user->ID) .'/profile/change-avatar/';
    }
    	
    	wp_redirect( $redirect_to );
    	exit();
    
    }
    
    add_action('login_redirect', 'vu_login_redirect', 20, 3);

    Obviously change the redirects to wherever you want the user to go.


    valuser
    Participant

    @valuser

    Apology!

    first suggestion should have read:

    function vub_login_redirect($redirect_to, $set_for, $user){
    
    	global $current_user, $wpdb;
    	$val = 'no';
    	$var = xprofile_get_field_data (virgin, $user -> ID);
    	if ($var != "") {
    	$redirect_to = bp_core_get_user_domain($user->ID) .'/groups/';	
    }else{
    	xprofile_set_field_data( 'virgin', $user -> ID, $val);
    	$redirect_to = bp_core_get_user_domain($user->ID) .'/profile/change-avatar/';
    }
    
    	wp_redirect( $redirect_to );
    	exit();
    
    }
    
    add_action('login_redirect', 'vub_login_redirect', 20, 3);

    Henry Wright
    Moderator

    @henrywright

    @ndh01

    I haven’t tested yet. There is an activity entry for ‘new_member’ that gets created. Not sure if that is when they first login or or first activate.

    new_member is an activity type. It’s created when a user activates their account (at the point at which the bp_core_activated_user hook fires).


    Henry Wright
    Moderator

    @henrywright

    Here’s my stab at it, in actual code (demonstrating what I mean by my rambling above):

    function my_add_meta( $user_id, $key, $user ) {
        add_user_meta( $user_id, 'first_login', 'not_yet' );
    }
    add_action( 'bp_core_activated_user', 'my_add_meta', 10, 3 );
    
    function my_check_meta( $redirect_to, $redirect_to_raw, $user ) {
        $meta = get_user_meta( $user->ID, 'first_login', true );
        if ( $meta == '' ) {
            // The user has logged in before
        } else {
            // The user is logging in for the first time.
            // Do something here.
            delete_user_meta( $user->ID, 'first_login' );
        }
    }
    add_filter( 'bp_login_redirect', 'my_check_meta', 10, 3 );

    References

    https://codex.wordpress.org/Function_Reference/add_user_meta
    https://codex.wordpress.org/Function_Reference/get_user_meta
    https://codex.wordpress.org/Function_Reference/delete_user_meta


    ndh01
    Participant

    @ndh01

    Thanks guys!


    valuser
    Participant

    @valuser

    @henrywright Works! Now that is elegant!


    Henry Wright
    Moderator

    @henrywright

    @shanebp’s approach is probably the simplist. Did anyone manage to test?


    ndh01
    Participant

    @ndh01

    Henry,

    Yes, I tested but it didn’t work because the new_member activity is created when the user is activated. So if( bp_get_user_last_activity( $user->ID ) == ” ) is never true when they log in.


    Henry Wright
    Moderator

    @henrywright

    @ndh01 what does new_member have to do with bp_get_user_last_activity()?


    valuser
    Participant

    @valuser

    Just to clarify.

    I have now tested @shanebp code. (as per sample below)

    It appears to me to work perfectly and would appear, as per @henrywright, to be the simplest solution.

    last_activity gets created on login – not before – not on activation.

    function sbp_first_login_check( $redirect_to, $not_used, $user ) {
      if( bp_get_user_last_activity( $user->ID ) !== '' ) {
      $redirect_to = bp_core_get_user_domain($user->ID) .'/groups/';
     }else{
      $redirect_to = bp_core_get_user_domain($user->ID) .'/profile/change-avatar/';
    }
        wp_redirect( $redirect_to );
        exit();
    }
    add_filter( 'login_redirect', 'sbp_first_login_check', 20, 3 );

    ndh01
    Participant

    @ndh01

    It doesn’t work for me, perhaps because I’m using gravity forms for user registration.


    Henry Wright
    Moderator

    @henrywright

    @ndh01

    It doesn’t work for me, perhaps because I’m using gravity forms for user registration.

    That could be why. There’s no reason @shanebp’s solution won’t work on a default BuddyPress install


    shanebp
    Moderator

    @shanebp

    @valuser Thanks for sharing the function.
    One issue re ‘proper’ WP coding – add_filter should always return something, so…

    
    function sbp_first_login_check( $redirect_to, $not_used, $user ) {
    
      if( bp_get_user_last_activity( $user->ID ) !== '' ) 
           $redirect_to = bp_core_get_user_domain($user->ID) .'/groups/';
      else
           $redirect_to = bp_core_get_user_domain($user->ID) .'/profile/change-avatar/';
    
     return $redirect_to;
    
    }
    add_filter( 'login_redirect', 'sbp_first_login_check', 20, 3 );

    valuser
    Participant

    @valuser

    Bril.

    Thats great cos i’d imagine this topic will be of interest to many bp users and will be searched for.

    Having this shown/done properly on the forum is particularly useful and this is, if i may so, the way forums should work !!!

    Pidgin code and get out jail solutions (that may work) put up by rank amateurs (like me) should ideally always be corrected.

    In fairness, most are.

    Thanks!

    (Sermon over)


    Henry Wright
    Moderator

    @henrywright

    @valuser

    I’m with you on the good practice thing, always good to keep an eye on that when using or writing code

Viewing 19 replies - 1 through 19 (of 19 total)
  • The topic ‘Hook for when a user first logs in’ is closed to new replies.
Skip to toolbar