我只想為登錄用戶提供訪問 Woo 產品單頁的權限。
我在主頁和商店頁面上有一個產品串列,我想要的是:當注銷的用戶單擊產品時,用戶將獲得一個登錄表單,登錄后用戶將被重定向到他想要的產品頁面看法。
所以流程將是這樣的:主頁/商店->點擊產品X->登錄頁面->重定向到產品X單頁目前,我正在使用由這個函式woocommerce_login_form()創建的常規Woo登錄表單
我正在嘗試以下代碼片段:
add_filter('login_redirect', 'my_login_redirect', 10, 3);
function my_login_redirect() {
$location = $_SERVER['HTTP_REFERER'];
var_dump($location);
wp_safe_redirect($location);
exit();
}
}
add_action('init','my_login_redirect');
function my_login_redirect() {
$location = $_SERVER['HTTP_REFERER'];
var_dump($location);
//wp_safe_redirect($location);
}
-----------------------
AND ALSO THIS ONE
-----------------------
function redirect_after_login(){
global $wp;
$protocol='http';
if (isset($_SERVER['HTTPS']))
if (strtoupper($_SERVER['HTTPS'])=='ON')
$protocol='https';
if (!is_user_logged_in() && is_product() ){
$redirect = site_url() . "/my-account.php?redirect_to= $protocol://" .
$_SERVER["HTTP_HOST"] . urlencode($_SERVER["REQUEST_URI"]);
wp_redirect( $redirect );
exit;
}
}
add_action( 'wp', 'redirect_after_login', 3 );
In both of cases, the problem is, it always find the Login page as HTTP_REFERER / REQUEST_URI
Because currently, I am using below code to redirect Non-logged-in user who is trying to see the product page to the Login page:
add_action('template_redirect', 'ethis_redirect_for_loggedin_users');
function ethis_redirect_for_loggedin_users() {
if ( !is_user_logged_in() && is_product() ) {
wp_redirect(site_url().'/default-login');
exit;
}
}
uj5u.com熱心網友回復:
您可以使用template_redirect過濾器掛鉤來阻止訪客用戶訪問單個產品頁面。
add_action( 'template_redirect', 'wc_redirect_non_logged_to_login_access');
function wc_redirect_non_logged_to_login_access() {
if ( !is_user_logged_in() && is_singular( 'product' ) ) {
global $post;
wp_redirect( get_permalink( get_option('woocommerce_myaccount_page_id')).'?redirect='.get_the_permalink( $post->ID ) );
exit();
}
}
然后在您必須使用woocommerce_login_redirect過濾器掛鉤進行登錄重定向之后。
add_filter( 'woocommerce_login_redirect', 'my_login_redirect', 10, 2 );
function my_login_redirect( $redirect, $user ) {
if( isset( $_GET['redirect'] ) && $_GET['redirect'] != '' ){
return $_GET['redirect'];
}
return $redirect;
}
代碼將進入您的活動主題functions.php 測驗和作業。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/331148.html
標籤:WordPress的 重定向 求购
