Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
WordPress admin column to print user websites visits
/*
* Admin user page custom column - Print User Web Visits Column Value
*/
class NSLogin_Counter {
public function init() {
add_action( 'wp_login', array( $this, 'count_user_login' ), 10, 2 );
add_filter( 'manage_users_columns', array( $this, 'add_stats_columns' ) );
add_filter( 'manage_users_custom_column', array( $this, 'fill_stats_columns' ), 10, 3 );
}
/**
* Save user login count to Database.
*
* @param string $user_login username
* @param object $user WP_User object
*/
public function count_user_login( $user_login, $user ) {
$count = get_user_meta( $user->ID, 'sp_login_count', true );
if ( ! empty( $count ) ) {
$login_count = get_user_meta( $user->ID, 'sp_login_count', true );
update_user_meta( $user->ID, 'sp_login_count', ( (int) $login_count + 1 ) );
}
else {
update_user_meta( $user->ID, 'sp_login_count', 1 );
}
}
/**
* Add the login stat column to WordPress user listing
*
* @param string $columns
*
* @return mixed
*/
public function add_stats_columns( $columns ) {
$columns['login_stat'] = __( 'Login Count' );
return $columns;
}
/**
* Fill the stat column with values.
*
* @param string $empty
* @param string $column_name
* @param int $user_id
*
* @return string|void
*/
public function fill_stats_columns( $empty, $column_name, $user_id ) {
if ( 'login_stat' == $column_name ) {
if ( get_user_meta( $user_id, 'sp_login_count', true ) !== '' ) {
$login_count = get_user_meta( $user_id, 'sp_login_count', true );
return "<strong>$login_count</strong>";
}
else {
return __( '0' );
}
}
return $empty;
}
/**
* Singleton class instance
* @return NSLogin_Counter
*/
public static function get_instance() {
static $instance;
if ( ! isset( $instance ) ) {
$instance = new self();
$instance->init();
}
return $instance;
}
}
NSLogin_Counter::get_instance();
/*
* Admin user page custom column - Capture User Last Login
*/
add_action('wp_login','ns_capture_user_last_login', 10, 2);
function ns_capture_user_last_login($user_login, $user){
update_user_meta($user->ID, 'last_login', current_time('mysql'));
}
//Display Last Login Date in Admin
add_filter( 'manage_users_columns', 'ns_user_last_login_column');
function ns_user_last_login_column($columns){
$columns['lastlogin'] = __('Last Login', 'lastlogin');
return $columns;
}
add_action( 'manage_users_custom_column', 'ns_add_user_last_login_column', 10, 3);
function ns_add_user_last_login_column($value, $column_name, $user_id ) {
if ( 'lastlogin' != $column_name )
return $value;
return get_user_last_login($user_id,false);
}
function get_user_last_login($user_id,$echo = true){
$date_format = get_option('date_format') . ' ' . get_option('time_format');
$last_login = get_user_meta($user_id, 'last_login', true);
$login_time = 'Never logged in';
if(!empty($last_login)){
if(is_array($last_login)){
$login_time = mysql2date($date_format, array_pop($last_login), false);
}
else{
$login_time = mysql2date($date_format, $last_login, false);
}
}
if($echo){
// @codingStandardsIgnoreStart
echo $login_time;
// @codingStandardsIgnoreEnd
}
else{
return $login_time;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.