I have a form collecting user input that spans 3 separate pages in Wordpress. The user first fills in data in the form on page 1, presses "Next" and continues this process for pages 2 and 3. Each page, I collect a PHP variable that contains the user input. After the 3rd form page, when the user presses "Submit" I'm trying to echo each variable from all three form pages. I'm using WordPress and have received an error when trying to use session_register('var'); in my individual Wordpress pages. Is there a best way to achieve this?
Add session_start()
at the very top line of your header.php file
And then you can store the user input values in the global session variable $_SESSION[]
and retrieve it wherever you want.
Hope it helps.
Put this in your functions file...
// Session Management
add_action('init', 'myStartSession', 1);
add_action('wp_logout', 'myEndSession');
// Create session on init
function myStartSession() {
if(!session_id()) {
session_start();
}
}
// End session on logout
function myEndSession() {
session_destroy();
}
Now wordpress creates a session on initialisation and you can store any data in the session by using...
$_SESSION['whatever_your_data_is'] = 'Your Data';
And you can retrieve it on the next page with...
$my_data = $_SESSION['whatever_your_data_is'];
Although if your form is only short I would consider using hidden inputs to store your post data in the $_POST
, my experience with sessions in wordpress is that they're not 100% reliable.
Hope that helps.
regards
Dan