skip to Main Content

I have a gateway plugin and I added the code below to redirect the customer to a custom page when the payment fails.

My question is, do I need to add exit(); after the end of the redirect_url which is inside the if statement?

I have seen some codes with exit and some without.

else {
        //failed
        $this->msg['class'] = 'error';
        $this->msg['message'] = __( "Your Transaction Has Failed Due to Some Technical Error. Please Try Again.<br/><br/>", 'toBeTranslatd');
        $order->update_status('failed');
        $order->add_order_note('Failed');
        $order->add_order_note($this->msg['message']);
        $redirect_url = home_url( '/payment-faild/' );

        $lang_code = get_post_meta( $order_id, 'wpml_language', true );
        if ( $lang_code === ar ) {
            $redirect_url = apply_filters( 'wpml_permalink', $redirect_url, $lang_code );
            //exit(); // Do I need this or delete it?
        }
}

2

Answers


  1. WordPress provides two functions for this:

    and

    And here’s what wordpress recommends:

    Note: wp_redirect() and wp_safe_redirect() do not exit automatically, and should almost always be followed by a call to exit;

    Login or Signup to reply.
  2. wp_redirect() or wp_safe_redirect() does not exit or die automatically, and should almost always be followed by a call to exit or die();

    Reference : https://developer.wordpress.org/reference/functions/wp_redirect

    Thank you.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search