Custom Auth Cookie Expiration for WordPress

Trang chủ Blog WORDPRESS Custom Auth Cookie Expiration for WordPress
Custom Auth Cookie Expiration for WordPress

Mặc định WordPress cài đặt thời gian cho cookie đăng nhập users như sau: nếu bạn chọn remember khi đăng nhập, cookie sẽ được tạo với thời gian là 14 ngày, nếu không chọn remember khi đăng nhập thì cookie sẽ được tạo là 2 ngày. Bạn muốn cài đặt – setting thời gian thì thêm code sau vào function.php của theme nhé.

add_filter('auth_cookie_expiration', 'custom_expiration_filter', 100, 3);
function custom_expiration_filter($seconds, $user_id, $remember) {
	$expire_in = 0;
	if ( $remember ) {
	  $expire_in = intval(get_option('clt_remember_me_auth_timeout'));
	  if ( $expire_in <= 0 ) { $expire_in = 7*24*60*60; } // 1 week
	} else {
	  $expire_in = intval(get_option('clt_normal_auth_timeout'));
	  if ( $expire_in <= 0 ) { $expire_in = 60*60; } // 1 hours
	}
  
	// check for Year 2038 problem - http://en.wikipedia.org/wiki/Year_2038_problem
	if ( PHP_INT_MAX - time() < $expire_in ) {
	  $expire_in =  PHP_INT_MAX - time() - 5;
	}
  
	return $expire_in;
}