0

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);
}

}

1 Answer 1

0

Try 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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.