我试图在WordPress中显示用户的最后一次登录,但该函数捕获了最新的登录,而我更喜欢在最新的登录时间之前显示登录。
我的代码片段如下所示。希望有人能带我出去。谢谢!
public static function last_login( $user_login, $users ){
global $show_login_records;
//get/update user meta 'when_last_login' on login and add time() to it.
update_user_meta( $users->ID, 'when_last_login', time() );
//get and update user meta 'when_last_login_count' on login for # of login counts. Thanks to Jarryd Long (@jarrydlong) from Code Cabin (@code_cabin) for the assistance
$wll_count = get_user_meta( $users->ID, 'when_last_login_count', true );
if( $wll_count === false ){
update_user_meta($users->ID, 'when_last_login_count', 1);
} else {
$wll_new_value = intval($wll_count);
$wll_new_value = $wll_new_value + 1;
update_user_meta($users->ID, 'when_last_login_count', $wll_new_value);
}
if( $show_login_records == true ){
$args = array(
'post_title' => $users->data->display_name . __( ' has logged in at ', 'when-last-login' ) . date( 'Y-m-d H:i:s', current_time( 'timestamp' ) ),
'post_status' => 'publish',
'post_author' => $users->ID,
'post_type' => 'wll_records'
);
$post_id = wp_insert_post( $args );
}
$wll_settings = get_option( 'wll_settings' );
if( isset( $wll_settings['record_ip_address'] ) && $wll_settings['record_ip_address'] == 1 ){
// call function to anonymize here.
$ip = When_Last_Login::wll_get_user_ip_address();
if ( ! empty( $post_id ) ) {
update_post_meta( $post_id, 'wll_user_ip_address', $ip );
}
update_user_meta( $users->ID, 'wll_user_ip_address', $ip );
}
do_action( 'wll_logged_in_action', array( 'login_count' => $wll_new_value, 'user' => $users ), $wll_settings );
}
发布于 2018-08-06 20:38:34
这可能会对你有帮助。它与两个用户meta的current_login, last_login.Lets一起工作,请参阅代码。
//function for setting the last login
function set_last_login($login) {
$user = get_userdatabylogin($login);
$curent_login_time = get_user_meta( $user->ID , 'current_login', true);
//add or update the last login value for logged in user
if(!empty($curent_login_time)){
update_usermeta( $user->ID, 'last_login', $curent_login_time );
update_usermeta( $user->ID, 'current_login', current_time('mysql') );
}else {
update_usermeta( $user->ID, 'current_login', current_time('mysql') );
update_usermeta( $user->ID, 'last_login', current_time('mysql') );
}
}这将帮助您保存当前和上次登录时间。您可以编写一个get last time函数来获取上次登录时间。这是一篇关于上次登录Click here的文章
发布于 2018-08-07 18:10:20
我使用带有两个自定义字段的ACF来检查登录时间的值。使用下面的代码,我设法通过短码输出
/**
* Display last login time
*
*/
add_action('wp_login', 'set_last_login');
//function for setting the last login
function set_last_login($login) {
$user = get_userdatabylogin($login);
$curent_login_time = get_user_meta( $user->ID , 'current_login', true);
//add or update the last login value for logged in user
if(!empty($curent_login_time)){
update_usermeta( $user->ID, 'last_login', $curent_login_time );
update_usermeta( $user->ID, 'current_login', current_time('mysql') );
}else {
update_usermeta( $user->ID, 'current_login', current_time('mysql') );
update_usermeta( $user->ID, 'last_login', current_time('mysql') );
}
}
function lastlogin() {
$atts = shortcode_atts(
array(
'user_id' => get_current_user_id(),
'format' => get_option( 'date_format' ).' '.get_option( 'time_format' )
), $atts, 'current_login' );
$author_id = get_current_user_id();
$last_login = get_field('last_login', 'user_'. $author_id);
$offset = get_option( 'gmt_offset' ) * HOUR_IN_SECONDS;
return $last_login;
}
/**
* Add Shortcode lastlogin
*
*/
add_shortcode('lastlogin','lastlogin');
https://stackoverflow.com/questions/51707239
复制相似问题