0

Based on this answer i created a small functionality for users that are logging in for the first 3 times to make some elements classes use css animation. What i am trying to do is to adjust this code to count and control the first 3 times of all user roles except subscribers.

Basically i need this functionality to start counting the first login by user role. Lets say the first time a contributor login, or an editor, or better all user roles except subscribers

Here is my code so far

add_action( 'wp_login', 'track_user_logins', 10, 2 );
function track_user_logins( $user_login, $user ){
    if( $login_amount = get_user_meta( $user->id, 'login_amount', true ) ){
        // They've Logged In Before, increment existing total by 1
        update_user_meta( $user->id, 'login_amount', ++$login_amount );
    } else {
        // First Login, set it to 1
        update_user_meta( $user->id, 'login_amount', 1 );
    }
}

add_action('wp_head', 'notificationcss');
function notificationcss{
    if ( !current_user_can('subscriber') ) {
        // Get current total amount of logins (should be at least 1)
        $login_amount = get_user_meta( get_current_user_id(), 'login_amount', true );

        // return content based on how many times they've logged in.
        if( $login_amount <= 3 ){
            echo '<style>.create-post {animation: pulse-blue 2s 7;} .bb-header-icon.logged-in-user.element-toggle.only-mobile img{animation: pulse-red 2s 7;} </style>';
        } else {
            echo '<style>.create-post {animation: none;} .bb-header-icon.logged-in-user.element-toggle.only-mobile img{animation: none;} </style>';
        }
    }
}

How could i manipulate the track_user_logins function to count the login times for all roles except subscribers. The existing function counts all users.

Example: A user is registered in the site for the first time with role Subscriber. After some login times(lets say 7-8), his role is being changes to Contributor. From that first time logged in as a Contributor the track_user_logins should start counting.

Any ideas would be gladly appreciated.

4
  • You have if ( !current_user_can('subscriber') ) { in your code. Does that not work? Aug 27, 2020 at 12:16
  • @JacobPeattie thanks for the reply. This if is just for keeping the css classes with animation for all non subscriber roles. But doesn't actually ''count'' the login times a non subscriber has been logged in. So what i need is to change the track_user_logins function to count accordingly to user role. Aug 27, 2020 at 12:24
  • Can't you just add the same condition? Aug 27, 2020 at 12:27
  • @JacobPeattie not really sure how Aug 27, 2020 at 12:31

2 Answers 2

0

You've already got the condition you need in your notificationcss() function, so just add the exact same code to track_user_logins():

function track_user_logins( $user_login, $user ){
    if ( !current_user_can('subscriber') ) {
        if( $login_amount = get_user_meta( $user->id, 'login_amount', true ) ){
            // They've Logged In Before, increment existing total by 1
            update_user_meta( $user->id, 'login_amount', ++$login_amount );
        } else {
            // First Login, set it to 1
            update_user_meta( $user->id, 'login_amount', 1 );
        }
    }
}
3
  • thanks @JacobPeattie i will try that and give a feedback Aug 27, 2020 at 12:37
  • i tested but doesn't seems to work. i made the following example. A user is registered in the site for the first time with role Subscriber. After some login times(lets say 7-8), his role is being changed to Contributor. From that first time logged in as a Contributor i should get an animation response on those classes. For the first, second and third loggin time. I dont get the animation. Although this seems to work for already existing 'non subscriber' users, it didn't work for a new user. Any idea? Aug 27, 2020 at 13:45
  • as it seems it doesn't actually start counting for all 'non subscriber' users. I made another test. Created a user with Subscriber default user role. 1 attempt to login as Subscriber, -switched roles- 2nd attempt to login as 'Contributor' , can see the animation. 3d attempt to login as 'Contributor', can see the animation. 4d attempt to login as 'Contributor', can not see the animation. (But i should be able to see it since i set $login_amount <= 3) Aug 27, 2020 at 14:17
0

Made it working. The correct condition would be

add_action( 'wp_login', 'track_user_logins', 10, 2 );
function track_user_logins( $user_login, $user ){
    if ( user_can( $user->id, 'edit_posts' )) {
        if( $login_amount = get_user_meta( $user->id, 'login_amount', true ) ){
            // They've Logged In Before, increment existing total by 1
            update_user_meta( $user->id, 'login_amount', ++$login_amount );
        } else {
            // First Login, set it to 1
            update_user_meta( $user->id, 'login_amount', 1 );
        }
    }
}

with user_can( $user->id, 'edit_posts' ) can check the role of the logged in user.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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