How can i use my own (custom) session value in Wordpress?
For example: $_SESSION['myname']="4lvin"

I've already inserted session_start() at all page i need as following.

$_SESSION['myname'] = "4lvin";

But not working Global-wise.
Just working on the self page.
It is NOT call-able Globally from another pages (using same logic).

  • Can you please clarify the WordPress-specific nature of your question? Nov 1, 2011 at 19:48
  • 1
    "What is WordPress-specific nature ?" - that means, in what way is your question related to WordPress? Nov 1, 2011 at 19:51
  • 1
    I see a question about session cookies. I don't see anything specific to WordPress, which is why I asked for clarification. Nov 1, 2011 at 19:53
  • 2
    No. Sessions and cookies are general internet/web-browser topics toward which WordPress is entirely agnostic. Nov 1, 2011 at 19:57
  • 6
    Yes, sessions and cookies are general topics, but this is more a question of making sessions work with WordPress ... and since WP itself doesn't use sessions, it's relevant here.
    – EAMann
    Nov 1, 2011 at 21:30

4 Answers 4



There is a good WordPress Plugin adapted from CodeIgniter Session class: WP Sessions Plugin.

When you activate the plugin, you can start to use $session object from anywhere in your theme ($session object as long as global). For instance, to use $session object into header.php file, simply add this code:

global $session;

If you are a plugin developer and you want to adapt this plugin with yours, you can find standalone version in the package as well. Documentation of the plugin gives more information for plugin developers about how to adapt to your project.

Here is some useful functions for both theme and plugin developers.

You can add session data like this:

// One value
$session->set_userdata( 'username', 'john' );

// Passing array
$array = array(
    'username' => 'john',
    'email'    => '[email protected]'

$session->set_userdata( $array );

To retrieve session data:

$session->userdata( 'username' );

To get all session data:

$session->all_userdata(); // returns array

To remove one item from session:

$session->unset_userdata( 'username' );

To remove more items from session:

$array = array(
    'username' => '',
    'email'    => ''
$session->unset_userdata( $array );

You can also use Flashdata which is session data that will only be available for the next server request, are then automatically cleared. These can be very useful when you use them for informational or status messages (e.g. “Product has been deleted”).

// Add Flashdata
$session->set_flashdata( 'item', 'value' );

// Retrieve Flashdata
$session->flashdata( 'item' );

// Preserving flashdata 
// (if you need to preserve flashdata through an additional request, 
// you can use this function):
$session->keep_flashdata( 'item' );

To destroy session:


The plugin also supports shortcodes. You can print any session data on your posts or pages:

[session key="username"]

To reach second key:

[session key="user_data" sec_key="display_name"]

I hope this helps for someone.

  • 1
    WP Sessions Plugin is not there!??
    – Kiren S
    Dec 26, 2013 at 10:02
  • 1
    Yes, you'll want to use that one: wordpress.org/plugins/wp-session-manager (This is much better and stabile). Dec 28, 2013 at 1:55
  • Another plugin wordpress.org/plugins/wp-native-php-sessions
    – nu everest
    Dec 5, 2016 at 19:45
  • Why can't we use PHP default session functionality in WordPress? This solution creates dependency on a plugin.
    – Amrit
    Oct 16, 2017 at 14:36
  • @Amritpal because not all PHP/Apache servers support sessions. If you want to build a public software/plugin like WP, you have to think about it. If its personal project where you are able to edit server, that's not a problem. Oct 16, 2017 at 14:44

WordPress doesn't use sessions, that's why your session variables aren't working.

As a matter of fact, if certain variables are defined, WordPress will actually destroy $_SESSION to keep itself stateless.

But if you really want to use sessions, try adding session_start() at the beginning of your wp-config.php file. This will (hopefully) start sessions whenever WP starts up, so you'll then be able to set and read your $_SESSION variables elsewhere in the system.

  • I saw that Wordpress uses Cookies to store some login data. When i printed $_COOKIE array, i could see some data. I would like to set that data manually. More info here: stackoverflow.com/questions/21595900/… Feb 6, 2014 at 7:13
  • 1
    Also, is it recommended to modify the wp-config.php file, to start session ? If we update Wordpress later, does the wp-config.php file get updated too? May 29, 2014 at 9:56
  • Tutorial that discusses this more silvermapleweb.com/using-the-php-session-in-wordpress
    – nu everest
    Dec 5, 2016 at 19:46
  • 2
    @shasikanth no, wp-cofnig.php is not touched on updates.
    – T.Todua
    Mar 8, 2018 at 11:36
  • 2
    @shasikanth wp-config.php will not get updated, otherwise you could lose you DB connection and other parameters you have manually set.
    – Erenor Paz
    Aug 14, 2019 at 14:32

One simple solution without using plug-in or without modifying the wp-config.php is to update your theme's functions.php and insert at the very beginning the following:


 //WordPress functions.php file
  if(!session_id()) {           

Since the functions.php (or so called theme functions file) is a template included in WordPress themes automatically and acts like a plugin for your WordPress site. Moreover, if you have more than 1 theme installed on your website, modifying the functions.php will affect only the chosen theme, not all (where you may not want to enable sessions).


Wordpress does support it.

You need to add following lines at the top of functions.php

if ( ! session_id() ) {
  • 11
    Why would you start the session twice?
    – kaiser
    Nov 16, 2012 at 11:45

Not the answer you're looking for? Browse other questions tagged or ask your own question.