I want to use flash messages to deliver validation error notices. For people who are not familiar this means that
- the user sends the form,
- I process the POST data,
- I store the error messages temporarily,
- I redirect the page to have a GET request
- I read and remove the error messages from the temporary storage
- I display the error messages in a bar on the new page
Normally I would do this with $_SESSION
, but afaik. WP uses something different for session management. I am still investigating it. The current recommended solution is using the get_current_user_id()
for identification of the session and to use a transient
as a temporary storage. My main problem that we don't necessarily have a logged in user in the case of sending a form. For example we can have anonymous surveys, quizzes or contact forms without logged in users. E.g. in the case of contact form the error message can be something like "invalid email format", "invalid email provider", "email required", "message required", etc... Some of them can be handled by HTML5, but not all of them.
So is there a way to get a session id for not logged in users or store the flash messages in the session of a not logged in user temporarily somehow?
is_user_logged_in()
to check status and use cookies to store data?