Using Sessions in WordPress


March 13, 2012 by Al

WordPress does not use sessions to hold data. It is a stateless application. This means that if you want to use sessions in your plugins or custom modifications of WordPress, you may need to do a little hack to enable sessions.

Edit your wp-config.php file (located at the root of your blog) and add the following line at the beginning of the file:

if (!session_id())

Now you can use sessions in WordPress. Remember that for most plugins, instead of using sessions to store data you can pass variables in URLs and hidden fields or use cookies, and that is what WordPress encourages.


Here’s a simple function you can add in your functions.php file that will start a session automatically. Then you don’t need the wp-config.php session_start(); code.

function cp_admin_init() {
if (!session_id())

add_action(‘init’, ‘cp_admin_init’);


see also:


Sorry, comments are closed.