我想為我自己的WordPress網站制作一個插件,其中如果從特定地址訪問WordPress的登錄頁面,則顯示它,否則它會重定向到主頁。
例子:
if(current_page == login && ip_address != xxx.xxx.xxx.xxx)
redirect_to_homepage;
我做了一個簡單的插件,可以讀取當前訪問者的ip地址并可以訪問當前頁面的url,但是該插件不會在登錄頁面上運行。該插件在所有公共頁面上執行,例如example.com/index.php,但不在example.com/wp-login.php
我假設我應該使用:
add_action('template_redirect', 'ss_check_login');
這樣我就可以在發送標頭之前重定向頁面。我對么?
如何在 WordPress 登錄頁面上執行插件(及其代碼)。
我想知道用于重定向的 add_action 是什么?
我不想使用.htaacess.
uj5u.com熱心網友回復:
您可以通過多種方式進行設定。例如,您可以使用login_init動作掛鉤。使用以下代碼,我為每個步驟添加了注釋:
add_action('login_init', 'redirecting_users');
function redirecting_users()
{
// Getting the current page
global $pagenow;
// Whitelisting ip addresses in an array so that you could add more than one ip address
$allowed_ip_addresses = array('0000000000', '111111111111');
// Getting the current ip of the user
$current_ip_address = $_SERVER['REMOTE_ADDR'];
if (
'wp-login.php' == $pagenow
&&
!in_array($current_ip_address, $allowed_ip_addresses)
)
{
wp_safe_redirect(site_url());
exit;
}
};
筆記:
- 就像我在代碼注釋中所說的那樣,我使用了一個陣列來將 ip 地址列入白名單,以便您可以添加多個 ip 地址。
- 我以前
$_SERVER['REMOTE_ADDR']獲取當前ip,但還有其他方法!
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/400198.html
