注册后 Woocommerce 重定向
Woocommerce redirect after registration
我正在尝试在 Woocommerce 注册后重定向用户。我已经尝试了一切,但没有用。
我尝试了一些在网上找到的方法,但没有用……
当我将 'myaccount' 更改为另一个永久链接时,它只是在您单击注册时冻结。不知道为什么。
wp_safe_redirect( apply_filters( 'woocommerce_registration_redirect', wp_get_referer() ? wp_get_referer() : wc_get_page_permalink( 'welcome' ) )
我什至尝试使用页面 ID
wp_safe_redirect( apply_filters( 'woocommerce_registration_redirect', wp_get_referer() ? wp_get_referer() : wc_get_page_permalink( '1072' ) )
有什么帮助吗?
您想使用这样的过滤器:
function plugin_registration_redirect() {
return home_url( '/page-to-show' );
}
add_filter( 'registration_redirect', 'plugin_registration_redirect' );
或者,具体到您的代码:
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' );
已接受的答案对我不起作用。对我有用的是:
// 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);
WP WooCommerce Redirect 是一个 WordPress 插件,用于在注册或登录后重定向您的 WooCommerce 网站!您可以根据用户角色设置任何自定义页面或自定义重定向。
您可以设置用户登录和注册重定向页面,而无需任何编码知识到您的 WooCommerce 网站。您的客户减少使用此插件的时间并轻松到达目的地。
我已经针对这个问题开发了一个插件。还给出了没有任何插件的重定向原始代码。
//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 );
使用插件还是不使用代码你觉得舒服吗?您可以下载并安装我的插件“WP WooCommerce Redirect”
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.
正确的测试和功能代码是:
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
}
代码进入您活跃的子主题(或主题)的 function.php 文件...
woocommerce 中使用的一些常见重定向 url:
- 获取“首页”页面Url:
$redirection_url = get_home_url();
- 获取“商店”页面url:
$redirection_url = get_permalink( wc_get_page_id( 'shop' ) );
- 获取“购物车”页面 Url:
$redirection_url = wc_get_cart_url();
- 获取“结帐”页面Url:
$redirection_url = wc_get_checkout_url();
- 获取“我的帐户”或注册页面Url:
$redirection_url = get_permalink( wc_get_page_id( 'myaccount' ) );
- 通过 ID 获取其他 post 或页面:
$redirection_url = get_permalink( $post_id );
(其中 $post_id
是 post 或页)
- 通过路径获取post或页面(示例):
$redirection_url = home_url('/product/ninja/');
在主题功能文件中添加此过滤器。
function filter_woocommerce_registration_redirect( $var ) {
// make filter magic happen here...
return get_page_link(3598); // 3598 id page id.
};
添加过滤器:
add_filter( 'woocommerce_registration_redirect',
'filter_woocommerce_registration_redirect', 10, 1 );
如果有人正在寻找适用于 woocommerce 我的帐户登录的登录重定向版本:
add_filter('woocommerce_login_redirect', 'hs_login_redirect');
function hs_login_redirect( $redirection_url ) {
$redirection_url = get_home_url();
return $redirection_url;
}
我正在尝试在 Woocommerce 注册后重定向用户。我已经尝试了一切,但没有用。
我尝试了一些在网上找到的方法,但没有用……
当我将 'myaccount' 更改为另一个永久链接时,它只是在您单击注册时冻结。不知道为什么。
wp_safe_redirect( apply_filters( 'woocommerce_registration_redirect', wp_get_referer() ? wp_get_referer() : wc_get_page_permalink( 'welcome' ) )
我什至尝试使用页面 ID
wp_safe_redirect( apply_filters( 'woocommerce_registration_redirect', wp_get_referer() ? wp_get_referer() : wc_get_page_permalink( '1072' ) )
有什么帮助吗?
您想使用这样的过滤器:
function plugin_registration_redirect() {
return home_url( '/page-to-show' );
}
add_filter( 'registration_redirect', 'plugin_registration_redirect' );
或者,具体到您的代码:
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' );
已接受的答案对我不起作用。对我有用的是:
// 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);
WP WooCommerce Redirect 是一个 WordPress 插件,用于在注册或登录后重定向您的 WooCommerce 网站!您可以根据用户角色设置任何自定义页面或自定义重定向。
您可以设置用户登录和注册重定向页面,而无需任何编码知识到您的 WooCommerce 网站。您的客户减少使用此插件的时间并轻松到达目的地。
我已经针对这个问题开发了一个插件。还给出了没有任何插件的重定向原始代码。
//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 );
使用插件还是不使用代码你觉得舒服吗?您可以下载并安装我的插件“WP WooCommerce Redirect”
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.
正确的测试和功能代码是:
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
}
代码进入您活跃的子主题(或主题)的 function.php 文件...
woocommerce 中使用的一些常见重定向 url:
- 获取“首页”页面Url:
$redirection_url = get_home_url();
- 获取“商店”页面url:
$redirection_url = get_permalink( wc_get_page_id( 'shop' ) );
- 获取“购物车”页面 Url:
$redirection_url = wc_get_cart_url();
- 获取“结帐”页面Url:
$redirection_url = wc_get_checkout_url();
- 获取“我的帐户”或注册页面Url:
$redirection_url = get_permalink( wc_get_page_id( 'myaccount' ) );
- 通过 ID 获取其他 post 或页面:
$redirection_url = get_permalink( $post_id );
(其中$post_id
是 post 或页) - 通过路径获取post或页面(示例):
$redirection_url = home_url('/product/ninja/');
在主题功能文件中添加此过滤器。
function filter_woocommerce_registration_redirect( $var ) {
// make filter magic happen here...
return get_page_link(3598); // 3598 id page id.
};
添加过滤器:
add_filter( 'woocommerce_registration_redirect',
'filter_woocommerce_registration_redirect', 10, 1 );
如果有人正在寻找适用于 woocommerce 我的帐户登录的登录重定向版本:
add_filter('woocommerce_login_redirect', 'hs_login_redirect');
function hs_login_redirect( $redirection_url ) {
$redirection_url = get_home_url();
return $redirection_url;
}