首页 > 解决方案 > 如何获取用户上次登录信息并将电子邮件发送到上次登录提醒?

问题描述

我想让系统向非活动用户发送电子邮件警报,该用户在过去 7 天或 1 个月内没有记录它。我怎样才能做到这一点?

    // user last login reminder
function user_last_login( $user_login, $user ) {
 update_user_meta( $user->ID, 'last_login', time() );
}
add_action( 'wp_login', 'user_last_login', 10, 2 );
if( ! wp_next_scheduled( 'inactivity_reminder' ) ) {
 wp_schedule_event( time(), 'daily', 'inactivity_reminder' );
}
function inactivity_reminder() {
 $users = new WP_User_Query( [
   'role'         => ['student', 'author'],
   'meta_key'     => 'last_login',
   'meta_value'   => strtotime( '7days' ),
   'meta_compare' => '<',
 ] );
 foreach( $users->get_results() as $user ) {
   $to = $user->user_email;
   $subject = "Inactivity Notice";
   $message = "ALERT.......... We notice you have not logged in for 20 days.";
   wp_mail( $to, $subject, $message);
 }
}
add_action( 'inactivity_reminder', 'inactivity_reminder' );

// user last login reminder
function user_last_login( $user_login, $user ) {
 update_user_meta( $user->ID, 'last_login', time() );
}
add_action( 'wp_login', 'user_last_login', 10, 2 );
if( ! wp_next_scheduled( 'inactivity_reminder' ) ) {
 wp_schedule_event( time(), 'daily', 'inactivity_reminder' );
}
function inactivity_reminder() {
 $users = new WP_User_Query( [
   'role'         => ['student', 'author'],
   'meta_key'     => 'last_login',
   'meta_value'   => strtotime( '7days' ),
   'meta_compare' => '<',
 ] );
 foreach( $users->get_results() as $user ) {
   $to = $user->user_email;
   $subject = "Inactivity Notice";
   $message = "ALERT.......... We notice you have not logged in for 20 days.";
   wp_mail( $to, $subject, $message);
 }
}
add_action( 'inactivity_reminder', 'inactivity_reminder' );

我尝试使用上面的代码但没有奏效。我检查了用户邮件,1周后没有警报按摩。

标签: phpwordpress

解决方案


推荐阅读