[英]Restrict page access in wordpress based on the logged in user's email
我想將我的 wordpress 網站上的頁面限制為與我提供的電子郵件匹配的用戶。 如果某些其他用戶或未登錄的用戶打開該頁面,則應該收到一條消息,表明他們無權查看該頁面。 除了允許的用戶之外,管理員還應該能夠查看該頁面。 我搜索了很多,但沒有提供此功能的插件,只有基於用戶角色的限制可用。 任何幫助表示贊賞。
我建議您根據用戶role
而不是 email 地址檢查用戶。
您可以使用customrole
的內置用戶角色,例如編輯器,也可以使用此代碼創建您的自定義角色。
下面給出的示例代碼應該 go 到您的子主題的functions.php
。php 或者您可以安裝代碼片段插件以將自定義功能注入您的 WP 站點。
// Add custom role
add_role("customrole", __( "Custom Role" ),array('read' => true));
然后,您可以通過更改角色來分配要授予對某些頁面訪問權限的用戶。
我更喜歡使用簡碼將頁面限制為某個或多個角色。 您可以使用此代碼來創建您的自定義簡碼。 它將用戶重定向到無權訪問的 404 頁面。
// Make a certain page available only to customrole users
function shortcode_restricted_page() {
$current_user = wp_get_current_user();
$current_username = $current_user->user_login;
$role = $current_user->roles[0];
if ($role == 'customrole' || $role == 'administrator') {
// Access granted to the page
return;
}
else {
global $wp_query;
$wp_query->set_404();
status_header( 404 );
get_template_part( 404 ); exit();
}
}
add_shortcode('restricted_page', 'shortcode_restricted_page');
您現在可以將[restricted_page]
快照代碼添加到任何頁面的內容以輕松限制該頁面。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.