0

I want add text to array and display after redirect and unset array.

        public function save($type, $value){

        global $tab;

        $tab[] = array('type' => $type, 'value' => $value); 

        }


        public function display(){

        global $tab;

        if (!empty($tab)){

            foreach (){ 

            echo ....

            }

         $tab = array();


        }

     } 


class test{


public function one(){

$this->save(2, 'some');

 $url = get_site_url(null, '/redirect_to_two');
 wp_safe_redirect( $url );

}


public function two(){

$this->display();

}



}

I have no value, nothing.

Did i must use sessions? This is only for messages, notices like succes register.

0

If you need to pass some data over the redirect, you can do that by adding parameters to the redirect url with add_query_arg(). But other more elegant solutions might exist also.

Single parameter,

wp_redirect( add_query_arg( 'notice', 'success', get_site_url(null, '/redirect_to_two') ) );
exit;

or multiple parameters

wp_redirect( add_query_arg( array( 'notice' => 'success', 'foo' => 'bar' ), get_site_url(null, '/redirect_to_two') ) );
exit;

Then use $_GET to grab the parameter(s),

if ( ! empty( $_GET['notice'] ) && 'success' == $_GET['notice'] ) {
    // Do whatever.
}

If I'm not mistaken, I don't think you need to worry about unsetting arrays as data doesn't normally persist in WordPress. Once you do a redirect all the data in your $variables are gone.

  • Data in variables are gone but i still have in url something like mysite/login?notice=success and i not want this. If someone go back and return then is not good. I refresh site and still is. – polyman Mar 28 at 21:05
  • What if you removed the query parameters with javascript from the url? Or would creative use of transients be applicable? E.g. after the save function create a one-use random string as a transient and add the same string as a query parameter to the redirect url. After redirect match the parameter and the transient to trigger a notice. Delete the transient afterwards. – Antti Koskinen Mar 29 at 11:45
  • Could perhaps use a cookie, then delete it after the redirect. But that wouldn't work if the browser had disabled them. There could still be an issue with returning to the site. Could perhaps put a quite short expiration time on the cookie. – Loren Rosen Mar 30 at 3:48

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.