Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

<?php
session_start();
$_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).

share|improve this question
    
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
3  
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
show 5 more comments

3 Answers

up vote 12 down vote accepted

EDIT: "THE PLUGIN BELOW ISN'T AVAILABLE ANYMORE, SO PLEASE USE THAT PLUGIN INSTEAD: WordPress Session Plugin"

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:

$session->sess_destroy();

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.

share|improve this answer
1  
WP Sessions Plugin is not there!?? –  Kiren Siva Dec 26 '13 at 10:02
    
Yes, you'll want to use that one: wordpress.org/plugins/wp-session-manager (This is much better and stabile). –  dino Dec 28 '13 at 1:55
add comment

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.

share|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 Feb 6 at 7:13
add comment

Wordpress does support it.

You need to add following lines at the top of wp-config.php

if (!session_id()) {
    session_start();
}


Then add following line at the top of header.php

session_start();
share|improve this answer
8  
Why would you start the session twice? –  kaiser Nov 16 '12 at 11:45
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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