检查特定角色的首次登录

时间:2020-08-27 作者:Honoluluman

基于this 答案我为前3次登录的用户创建了一个小功能,使一些元素类使用css动画。我试图做的是调整此代码,以计数和控制all user roles 除了subscribers.

基本上,我需要这个功能来开始按用户角色统计第一次登录的次数。让我们假设参与者第一次登录,或者编辑器,或者更好all user roles 除了subscribers

这是我目前的代码

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>\';
        }
    }
}
如何操作track\\u user\\u logins函数来计算除订阅者之外的所有角色的登录时间。现有函数统计所有用户。

示例:用户第一次在站点中使用角色Subscriber注册。在登录几次之后(比如7-8),他的角色将被更改为贡献者。从第一次以参与者身份登录开始,track\\u用户登录应该开始计数

任何想法都将不胜感激。

2 个回复
SO网友:Jacob Peattie

你已经具备了notificationcss() 函数,所以只需将完全相同的代码添加到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 );
        }
    }
}

SO网友:Honoluluman

让它发挥作用。正确的条件是

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 );
        }
    }
}
使用user\\u can($user->;id,\'edit\\u posts\')可以检查登录用户的角色。