![](/img/trans.png)
[英]WordPress restricted page redirect for non logged users to login page with PHP
[英]How to redirect non logged in users to Login page on Wordpress?
我知道這是一個被問過數百萬次的問題,但我找不到適合現在的 Wordpress 版本和/或我的特定情況的答案。
我有一個安裝了 Buddypress 的 Wordpress 網站。
我想要: - 未登錄的用戶只能看到頁面 wp-login.php - 登錄的用戶可以訪問整個網站
我只是想不通,這是不可能的。 我要么更改站點地址,要么分配頁面,或者功能……什么都不起作用,總是出現錯誤或 404 頁面錯誤等……
基本上:你到達網站,你沒有登錄= Wp-Login.php,如果你登錄頁面“成員”
謝謝 !
根據您的最終目標,有很多不同的方法(使用WP登錄頁面,自定義登錄頁面等)。 您可以嘗試將其添加到主題的functions.php文件中:
if ( ( is_single() || is_front_page() || is_page() || is_archive() || is_tax() )
&& ! is_page( 'login' ) && ! is_page('register') && ! is_user_logged_in() ) {
auth_redirect();
}
或者您可以使用插件強制登錄
更新
從理論上講,您可能只是使用此方法,而沒有進行測試...
if( ! is_page('login') && ! is_page('register') && ! is_user_logged_in() ) {
auth_redirect();
}
在這里看看
https://wordpress.org/support/topic/how-to-auth_redirect-to-specific-page
<?php
if( !is_user_logged_in() ) {
wp_redirect('http://somepagehere');
exit;
}
?>
您也可以將其添加到您的functions.php文件中,而不是我之前所說的內容:
function admin_redirect() {
if ( !is_user_logged_in()) {
wp_redirect( home_url('/{custom page goes here}/') );
exit;
}
}
add_action('get_header', 'admin_redirect');
只需使用此wordpress插件https://wordpress.org/plugins/restrict-user-access/來控制誰可以/不能訪問您的網站。
如果您用Google搜索它,可能會得到大量類似的插件。
您要它檢查用戶當前是否未嘗試登錄或注冊,如果沒有將其重定向到您想要的任何頁面。
if ( !is_user_logged_in() && !is_page( 'login' ) && ! is_page('register') ) {
//redirect user, create account with them, do a hoola-hoop
}
確保將此代碼放在get_header()之前的頁面文件中。
使用指南: https : //vvcares.com/blog/post/wordpress-buddypress-hide-page-from-non-members
如何對非登錄用戶隱藏會員頁面?
這是保護您的 Wordpress BuddyPress 會員頁面的簡單步驟。 將這個簡單的片段添加到您主題的“functions.php”文件中。 就是這樣..
如果訪問者未登錄並嘗試訪問敏感的 BuddyPress 頁面,以下代碼片段示例將重定向訪問者。
將嘗試訪問私人內容的未登錄用戶重定向到您的首頁或主頁
//Hide for non-logged-in users (public visitors)
function bp_logged_out_page_template_redirect() { if( ! is_user_logged_in() && is_page( 'members' )|| is_page( 'activity' ) || bp_is_user() ) { wp_redirect( home_url( 'https://vvcares.com/?buddypressBlogPostRedirect' ) ); exit(); } } add_action( 'template_redirect', 'bp_logged_out_page_template_redirect' );
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.