內容簡介
若使用者未登入,此外掛會將其重新導向至登入頁面,而在登入後,使用者會被重新導向到原始進入頁面。此外,此外掛也提供能夠覆寫重新導向的篩選器,以供進階使用者使用。
此外掛的原則是將所有使用者 - 從每一篇文章、頁面、檔案等等 - 重新導向至登入頁面(通常是 wp-login.php)。除了覆寫篩選器外,此外掛不會進行其他任何操作。
覆寫重新導向
注意:這個外掛不一定適合您,會員外掛可能更適合。Chris Lema 在這裡寫了關於 +30 會員外掛的優秀評論:http://chrislema.com/category/memberships-plugins/
如果您需要此外掛,而且想要在特定條件下排除特定視圖,則可以使用篩選器覆寫重新導向。
要覆寫重新導向,篩選器必須返回布林值 true。WordPress 核心提供了許多條件標籤,它們會傳回 true 或 false,或者您可以自己撰寫條件。
參考 The WordPress Codex 上的「條件標籤」頁面以獲取一些靈感。
https://codex.wordpress.org/Conditional_Tags
用法:
將範例複製/貼上/編輯到您的主題的 functions.php,或在 wp-content/mu-plugins/ 中創建一個新文件,以免編輯您的主題。
注意:請勿同時使用多個篩選器,因為這可能會產生意外的結果。相反地,請在一個篩選器中使用多個條件。
如果首頁是文章或頁面,則覆寫該部分:
add_filter( 'rtl_override_redirect', 'is_front_page' );
如果文章是「hello-world」,則會覆寫:
add_filter( 'rtl_override_redirect', function() {
return is_single( 'hello-world' );
});
如果頁面是「sample-page」,則會覆寫:
add_filter( 'rtl_override_redirect', function() {
return is_page( 'sample-page' );
});
如果頁面 ID 是 42,slug 是「sample-page」或標題是「About Me」,則會覆寫:
add_filter( 'rtl_override_redirect', function() {
return is_page( array( 42, 'sample-page', 'About Me' ) );
});
如果頁面 ID 是 42 或文章是「hello-world」,則會覆寫:
add_filter( 'rtl_override_redirect', function() {
if ( is_page( 42 ) || is_single( 'hello-world' ) ) {
return true;
}
});
外掛標籤
開發者團隊
② 後台搜尋「Redirect to login if not logged in」→ 直接安裝(推薦)
原文外掛簡介
Redirects users to the login page if the user is not logged in. After login the user gets redirected to the original entry page. For advanced users a filter is provided to override the redirect.
The principle behind this plugin is to redirect all users – from every post, page, archive, etc. – to the login page (usually wp-login.php). Except for the override filter it does nothing else.
Overriding the redirect
Note: This plugin may not be for you, a membership plugin might be a better fit. Chris Lema writes excellent reviews of +30 membership plugins here: http://chrislema.com/category/memberships-plugins/
If you do have a need for this plugin and you want to exclude specific views under specific conditions, a filter is provided to override the redirect.
To override the redirect the filter must return a boolean value of true. WordPress core provides many conditional tags that either return true or false or you can write your own conditionals.
Take a look at the Conditional Tags page on The WordPress Codex for some inspiration.
https://codex.wordpress.org/Conditional_Tags
Usage:
Copy/paste/edit an example to the functions.php of your theme or create a new file in wp-content/mu-plugins/ if you do not wish to edit your theme.
Note: Be carefull not to use multiple filters at the same time as that may cause unexpected results. Instead use multiple conditions in one filter.
Override if the front page is either posts or a page:
add_filter( ‘rtl_override_redirect’, ‘is_front_page’ );
Override if the post is ‘hello-world’:
add_filter( ‘rtl_override_redirect’, function() {
return is_single( ‘hello-world’ );
});
Override if the page is ‘sample-page’:
add_filter( ‘rtl_override_redirect’, function() {
return is_page( ‘sample-page’ );
});
Override if the page ID is 42, the slug is ‘sample-page’ or the title is ‘About Me’:
add_filter( ‘rtl_override_redirect’, function() {
return is_page( array( 42, ‘sample-page’, ‘About Me’ ) );
});
Override if the page ID is 42 or a post is ‘hello-world’:
add_filter( ‘rtl_override_redirect’, function() {
if ( is_page( 42 ) || is_single( ‘hello-world’ ) ) {
return true;
}
});
