admin管理员组文章数量:1425784
I need to prohibit certain IP Address from visiting my wordpress site. Hence i have used below code in my child theme function.php
add_action('template_include', 'restrict_user_access');
function restrict_user_access() {
$ipAddress = $_SERVER['REMOTE_ADDR'];
if($ipAddress === 'XX.XX.XXX.XXX') {
return "home/www/html/blog/wp-content/themes/theme-child/restrictusers.php";
}
}
}
I can able to load this template & display the content for this IP whereas for other IP Address i get empty page.
Am i missing any?
My wordpress version is 4.9.10.
I also need to replicate this same process in other wordpress site v5.0.2.
I need to prohibit certain IP Address from visiting my wordpress site. Hence i have used below code in my child theme function.php
add_action('template_include', 'restrict_user_access');
function restrict_user_access() {
$ipAddress = $_SERVER['REMOTE_ADDR'];
if($ipAddress === 'XX.XX.XXX.XXX') {
return "home/www/html/blog/wp-content/themes/theme-child/restrictusers.php";
}
}
}
I can able to load this template & display the content for this IP whereas for other IP Address i get empty page.
Am i missing any?
My wordpress version is 4.9.10.
I also need to replicate this same process in other wordpress site v5.0.2.
Share Improve this question asked Jun 7, 2019 at 4:27 Prasanth RPrasanth R 31 bronze badge 01 Answer
Reset to default 2template_include
is a filter - it takes one argument and it should always return a value (a template to include).
Your function doesn’t do that. It doesn’t take any arguments and it returns value only if the condition is true - so if it’s not, it returns no value, so no template will be included in such case.
Here’s the fix:
add_action('template_include', 'restrict_user_access');
function restrict_user_access( $template ) {
$ipAddress = $_SERVER['REMOTE_ADDR'];
if($ipAddress === 'XX.XX.XXX.XXX') {
return "home/www/html/blog/wp-content/themes/theme-child/restrictusers.php";
}
}
return $template;
}
Another problem is that you should never use absolute paths in such filters. There are functions that will help you locate the template based on its file. So instead of this:
return "home/www/html/blog/wp-content/themes/theme-child/restrictusers.php";
You should do this:
$tpl = locate_template( array( 'restrictusers.php' ) );
if ( $tpl ) return $tpl;
本文标签: Include Template Based on User IP Address
版权声明:本文标题:Include Template Based on User IP Address 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745429265a2658253.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论