检查第一次登录wordpress

Jul*_*e k 4 wordpress

我需要创建这样的东西:

如果是第一次登录显示:[something]

如果是第二次登录显示:[something2]

如果是第三次登录显示:[something3]

因为每次登录时我都需要在帖子中显示不同的消息,这可能吗?

Xhy*_*ynk 7

这当然是可行的,您可以尝试跟踪 cookie,但从长远来看,这会变得非常乏味和不准确。

您可能希望使用update_user_meta()绑定到wp_login用户登录后触发的钩子的函数来跟踪和增加自定义用户元字段。

此外,您需要阅读该add_shortcode()函数以输出您想要的内容,但这样的事情足以让您入门。它会跟踪他们登录的次数以及您放置的任何位置[login_content]- 它会根据$login_amount.

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_shortcode( 'login_content', 'login_content' );
function login_content( $atts ){
    if( is_user_logged_in() ){
        // 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 == 1 ){
            return 'Welcome, this is your first time here!';
        } else if( $login_amount == 2 ){
            return 'Welcome back, second timer!';
        } else if( $login_amount == 3 ){
            return 'Welcome back, third timer!';
        } else {
            return "Geez, you have logged in a lot, $login_amount times in fact...";
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

你应该能够把它放在你的functions.php文件中。