3

I wondering the session was disable in wordpress. I try to find some information on using session variables in the wordpress. I tried the following method to implement session variables in the wordpress site.

I tried this way.

<?php 
session_start();

$_SESSION['firstname'] = $_POST['firstname'];

?>

If i echoing the $_SESSION['firstname'] is not come's up. So I googled to find the reason why it doesn't support.

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

add_action('init', 'register_session');

This above method i tried and implemented but it also doesn't work.

Right now i integrating session in plugin so i don't want to edit the core file. Because i doing the custom plugin development.

How to enable the session in wordpress plugin. Any Suggestion would be great

Thanks.

4

Your code doesn't seem to be wrong, it's just how I would do it - see my answer here. So the question would be is your session getting started? Try debugging this:

// do you have a session id
$s_id = session_id();
print_r($s_id);

// can you declare a bogus session variable, yours might just be empty
$_SESSION['bogus'] = 'bogus';
print_r($_SESSION['bogus']);

Another advisable step is to lower the priority of the add_action() call:

add_action('init', 'register_session', 1);

just to make sure you have your session ready before it's needed by another action/function.

| improve this answer | |
1

Could the same functionality be achieved with Transients? i.e.:

set_transient( 'username', $_POST['firstname'], MINUTE_IN_SECONDS );

Sessions are not necessary then.

| improve this answer | |
0

In your plugins, use wp hook to store session variable.

add_action('wp', 'start_my_session');
function start_my_session() {
    session_start();
    $_SESSION['myvar'] = 'this is my var';
}

Now you can access it from anywhere. for example:

add_action('wp_footer', 'show_session_var');
function show_session_var() {
    if(isset($_SESSION['myvar'])) echo $_SESSION['myvar'];
}
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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