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? – Chip Bennett Nov 1 '11 at 19:48
  • 1
    "What is WordPress-specific nature ?" - that means, in what way is your question related to WordPress? – Chip Bennett Nov 1 '11 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. – Chip Bennett Nov 1 '11 at 19:53
  • 2
    No. Sessions and cookies are general internet/web-browser topics toward which WordPress is entirely agnostic. – Chip Bennett Nov 1 '11 at 19:57
  • 5
    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 '11 at 21:30


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'    => 'john@gmail.com'

$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.

| improve this answer | |
  • 1
    WP Sessions Plugin is not there!?? – Kiren Siva Dec 26 '13 at 10:02
  • 1
    Yes, you'll want to use that one: wordpress.org/plugins/wp-session-manager (This is much better and stabile). – beytarovski Dec 28 '13 at 1:55
  • Another plugin wordpress.org/plugins/wp-native-php-sessions – nu everest Dec 5 '16 at 19:45
  • Why can't we use PHP default session functionality in WordPress? This solution creates dependency on a plugin. – Amrit Oct 16 '17 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. – beytarovski Oct 16 '17 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.

| improve this answer | |
  • 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/… – shasi kanth Feb 6 '14 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? – shasi kanth May 29 '14 at 9:56
  • Tutorial that discusses this more silvermapleweb.com/using-the-php-session-in-wordpress – nu everest Dec 5 '16 at 19:46
  • 1
    @shasikanth no, wp-cofnig.php is not touched on updates. – T.Todua Mar 8 '18 at 11:36
  • 1
    @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 '19 at 14:32

Wordpress does support it.

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

if ( ! session_id() ) {
| improve this answer | |
  • 11
    Why would you start the session twice? – kaiser Nov 16 '12 at 11:45

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