0

Is there a way to save a variable after you redirect to another page. For example... I want to save the password submitted from my form into the variable $temp_passkey, and be able to access it on the next page that I redirected too.

I tried passing the argument through the url, but I don't want data to be visible in the url, so I need another way, or a way to hide the add_query_arg data from the url after the redirect.

function process_my_form() {

        if ( ! empty($_POST['password']) ) {
            $temp_passkey = ($_POST['password']);
        }

        wp_safe_redirect( esc_url_raw( add_query_arg( 'bid_passkey', $temp_passkey, '/secure-window' ) ) );
        exit;
    }
    add_action( 'admin_post_process_my_form', 'process_my_form' );
    add_action( 'admin_post_nopriv_process_my_form', 'process_my_form' );
  • Why not just post the form directly to /secure-window and get the key from $_POST? Not sure why a redirect is necessary here. – Jacob Peattie Feb 26 at 10:09
0

You could store the password as a transient, then pass the transient key in the URL instead so it can be retrieved using that key on the next page.

function process_my_form() {

        if ( ! empty($_POST['password']) ) {
            $password = ($_POST['password']);
        } else {return;}

        /* Store Password via Transient API */
        $passkey = wp_generate_password(12, false);
        add_transient($passkey, $password, 300);

        wp_safe_redirect( esc_url_raw( add_query_arg( 'passkey', $passkey, '/secure-window' ) ) );
        exit;
}
add_action( 'admin_post_process_my_form', 'process_my_form' );
add_action( 'admin_post_nopriv_process_my_form', 'process_my_form' );

/* Example */
function process_password() {

    if ( !strstr($_SERVER['REQUEST_URI'], 'secure-window' ) {return;}
    if ( !isset($_POST['passkey']) ) {return;}

    $passkey = $_POST['passkey'];
    $password = get_transient($passkey);
    delete_transient($passkey);

    /* ... ... */
}
add_action( 'init', 'process_password' );

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.