How to use sessions in wordpress
PHP Sessions are used to track and store information of specific user session through out the php application. I will show you how to use php session in a wordpress theme.
Start Session Using Action Hook
<?php add_action('init', 'wp_session_manager'); function wp_session_manager() { if (!session_id()) { session_start(); } }
Assign value to session when user authenticated
add_filter('authenticate', 'check_login', 10, 3); function check_login($user, $username, $password) { $user = get_user_by('login', $username); if($user) { $_SESSION['info']='value'; } return $user; }
Destroy the session data when user logged out
add_action('wp_logout', 'session_logout'); function session_logout() { session_destroy(); }
No comments: