在 WordPress 登录页面上将 IP 地址列入白名单并重定向用户
Whitelisting ip addresses and redirecting users on WordPress login page
我想为自己的WordPress网站做一个插件,如果从特定地址访问WordPress的登录页面,则显示,否则重定向到主页。
示例:
if(current_page == login && ip_address != xxx.xxx.xxx.xxx)
redirect_to_homepage;
我制作了一个简单的插件,可以读取当前访问者的 ip 地址并可以访问当前页面 url,但是该插件不会 运行 在登录页面上。该插件在 example.com/index.php
等所有 public 页面上执行,但不在 example.com/wp-login.php
上执行
我假设我应该使用:
add_action('template_redirect', 'ss_check_login');
这样我就可以在发送 headers 之前重定向页面。我说得对吗?
如何在 WordPress 登录页面上执行插件(及其代码)。
我想知道 add_action 用于重定向的是什么?
我不想使用 .htaacess
。
您可以通过多种方式进行设置。例如,您可以使用 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,但是还有其他方法呢!
我想为自己的WordPress网站做一个插件,如果从特定地址访问WordPress的登录页面,则显示,否则重定向到主页。
示例:
if(current_page == login && ip_address != xxx.xxx.xxx.xxx)
redirect_to_homepage;
我制作了一个简单的插件,可以读取当前访问者的 ip 地址并可以访问当前页面 url,但是该插件不会 运行 在登录页面上。该插件在 example.com/index.php
等所有 public 页面上执行,但不在 example.com/wp-login.php
我假设我应该使用:
add_action('template_redirect', 'ss_check_login');
这样我就可以在发送 headers 之前重定向页面。我说得对吗?
如何在 WordPress 登录页面上执行插件(及其代码)。
我想知道 add_action 用于重定向的是什么?
我不想使用 .htaacess
。
您可以通过多种方式进行设置。例如,您可以使用 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,但是还有其他方法呢!