Woocommerce redirect after registration

John B. picture John B. · May 20, 2015 · Viewed 24.9k times · Source

I am trying to redirect user after Woocommerce registration. I have tried everything and it is not working.

I have tried some methods found on internet but they didn't work…

When I change 'myaccount' to another permalink it just freezes when you click register.. not sure why.

wp_safe_redirect( apply_filters( 'woocommerce_registration_redirect', wp_get_referer() ? wp_get_referer() : wc_get_page_permalink( 'welcome' ) ) 

I even tried with the page id

wp_safe_redirect( apply_filters( 'woocommerce_registration_redirect', wp_get_referer() ? wp_get_referer() : wc_get_page_permalink( '1072' ) ) 

Any help?

Answer

LoicTheAztec picture LoicTheAztec · Dec 23, 2017

Important update (working on last version 3.2.x)


First the woocommerce_registration_redirect is a filter hook, but NOT an action hook.

A filter hook has always at least one argument and always require to return something. It can be the main function argument (the first one) or some custom value.

The correct tested and functional code is:

add_filter( 'woocommerce_registration_redirect', 'custom_redirection_after_registration', 10, 1 );
function custom_redirection_after_registration( $redirection_url ){
    // Change the redirection Url
    $redirection_url = get_home_url(); // Home page

    return $redirection_url; // Always return something
}

Code goes in function.php file of your active child theme (or theme)...


Some common redirections urls used in woocommerce:

  • Get the "Home" page Url: $redirection_url = get_home_url();
  • Get the "Shop" page url: $redirection_url = get_permalink( wc_get_page_id( 'shop' ) );
  • Get the "Cart" page Url: $redirection_url = wc_get_cart_url();
  • Get the "Checkout" page Url: $redirection_url = wc_get_checkout_url();
  • Get the "My account" or registration page Url:
    $redirection_url = get_permalink( wc_get_page_id( 'myaccount' ) );
  • Get other post or pages by ID: $redirection_url = get_permalink( $post_id );
    (where $post_id is the id of the post or the page)
  • Get post or pages by path (example): $redirection_url = home_url('/product/ninja/');