0

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?

2

1 Answer 1

0

What I did is combining cookies with transients just as others suggested. Be aware that the cookie must be sent with other HTTP headers, so init must be called in the plugin registration area while read and write can be called later in the code, because they are database reads and writes. I use this code along with storage interfaces which store arrays or small collection of entities this way.

class TransientSessionIO implements IO{

    protected $expirationSeconds;
    protected $cookiePrefix;
    public function __construct(array $settings = []){
        $this->expirationSeconds = isset($settings['expiration_seconds'])?$settings['expiration_seconds']:600;
        $this->cookiePrefix = isset($settings['cookie_prefix'])?$settings['cookie_prefix']:'transient_';
    }

    public function init(string $id){
        $this->getKey($id);
    }

    public function read(string $id){
        $data = get_transient($this->getKey($id));
        if ($data !== false)
            return $data;
    }
    public function write(string $id, array $data){
        delete_transient($this->getKey($id));
        set_transient($this->getKey($id), $data, $this->expirationSeconds);
    }

    protected $transientKey;
    protected function getKey(string $id):string{
        if (isset($this->transientKey))
            return $this->transientKey;
        $cookieKey = $this->cookiePrefix.$id;
        if (isset($_COOKIE[$cookieKey]))
            $this->transientKey = $_COOKIE[$cookieKey];
        else {
            $this->transientKey = $id.'__'.md5(''.time().rand());
            setcookie($cookieKey, $this->transientKey, time() + $this->expirationSeconds);
        }
        return $this->transientKey;
    }

}

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.