Custom Redirect after registration in WooCommerce

After a user registers using WooCommerce’s registration form, I want to redirect them to a custom page, such as my other website, instead of the my-account page.

add_action('woocommerce_registration_redirect', 'ps_wc_registration_redirect',10); 
function ps_wc_registration_redirect( $redirect_to ) {  
    $redirect_to ="http://example.com";
    return $redirect_to;
}

The hook above successfully redirects users when the destination is a page on the current site, but when the redirect location is off of the current site, it does not work.

Is there any other hook available after the user registration redirection occurs?

2 Answers
2

Internally, WooCommerce uses WordPress’ wp_safe_redirect() which does not allow redirects to external hosts. In order to get around this, we must add our desired host to the whitelist. The whitelist can be modified using the allowed_redirect_hosts which has been demonstrated below:

/**
 * Adds example.com to the list of allowed hosts when redirecting using wp_safe_redirect()
 *
 * @param array       $hosts An array of allowed hosts.
 * @param bool|string $host  The parsed host; empty if not isset.
 */
add_filter( 'allowed_redirect_hosts', 'wpse_allowed_redirect_hosts', 10, 2 );
function wpse_allowed_redirect_hosts( $hosts, $host ) {
    $hosts[] = 'example.com';

    return $hosts;
}

Use the code above along with your original code (customizing the host as needed) to allow WooCommerce users to be redirected to an external domain after completing the registration process.

Leave a Comment