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.

3 Answers
3

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.

Leave a Reply

Your email address will not be published. Required fields are marked *