基于这答案,我为前3次登录的用户创建了一个小功能,使某些元素类使用css动画。我要做的是调整这段代码,以计数和控制除订阅者以外的前3次all用户角色。
基本上,我需要这个功能来开始计算用户角色的第一个登录。让我们说,除了订阅者之外,第一次出现贡献者登录或编辑器,或者更好的all用户角色时
这是到目前为止我的代码
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 '.create-post {animation: pulse-blue 2s 7;} .bb-header-icon.logged-in-user.element-toggle.only-mobile img{animation: pulse-red 2s 7;} ';
} else {
echo '.create-post {animation: none;} .bb-header-icon.logged-in-user.element-toggle.only-mobile img{animation: none;} ';
}
}
}我如何操作track_user_logins函数来计算除订阅者之外的所有角色的登录时间。现有函数计算所有用户。
用户第一次以角色订阅者在网站注册。经过一些登录时间(比如说7-8),他的角色就变成了贡献者。从第一次作为贡献者登录时,track_user_logins应该开始计数。
任何想法都会很高兴的。
发布于 2020-08-27 12:33:08
您已经在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 );
}
}
}发布于 2020-08-28 14:30:30
让它发挥作用。正确的条件是
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_can( $ user ->id ),'edit_posts‘可以检查登录用户的角色。
https://wordpress.stackexchange.com/questions/373847
复制相似问题