[英]Redirect to the same page after registration with WooCommerce/WordPress
[英]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' ) )
有幫助嗎?
接受的答案對我不起作用。 對我有用的是:
// 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);
重要更新(適用於最新版本3.2.x)
首先,
woocommerce_registration_redirect
是一個過濾器掛鈎 ,但不是動作掛鈎。過濾器鈎子總是至少有一個參數,並且總是需要返回一些東西 。 它可以是主函數參數(第一個)或一些自定義值。
正確的測試和功能代碼是:
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中使用的一些常見的重定向網址:
$redirection_url = get_home_url();
$redirection_url = get_permalink( wc_get_page_id( 'shop' ) );
$redirection_url = wc_get_cart_url();
$redirection_url = wc_get_checkout_url();
$redirection_url = get_permalink( wc_get_page_id( 'myaccount' ) );
$redirection_url = get_permalink( $post_id );
$post_id
是帖子或頁面的ID) $redirection_url = home_url('/product/ninja/');
你想使用這樣的過濾器:
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' );
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 ”
在主題函數文件中添加此過濾器。
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 my-account登錄的登錄重定向版本:
add_filter('woocommerce_login_redirect', 'hs_login_redirect');
function hs_login_redirect( $redirection_url ) {
$redirection_url = get_home_url();
return $redirection_url;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.