0

I was wondering why (and how to) restricting pages by the following criteria does not work. I wanted to restrict the contact page due to spam. The code is being added and activated on front-end using the Code Snippets plugin:

function contactusnot()
{
    if ( !is_user_logged_in() && (is_page( 220 ) || is_page( 'Contact us' ) || is_page( 'contact-us' ))) 
    {
        header("Location: https://google.com/");
        exit();
    }
}
add_action('init', 'contactusnot');

I've tried removing the function, hoping it will force the filter to work before anything else and also using only the ID/slug/title one by one. Now working for my Woocommerce site.

2
  • It's possible that Code Snippets loads the code on or after the init hook, which means your code wouldn't execute. Try moving the code outside of the hook callback, and see what happens.
    – Caleb
    Jul 31 at 15:25
  • nothing. no redirect with or without function Jul 31 at 18:19

1 Answer 1

0

The init hook is called too early for your needs. And you should use wp_redirect(). Try this:

function contactusnot() {
    if ( ! is_user_logged_in() && (is_page( 220 ) || is_page( 'Contact us' ) || is_page( 'contact-us' )))  {
       wp_redirect( 'https://google.com/' ); 
       exit;
    }
}
add_action( 'template_redirect', 'contactusnot' );
1
  • same result, not redirecting visitors Jul 31 at 18:18

Your Answer

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

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