Skip to content

Instantly share code, notes, and snippets.

@Mte90
Created December 3, 2019 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Mte90/84cb47df2bd45b31631daf3afd57b3eb to your computer and use it in GitHub Desktop.
Save Mte90/84cb47df2bd45b31631daf3afd57b3eb to your computer and use it in GitHub Desktop.
Send email to user if not logged after 3 weeks
<?php
/**
* Plugin Name: Alert Last Login
* Plugin URI:
* Description:
* Version: 1.0.0
* Author: Codeat
* Author URI: http://codeat.it
* License: GPL-2.0+
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt
*/
function user_last_login( $user_login, $user ) {
update_user_meta( $user->ID, '_last_login', time() );
}
register_activation_hook( __FILE__, 'last_login_activate' );
register_deactivation_hook( __FILE__, 'last_login_deactivate' );
function last_login_activate() {
//Use wp_next_scheduled to check if the event is already scheduled
$timestamp = wp_next_scheduled( 'check_users_last_login' );
//If $timestamp == false schedule
if ( $timestamp == false ) {
wp_schedule_event( time(), 'daily', 'check_users_last_login' );
}
}
function last_login_deactivate() {
wp_clear_scheduled_hook( 'check_users_last_login' );
}
function execute_plugin() {
add_action( 'wp_login', 'user_last_login', 10, 2 );
add_action( 'check_users_last_login', 'send_mail_login' );
//send_mail_login();
}
add_action( 'plugins_loaded', 'execute_plugin' );
function send_mail_login() {
$months_ago = strtotime( '-3 weeks', time() );
echo $months_ago . '-' . time();
$users = get_users( array(
'meta_key' => '_last_login',
'meta_value' => $months_ago,
'meta_compare' => '<',
) );
foreach ( $users as $key => $user ) {
$user = $user->data;
wp_mail( 'admin@localhost.dev', 'User '.$user->user_login.' not logged since 3 weeks', 'Visit our website ' . $user->user_login );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment