wc_template_redirect() in WooCommerce: Guide [With Code]

wc_template_redirect() redirect flow in WooCommerce

Some WooCommerce redirects happen before WordPress picks a template. Shop page redirects, empty checkout redirects, and account endpoint 404s all come from one place: wc_template_redirect().

This guide explains what the function does, when it runs, which filters control it, and how to add your own early redirects with copy-paste code.

What wc_template_redirect() does

wc_template_redirect() is a WooCommerce function that checks the current request and redirects it or sets a 404 before any output is sent. It returns nothing. Its job is to decide whether the visitor should see the page they asked for or a more useful one.

When it runs

WooCommerce hooks the function to template_redirect. WordPress fires that action after it parses the query and before it loads a template, so conditional tags such as is_page() and is_search() already work. No output has been sent yet, so a redirect is still possible.

Tip: Callbacks on template_redirect run in priority order. The default priority is 10, so a callback at priority 5 runs before WooCommerce’s check.

What it handles by default

The function covers several cases. These three are the ones shoppers hit most often.

Shop page redirect

With default permalinks, WooCommerce can send requests for the shop page to the product archive, which keeps one canonical URL for the catalog.

Empty cart at checkout

If a shopper opens checkout with an empty cart, WooCommerce sends them to the cart page. A filter controls this, covered in the next section.

Single search result

When a search returns exactly one product, WooCommerce redirects straight to that product instead of showing a one-item results page.

The function also handles a few smaller cases:

  • Logout: the My Account logout endpoint requires a valid nonce.
  • Account endpoints: an account endpoint opened on the wrong page returns a 404.
  • Checkout and add payment method: these pages use output buffering and preload payment gateways and shipping methods.

Filters you can use

Three filters let you change the behavior without editing core files:

  • woocommerce_checkout_redirect_empty_cart decides whether an empty cart redirects from checkout. Return false to stay on the page.
  • woocommerce_redirect_single_search_result decides whether a single search result redirects to the product.
  • woocommerce_account_endpoint_page_not_found controls the 404 for account endpoints opened on the wrong page.

The first filter receives a boolean and returns the value to use. This example lets shop managers keep the checkout page open when the cart is empty, so they can preview it:

<?php

add_filter( 'woocommerce_checkout_redirect_empty_cart', 'my_keep_checkout_for_managers' );

function my_keep_checkout_for_managers( $redirect ) {
    if ( current_user_can( 'manage_woocommerce' ) ) {
        return false;
    }

    return $redirect;
}

To turn off single search result redirects everywhere, use the core helper __return_false:

<?php

add_filter( 'woocommerce_redirect_single_search_result', '__return_false' );

Adding your own early redirect

Hook your own callback to template_redirect with a priority below 10. This example sends an old sale page to the shop with a 301 status. Use wp_safe_redirect() for redirects inside your own site, and call exit afterwards so nothing else runs.

<?php

add_action( 'template_redirect', 'my_redirect_old_sale_page', 5 );

function my_redirect_old_sale_page() {
    if ( ! is_page( 'summer-sale' ) ) {
        return;
    }

    wp_safe_redirect( home_url( '/shop/' ), 301 );
    exit;
}

Checking the hook priority

If a redirect doesn’t behave as expected, confirm where WooCommerce hooked the function. has_action() returns the priority, or false if the hook is missing. Run the check from a mu-plugin or on a staging site, then read the value in your debug log:

<?php

add_action( 'init', 'my_check_template_redirect_priority', 20 );

function my_check_template_redirect_priority() {
    $priority = has_action( 'template_redirect', 'wc_template_redirect' );

    error_log( 'wc_template_redirect priority: ' . var_export( $priority, true ) );
}

A value of 10 means the default. Anything else means someone changed the priority.

FAQs About wc_template_redirect()

Can I disable it?

Technically yes, with remove_action( 'template_redirect', 'wc_template_redirect' ). That also removes the logout nonce check and the account endpoint handling, so use the filters above instead.

Why does my shop page redirect?

The most common cause is the shop page redirect with default permalinks. Check your permalink structure under Settings then Permalinks, and look for a conflicting redirect plugin.

Can I call it manually?

You can, but you shouldn’t. The function assumes it runs during template_redirect, when the main query is ready. Calling it from an early hook can produce redirects or 404s you didn’t intend.

Does it return a value?

No. It returns nothing. It either redirects, sets a 404, or lets the request continue.

Conclusion

wc_template_redirect() is a small function with a large effect: it decides what visitors see before the template loads. Use the filters to change its defaults, and use a priority below 10 when you need a redirect of your own.

If you need a custom checkout or redirect flow for your store, hire a WooCommerce developer or send a brief through the application form.

About The Author

Picture of Andriy Haydash

Andriy Haydash

Andriy Haydash is a WordPress Expert who helps people build and launch successful WordPress membership and e-learning websites.

Note: Not all of the articles are written directly by me.
Affiliate Disclaimer: Some links in the post may be my affiliate links

Kinsta - Managed Hosting for WordPress

Before YOU Leave...
Join My Newsletter

Get practical tips & tricks on how to start, grow and market your course/membership site.