Last active
June 28, 2016 15:44
-
-
Save greathmaster/1e6a7b54981d621f2ece to your computer and use it in GitHub Desktop.
Email Inactive Users
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
add_action( 'init', 'register_daily_email_inactive_members_email'); | |
// Function which will register the event | |
function register_daily_email_inactive_members_email() | |
{ | |
// Make sure this event hasn't been scheduled | |
if( !wp_next_scheduled( 'email_inactive_members' ) ) { | |
// Schedule the event | |
wp_schedule_event( time(), 'daily', 'email_inactive_members' ); | |
} | |
} | |
add_action('email_inactive_members', 'my_email_inactive_members'); | |
function my_email_inactive_members() | |
{ | |
//get the inactive members | |
$max_inactive_time = 30*24*60*60; //30 days in seconds | |
//$max_inactive_time = 3600; //one hour for testing purposes | |
$args = array( | |
'meta_query' => array( | |
'relation' => 'AND', | |
array( | |
'key' => 'last_login', | |
'value' => current_time('timestamp') - $max_inactive_time, | |
'compare' => '<' | |
), | |
array( | |
'key' => 'last_login_reminder_sent', | |
'value' => 1, | |
'compare' => '<' | |
), | |
)); | |
$inactive_user_query = new WP_User_Query($args); | |
$inactive_members = $inactive_user_query->get_results(); | |
foreach($inactive_members as $inactive_member) | |
{ | |
wp_mail($inactive_member->data->user_email, "Come back!", "Please come back and visit us! "); | |
//make sure we don't keep sending them emails! | |
update_user_meta($inactive_member->ID, 'last_login_reminder_sent', 1); | |
} | |
} | |
//associating a function to login hook | |
add_action('wp_login', 'set_last_login'); | |
//function for setting the last login | |
function set_last_login($login) | |
{ | |
$user = get_user_by('login', $login); | |
//add or update the last login value for logged in user | |
update_user_meta( $user->ID, 'last_login', current_time('timestamp')); | |
//if they login, reset the reminder | |
update_user_meta($user->ID, 'last_login_reminder_sent', 0); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment