可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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?
回答1:
The accepted answer didn’t work for me. What worked for me was this:
// After registration, logout the user and redirect to home page
function custom_registration_redirect() {
wp_logout();
return home_url('/');
}
add_action('woocommerce_registration_redirect', 'custom_registration_redirect', 2);
回答2:
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/');
回答3:
You want to use a filter like this:
function plugin_registration_redirect() {
return home_url( '/page-to-show' );
}
add_filter( 'registration_redirect', 'plugin_registration_redirect' );
Or, specifically to your code:
function plugin_registration_redirect() {
$url = wp_get_referer() ? wp_get_referer() : wc_get_page_permalink( 'welcome' );
return $url;
}
add_filter( 'registration_redirect', 'plugin_registration_redirect' );
回答4:
WP WooCommerce Redirect is a WordPress plugin to redirect your WooCommerce website after register or login! You can set any custom page or custom redirect according to user role.
You can set user login and register redirection page without any codding knowledge to your WooCommerce website . Your customer reduce time for using this plugin and get their destination easily.
I have developed a plugin for this issue. Also given bellow of raw code for redirect without any plugin.
//Redirect users to custom URL based on their role after login
function wp_woo_custom_redirect( $redirect, $user ) {
// Get the first of all the roles assigned to the user
$role = $user->roles[0];
$dashboard = admin_url();
$myaccount = get_permalink( wc_get_page_id( 'my-account' ) );
if( $role == 'administrator' ) {
//Redirect administrators to the dashboard
$admin_redirect = get_option('admin_redirect');
$redirect = $admin_redirect;
} elseif ( $role == 'shop-manager' ) {
//Redirect shop managers to the dashboard
$shop_manager_redirect = get_option('shop_manager_redirect');
$redirect = $shop_manager_redirect;
} elseif ( $role == 'customer' || $role == 'subscriber' ) {
//Redirect customers and subscribers to the "My Account" page
$customer_redirect = get_option('customer_redirect');
$redirect = $customer_redirect;
} else {
//Redirect any other role to the previous visited page or, if not available, to the home
$redirect = wp_get_referer() ? wp_get_referer() : home_url();
}
return $redirect;
}
add_filter( 'woocommerce_login_redirect', 'wp_woo_custom_redirect', 10, 2 );
If you feel comfort to work with plugin or without code? You can download and install my plugin "WP WooCommerce Redirect"
回答5:
Add this filter in theme function file.
function filter_woocommerce_registration_redirect( $var ) {
// make filter magic happen here...
return get_page_link(3598); // 3598 id page id.
};
Add the filter:
add_filter( 'woocommerce_registration_redirect',
'filter_woocommerce_registration_redirect', 10, 1 );
回答6:
If anyone is looking for the Login redirection version that works for the woocommerce my-account login:
add_filter('woocommerce_login_redirect', 'hs_login_redirect');
function hs_login_redirect( $redirection_url ) {
$redirection_url = get_home_url();
return $redirection_url;
}