User Last Login with sorting in WordPress

// Save wp user last login time in User meta

function update_user_lastlogin($user_login ) {
$user = get_user_by( ‘login’, $user_login );
update_user_meta( $user->ID, ‘wp-last-login’, time());
}

add_action(‘wp_login’,’update_user_lastlogin’);

function add_lastlogin_user_column($columns) {
return array_merge( $columns, array(‘wp-last-login’ => __(‘Last Login’,’wp-last-login’)) );
}
add_filter(‘manage_users_columns’ , ‘add_lastlogin_user_column’);

function manage_lastlogin_status_column( $value, $column_name, $user_id )
{
if ( ‘wp-last-login’ == $column_name ) {
$value = __( ‘Never.’, ‘wp-last-login’ );
$last_login = (int) get_user_meta( $user_id, ‘wp-last-login’, true );

if ( $last_login ) {
$format = apply_filters( ‘wpll_date_format’, get_option( ‘date_format’ ) );
$value = date_i18n( $format, $last_login );
}
}
return $value;
}
add_filter(‘manage_users_custom_column’, ‘manage_lastlogin_status_column’, 10, 3);

function add_sortable( $columns ) {
$columns[‘wp-last-login’] = ‘wp-last-login';
return $columns;
}
add_filter( ‘manage_users_sortable_columns’,’add_sortable’ );

function wpse_ls_pre_user_query($user_search) {
global $wpdb,$current_screen;
if ( ‘users’ != $current_screen->id )
return;
$vars = $user_search->query_vars;
if(‘wp-last-login’ == $vars[‘orderby’]){
$user_search->query_from .= ” INNER JOIN {$wpdb->usermeta} m1 ON {$wpdb->users}.ID=m1.user_id AND (m1.meta_key=’wp-last-login’)”;
$user_search->query_orderby = ‘ ORDER BY UPPER(m1.meta_value) ‘. $vars[‘order’];
}
}

add_action(‘pre_user_query’,’wpse_ls_pre_user_query’);

Get Remaining Time from given Expire future date

function ls_remaining_time( $expire_timestamp, $left_red = ‘right’ ){
if( !empty( $expire_timestamp ) && $expire_timestamp > -1 ){
$diff = $expire_timestamp – current_time( ‘timestamp’ );

if( $diff > 0 ){

$secondsInAMinute = 60;
$secondsInAnHour = 60 * $secondsInAMinute;
$secondsInADay = 24 * $secondsInAnHour;

/* extract days */
$days = floor( $diff / $secondsInADay );

/* extract hours */
$hourSeconds = $diff % $secondsInADay;
$hours = floor( $hourSeconds / $secondsInAnHour );

/* extract minutes */
$minuteSeconds = $hourSeconds % $secondsInAnHour;
$minutes = floor( $minuteSeconds / $secondsInAMinute );

/* extract the remaining seconds */
$remainingSeconds = $minuteSeconds % $secondsInAMinute;
$seconds = ceil( $remainingSeconds );

if( $days > 0 ){
if( $days == 1 ){
$remaining = ‘1 ‘.__( ‘day’, ‘couponxl’ );
}
else{
$remaining = $days.’ ‘.__( ‘days’, ‘couponxl’ );
}
}
else if( $hours > 0 ){
if( $hours == 1 ){
$remaining = ‘1 ‘.__( ‘hour’, ‘couponxl’ );
}
else{
$remaining = $hours.’ ‘.__( ‘hours’, ‘couponxl’ );
}
}
else if( $minutes > 0 ){
if( $minutes == 1 ){
$remaining = ‘1 ‘.__( ‘minute’, ‘couponxl’ );
}
else{
$remaining = $minutes.’ ‘.__( ‘minutes’, ‘couponxl’ );
}
}
else if( $seconds > 0 ){
if( $seconds == 1 ){
$remaining = ‘1 ‘.__( ‘second’, ‘couponxl’ );
}
else{
$remaining = $seconds.’ ‘.__( ‘seconds’, ‘couponxl’ );
}
}
}
else{
$remaining = __( ‘Expired’, ‘couponxl’ );
}
}
else{
$remaining = __( ‘Unlimited Time’, ‘couponxl’ );
}

if( $left_red == ‘right’ ){
return __( ‘Expires in: ‘, ‘couponxl’ ).'<span class=”red-meta”>’.$remaining.'</span>';
}
else{
return ‘<span class=”red-meta”>’.__( ‘Expires in: ‘, ‘couponxl’ ).'</span>’.$remaining;
}
}

Get url by page template name in WordPress

/* get url by page template */
function ls_get_permalink_by_tpl( $template_name ){
$page = get_pages(array(
‘meta_key’ => ‘_wp_page_template’,
‘meta_value’ => $template_name . ‘.php’
));
if(!empty($page)){
return get_permalink( $page[0]->ID );
}
else{
return “javascript:;”;
}
}

 

Now call

<form method=”post” action=”<?php echo ls_get_permalink_by_tpl( ‘page-tpl_register’ ) ?>”>

</form>

//page-tpl_register is a file name without .php extension

 

Destroy idle User Session in Php

in User Login page set session after user successfully login.

<?php
$_SESSION[‘last_active_time’]=time();
?>

In Common Page which are every time load.

<?php
session_start();
$idletime=60;//after 60 seconds idle time the user gets logged out.
if (time()-$_SESSION[‘last_active_time’]>$idletime){
session_destroy();
session_unset();
}
else
{
$_SESSION[‘last_active_time’]=time();
}
?>

User Gravatar from an Email Address in Php

function get_gravatar( $email, $s = 80, $d = ‘404’, $r = ‘x’, $img = false, $atts = array() ){

$url = ‘http://www.gravatar.com/avatar/';
$url .= md5(strtolower(trim($email)));
$url .= “?s=$s&d=$d&r=$r”;
if ( $img ){

$url = ‘<img src=”‘ . $url . ‘”‘;

foreach ( $atts as $key => $val )
$url .= ‘ ‘ . $key . ‘=”‘ . $val . ‘”‘;
$url .= ‘ />';
return $url;
}
$headers = @get_headers($url);
if (!preg_match(“|200|”, $headers[0])) {
$has_valid_avatar = ‘no';
} else {
$has_valid_avatar = ‘yes';
}
return $has_valid_avatar;
}