0

I'm thinking using transients to store form messages to be showed after a form was submited and the page reload.

My question is: if two or more user are using the same form in differents sessions, How can I get the correct transient message to the correct user?

  • But why do you want to use transients for this? Why not to use $_SESSION, if it's for logged in users? – Krzysiek Dróżdż Jun 15 '18 at 23:50
  • @KrzysiekDróżdż WordPress not work well with $_SESSION. – Marcelo Jun 16 '18 at 2:24
  • Why? And why do you think that transients will solve this problem? – Krzysiek Dróżdż Jun 16 '18 at 6:45
0

I would use user ID then user IP if not logged in, you can store the result like the following:

/*
your form validation/submission logiccode goes here
*/

$max_execution_tim = @ini_get('max_execution_time'); 
$submission_result = array('message' => 'Worked!', 'status' => 'success');

// You can use this function:
// https://stackoverflow.com/a/6718472/1321398
$user_ip = GetIP();
$transient_name = is_user_logged_in() ? "form_submission_user-" .         
get_current_user_id() : "form_submission_anonimous-" . $user_ip ;
// save it for a minute
set_transient( $transient_name, $submission_result, 1 * MINUTE_IN_SECONDS );

Then you can check before outputting the form:

$user_ip = GetIP();
$transient_name = is_user_logged_in() ? "form_submission_user-" . 
get_current_user_id() : "form_submission_anonimous-" . $user_ip ;
if( $submission_result = get_transient( $transient_name ) ){
    //show your message
    echo $submission_result['message'];
    // delete it to avoid duplicated messages
    delete_transient( $transient_name );
}
  • IP solution will not work in large office or school, where hundreds of users will have the same IP. – kierzniak Jun 16 '18 at 10:29
  • 1
    Agree with you @motivast, I would use PHP sessions (I know is not correct to suggest plugins here but es.wordpress.org/plugins/wp-native-php-sessions is a good option) or even better solution would be ajax submission. It's not clear to me but looks like the form is just for logged in users? – Pabamato Jun 18 '18 at 0:58
0

One quick way can be to add the hash of user's IP address to the key. Basically, if you were saving transient as 'user-response', now save it as 'user-response' . md5(<user's IP address>).

If you are collecting sensitive data, I'd advise storing the response in visitor's browser as a cookie and then displaying it on reload from the cookie itself. That doesn't require transient though.

0

Transients were invented for the purposes of storing the cache for all non-specific users. You are trying to simulate the operation of a session that has just been created for such things. Why invent your own solution and not use existing ones?

"WordPress not work well with $_SESSION."

Do you have any case studies to support your opinion? I used sessions many times in my projects and did not face any problems. There is even a plugin created only to work with the session in WordPress. https://github.com/ericmann/wp-session-manager

Only wordpress.com does not support the session because it uses own solution. Until you use wordpress.com, you can use the session without problems.

Comming back to your question. You’re looking for Flash Messages. Flash Messages are messages stored in the session and deleted right after they are retrieved. You can find some implementation on GitHub: https://github.com/plasticbrain/PhpFlashMessages

<?php 

// Start a Session
// Read about managing session with WordPress

// Instantiate the class
$fm = new \Plasticbrain\FlashMessages\FlashMessages();

// Process form e.g. send an email and add success message
$msg->success('Thank you for sending message!');

// Display message
if( $fm->hasMessages('success') ) {

    /**
     * This line will display success message and also remove it
     * from session. After refresh $fm->hasMessages('success')
     * will return false
     */ 
    $fm->display('success'); 
}

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.