I have been trying to figure out how to add session variables to WordPress Custom Pages for the past few days but have still been unable to find a solution. From researching it seems WordPress does not allow you to move Session variables from one page to the next. I have tried removing all of the 'session_start();' from each page and adding the below to functions.php file.

add_action('init', 'myStartSession', 1);
function myStartSession() {
if(!session_id()) {

Have also tried adding code below to the wp-config.php but to no avail.

if (!session_id())

The session will create a unique id for each user which will be checked on the next page to see if it equals the previous id. The first page code is as follows:

$_SESSION['t'] = md5(session_id().'3ac49262e797b6a51b6362e264d9dbe1');

The next page is:

$testValue = md5(session_id().'3ac49262e797b6a51b6362e264d9dbe1');
if ($testValue == $_SESSION['t'])
{$passFlag = 1;}
{$passFlag = 0;}

'session_write_close();' is called further down on this page. Any help would be greatly appreciated and if you need any further information please don't hesitate to message. Thanks.

  • 3
    WP doesn't use PHP sessions, and they won't play nicely with page caching systems. Also, some hosts don't support them because of the way they're built, e.g. WP Engine and PHP Sessions don't work together. Instead, rely on user meta, cookies, or client side local storage
    – Tom J Nowell
    Aug 24, 2017 at 23:13

2 Answers 2


You need to add your code to the functions.php file of your theme. (Actually, adding it to your Child Theme is best, since you don't want a theme update changing things. Unless you are writing your own theme.)

So this in your functions.php file:

if(!session_id()) {
  • Thanks for the quick reply. I have made the change and added the above code to the Child theme and removed the code that was there previous but still the issue still seems to persist. The console log shows the Session being created in the first page but once redirected to page 2 the Session equals null.
    – maxwell
    Aug 25, 2017 at 0:27
  • Hmm...assume that the above code is in the very beginning of functions.php, without any spaces before it. Should work. The 2nd answer to stackoverflow.com/questions/11797351/… indicates that you can stick it in the init filter, but I see that didn't work for you - perhaps without the priority? This might help: tommcfarlin.com/php-sessions-and-wordpress . Maybe multiple sessions being created? My research indicates either your or my approach should work. Maybe caching plugin? Try running tests in an incognito browser. Aug 25, 2017 at 1:26

(Making @Tom's comment into a proper answer)

PHP sessions are legacy from the beginning days of PHP when the use of DBs was rare, complex and expensive. Since then everybody moved on, and now there is nothing that a session can do that a DB will not do in a more consistent and reliable way. If you feel like you need some information stored on the server just write to the DB, whether it is to a user's meta or a new table (if your users do not map into WP users).

And since sessions require cookies in any case, you might as well just store your unique key in a cookie.

  • Mark will give the DB a go as there is also users data gathered in another session also. From your experience would you know if there is anything to look out for or anything that would be useful to know before implementing it in this way?
    – maxwell
    Aug 25, 2017 at 18:15
  • Just the most obvious thing, and where sessions are better then dB access, DB writes are more expensive, so if you really have to do it, do it only when you must. Every page load of every page of the site for example is probably not a great idea from scaling POV, but if it is limited to small amount of pages it can work Aug 26, 2017 at 3:48
  • From the entire website there is only 6 pages that it will be needed on so will give it a go, thanks for your help.
    – maxwell
    Aug 26, 2017 at 12:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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