![](/img/trans.png)
[英]WordPress: Redirect homepage and pages to account page when not in a specific user role
[英]Redirect WordPress User based on user role when accessing a page
當有人嘗試根據用戶角色訪問某個頁面時,我正在嘗試創建一個簡單的重定向。
我創建了一個具有會員功能的網站,為付費會員提供某些東西。 免費會員登錄后只能訪問一個頁面,但如果他們知道付費區的 URL,他們就可以看到內容。 我想知道是否有辦法創建 301 重定向,以便當免費會員嘗試進入付費頁面時,它會將他們重定向回免費區域。
這可能嗎? 如果您需要更多信息,請詢問!
謝謝大衛
將此代碼粘貼到您的激活主題或冷藏主題 function.php 文件中
function role_redirections() {
$logedin_user = wp_get_current_user();
if (!is_user_logged_in() || !in_array( 'shop_manager', (array) $logedin_user->roles )) {
$location1 = home_url();
header('Location: '.$location1.'');
}
}
add_action('wp_head', 'role_redirections');
@rajat.gite
我試過你的回答,但它並沒有像我想要的那樣工作,所以我修改了代碼如下:
function role_redirections() {
$loggedin_user = wp_get_current_user();
if (!is_user_logged_in() || !in_array( 'memorial_user', (array) $loggedin_user->roles )) {
$location1 = 'https://www.myswansong.com/member-portal/';
header('Location: '.$location1.'');
}
}
add_action('wp_head', 'role_redirections');
唯一真正的區別是修改 home_url(); 到一個帶有我需要的完整 URL 的字符串。
謝謝你的幫助。
你也可以這樣嘗試,讓它對 wordpress 更友好。
function role_redirections() {
$loggedin_user = wp_get_current_user();
if (!is_user_logged_in() || !in_array( 'memorial_user', (array) $loggedin_user->roles )) {
$location1 = get_option( 'siteurl' ) .'/member-portal/';
header('Location: '.$location1.'');
}
}
add_action('wp_head', 'role_redirections');
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.