Created
December 20, 2011 10:45
-
-
Save eteubert/1501175 to your computer and use it in GitHub Desktop.
WordPress: Find Users by Last Login Activity
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
<?php | |
register_activation_hook( __FILE__, 'add_last_activity_for_all_users' ); | |
function add_last_activity_for_all_users() { | |
global $wpdb; | |
$sql = $wpdb->prepare( " | |
SELECT | |
u.ID | |
FROM | |
$wpdb->users AS u | |
LEFT JOIN $wpdb->usermeta m ON u.ID = m.user_id AND m.meta_key = 'last_activity' | |
WHERE | |
m.meta_value IS NULL" ); | |
$userids = $wpdb->get_col( $sql ); | |
if ( $userids ) { | |
foreach ( $userids as $userid ) { | |
update_user_meta( $userid, 'last_activity', current_time( 'mysql' ) ); | |
} | |
} | |
} |
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
<?php | |
/* | |
Plugin Name: User Activity | |
Plugin URI: | |
Description: Provides means to query users by last login activity. | |
Version: 1.0 | |
Author: Eric Teubert | |
Author URI: [email protected] | |
License: MIT | |
*/ | |
namespace UserActivity; | |
add_action( 'wp_login' , '\UserActivity\add_login_time' ); | |
function add_login_time( $user_login ) { | |
global $wpdb; | |
$sql = $wpdb->prepare( "SELECT ID FROM $wpdb->users WHERE user_login = %s", $user_login ); | |
$user_id = $wpdb->get_var( $sql ); | |
update_user_meta( $user_id, 'last_activity', current_time( 'mysql' ) ); | |
} | |
register_activation_hook( __FILE__, '\UserActivity\add_last_activity_for_all_users' ); | |
function add_last_activity_for_all_users() { | |
global $wpdb; | |
$sql = $wpdb->prepare( " | |
SELECT | |
u.ID | |
FROM | |
$wpdb->users AS u | |
LEFT JOIN $wpdb->usermeta m ON u.ID = m.user_id AND m.meta_key = 'last_activity' | |
WHERE | |
m.meta_value IS NULL" ); | |
$userids = $wpdb->get_col( $sql ); | |
if ( $userids ) { | |
foreach ( $userids as $userid ) { | |
update_user_meta( $userid, 'last_activity', current_time( 'mysql' ) ); | |
} | |
} | |
} | |
/** | |
* Find user ids from user who were inactive for a certain amount of days. | |
* | |
* @param int $days | |
* @return array list of user ids. | |
*/ | |
function find_inactive_user_ids( $days = 30 ) { | |
global $wpdb; | |
$query = $wpdb->prepare( " | |
SELECT | |
u.ID | |
FROM | |
$wpdb->users AS u | |
LEFT JOIN $wpdb->usermeta m ON u.ID = m.user_id AND m.meta_key = 'last_activity' | |
WHERE | |
m.meta_value < %s", date( 'Y-m-d', strtotime( '-' . $days . ' days' ) ) ); | |
return $wpdb->get_col( $query ); | |
} |
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
<?php | |
function find_inactive_user_ids( $days = 30 ) { | |
global $wpdb; | |
$query = $wpdb->prepare( " | |
SELECT | |
u.ID | |
FROM | |
$wpdb->users AS u | |
LEFT JOIN $wpdb->usermeta m ON u.ID = m.user_id AND m.meta_key = 'last_activity' | |
WHERE | |
m.meta_value < %s", date( 'Y-m-d', strtotime( '-' . $days . ' days' ) ) ); | |
return $wpdb->get_col( $query ); | |
} |
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
<?php | |
add_action( 'wp_login' , 'add_login_time' ); | |
function add_login_time( $user_login ) { | |
global $wpdb; | |
$sql = $wpdb->prepare( "SELECT ID FROM $wpdb->users WHERE user_login = %s", $user_login ); | |
$user_id = $wpdb->get_var( $sql ); | |
update_user_meta( $user_id, 'last_activity', current_time( 'mysql ' ) ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment