0

I'm using a hook into init where I add a certain timestamp in order to include a splash screen into within my Wordpress project.

Basic logics of the script is:

  • add_action("init", "my_function", 1)
  • my_function handles all the logic and stores the $_SESSION variables I need else where

When I try to check the variables on my local machine (MAMP, Mac) it works perfectly fine. I can get the timestamp and simply check if the timestamp + offset is greater than the current time. If / else I will show the screen.

I pushed the scripts to my online development server however and for some kind of reason the $_SESSION variable returns NULL entirely. I've read things about destroying the session after usage, but that doesn't really work.

Wondering, does anyone know what's wrong here? I honestly have no idea... Besides; this whole chapter is very poorly documented on Wordpress' side which leaves me no other choice than asking here...

  • It's not documented on the WP docs because WP doesn't use PHP sessions, and neither should you – Tom J Nowell Nov 7 '19 at 12:02
1

WordPress does not use $_SESSION variables, and neither should you.

Sessions variables come with a lot of issues:

  • they're fundamentally incompatible with page caching plugins
  • they're incompatible with systems such as Cloudflare
  • WordPress uses cookies to track sessions
  • a lot of WP hosts are not set up to work with PHP sessions variables, e.g. WP Engine

If your goal is to show a splash screen the first time people visit your site, you don't need PHP session variables, there are much better/simpler/easier ways to do it.

For example:

  • Cookies!! This is probably your best bet, and can be set via javascript too
  • Using Javascript, and relying on a local storage mechanism
  • There may even be a CSS based solution using the :visited pseudo selector, e.g. .modal_link:visited * { display: none; visibility: hidden; } where modal_link is a class name on a hyperlink to your homepage
  • not showing a splash screen as it's awful UX and incredibly annoying to your sites visitors, and if you accidentally dismiss it, you need intermediate to advanced developer skills to bring it back. Research shows numerous disadvantages, etc, etc, etc

The last option would provide the best user experience.

As for your local machine, MAMP etc can't make too many guesses about what it's being used with, unlike a WP managed host. It's also extremely unlikely a CDN has been inserted between you and MAMP. Any page caching is probably disabled for logged in users as well. That doesn't mean what works in MAMP should work everywhere, just that what works in MAMP will work in MAMP

  • Working with a cookie indeed solved my issue. Please beware that including personal opinions like "not showing a splash screen as it's awful UX" is based on your personal experience and has little to do with answering code related questions :-). Thanks for the help pal! Enjoy your day. – Jim Nov 7 '19 at 13:21
  • It's a pretty thoroughly documented thing, glad the cookies worked, if the answer is correct could you mark it as accepted via the big tick under the voting buttons? – Tom J Nowell Nov 7 '19 at 13:26

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.