我想为我自己的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,但还有其他方法!
0 评论