# 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.

- Type: PHP
- Runs: Run everywhere
- Category: [WooCommerce](https://wpide.com/library/category/woocommerce/)
- Requires: WooCommerce
- Tags: woocommerce, checkout, thank-you, redirect
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/woo-custom-thank-you-page/

## Code

```php
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 use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Redirect to a Custom Thank-You Page" in its snippet library and add it in one click; or create a new PHP snippet in WPIDE (or any code snippets plugin) and paste the code above. More snippets: https://wpide.com/library/
