Halloween Sale 15% off Ends in See the plans
PHP

Redirect to a Custom Thank-You Page

Sends customers to your own thank-you page after a successful order instead of the default order received page, passing the order number along.

Don’t have WPIDE yet? Install it on your site

Code

add_action( 'template_redirect', function () {
    $page_slug = 'thank-you'; // Slug of your thank-you page.

    if ( ! function_exists( 'is_wc_endpoint_url' ) || ! is_wc_endpoint_url( 'order-received' ) ) {
        return;
    }

    $order_id = absint( get_query_var( 'order-received' ) );
    $key      = isset( $_GET['key'] ) ? wc_clean( wp_unslash( $_GET['key'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification
    $order    = $order_id ? wc_get_order( $order_id ) : false;
    $page     = get_page_by_path( $page_slug );

    if ( ! $order || ! $page || ! hash_equals( $order->get_order_key(), $key ) || $order->has_status( 'failed' ) ) {
        return;
    }

    wp_safe_redirect( add_query_arg( 'order', $order->get_order_number(), get_permalink( $page ) ) );
    exit;
} );

Notes

Create the page and set $page_slug. Failed orders are left on the default page so the customer can retry payment.

How to add this snippet

  1. Install WPIDE on your WordPress site (it is free) and open Code Snippets.
  2. Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
  3. It is added inactive: review the code and settings, then switch it on.

Have a snippet worth sharing?

Publish it to the library from WPIDE or right here. Sign in with any WordPress site that runs WPIDE, free or Pro. Public snippets are reviewed by the WPIDE team before they appear.

Which site should get this snippet?

Enter the address of your WordPress site. WPIDE opens there in a new tab with the snippet ready to review and save.

Remembered in this browser only, so you are asked once.

Report this snippet

Tell the WPIDE team what is wrong: broken code, something harmful, spam or copied work.