admin管理员组文章数量:1122832
Im currently trying to protect some WP pages by assigning a specific template to them.
Ive created the ACF fields "Referring URL" and "Redirect URL", as these could alter depending which page the template is assigned to.
What changes would be needed to have the template functionality go: IF referer is not "referring_url" redirect to "redirect_url"
Id like there to be no caching taking place either
However, the code below, placed just below get_header();
, doesn't seem to work consistently, either incorrect formatting, placement in the template file, or a cache issue.
if ( get_field('enable_portal_protection') && !current_user_can('administrator') ) {
$referring_url = get_field('referring_url');
$redirect_url = get_field('redirect_url');
$referer = $_SERVER['HTTP_REFERER'];
$location = "Location: " . $redirect_url;
if ( $referer != $referring_url) {
header($location);
}
}
Im currently trying to protect some WP pages by assigning a specific template to them.
Ive created the ACF fields "Referring URL" and "Redirect URL", as these could alter depending which page the template is assigned to.
What changes would be needed to have the template functionality go: IF referer is not "referring_url" redirect to "redirect_url"
Id like there to be no caching taking place either
However, the code below, placed just below get_header();
, doesn't seem to work consistently, either incorrect formatting, placement in the template file, or a cache issue.
if ( get_field('enable_portal_protection') && !current_user_can('administrator') ) {
$referring_url = get_field('referring_url');
$redirect_url = get_field('redirect_url');
$referer = $_SERVER['HTTP_REFERER'];
$location = "Location: " . $redirect_url;
if ( $referer != $referring_url) {
header($location);
}
}
Share Improve this question asked Jan 10, 2019 at 14:25 MikeMike 12 bronze badges1 Answer
Reset to default 0Try the template_redirect hook
function portal_protection() {
if ( get_field('enable_portal_protection') && !current_user_can('administrator') ) {
$referring_url = get_field('referring_url');
$redirect_url = get_field('redirect_url');
$referer = $_SERVER['HTTP_REFERER'];
if ( $referer != $referring_url) {
wp_redirect( $redirect_url );
exit;
}
}
}
add_action( 'template_redirect', 'portal_protection' );
Also use wp_redirect. it's the WordPress way of redirecting.
本文标签: page templateRedirect based on referer using Advanced Custom Fields
版权声明:本文标题:page template - Redirect based on referer using Advanced Custom Fields 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736289007a1928212.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论