WordPress is stateless open-source content management system so WordPress does not use sessions.This means that session is not initialized automatically in WordPress.If you want to use sessions in WordPress, WordPress you need to initialize the session manually.
Let’s have a look into following methods for enabling a seWordPress.
Config(wp-config.php) file is the Common place to initialize the session in wordpress.
Open your wp-config.php file and add the following line at the beginning of the file:
you can use session_start() in config.php, but it is not recommended.
Next Method, you can add following the line of code into functions.php. Here in this method,We use add_action( ‘init’,function_name ) which used to initialize and load plugins
Let’s have a look into the below function, I called by add_action and added session_start() so the session will be initialized and we can use it.
Recommended Read: Auto tagging in WordPress
$result=$wpdb- >get_var("SELECT count(*) FROM wp_userdetail
WHERE username = '$uname' AND password = '$pwd' AND status=1");
if($result > 0)
$_SESSION['username'] = $uname;
Also Read: Post via e-mail in WordPress
Here, I have called one function that is wp_check_login and fist of all, I have started a session with session_start() .Now you can use sessions in WordPress.