search

Home  >  Q&A  >  body text

Redirect to another page in WordPress if referrer is not a specific URL

I'm using WordPress with Elementor and I want only specific pages from a specific URL to be accessible. I saw from other answers to similar questions that I could use this:

add_action( 'template_redirect', 'wpse15677455_redirect' );

function wpse15677455_redirect() {

  $value = ('https://mywebsite.com/quotaton/') ;
    if (!is_page(555) & wp_get_referer() !== $value ) {

       wp_safe_redirect( get_home_url() );

    }
 };

I tried using this in my theme's function.php but it returned the error "Unable to communicate with the server to check for a fatal error". I tried deactivating all plugins except elementor but same result. I tried without the add_action call but it also didn't do anything although it gave no error. I can't seem to find the right place/way to use this feature.

P粉407936281P粉407936281315 days ago394

reply all(1)I'll reply

  • P粉336536706

    P粉3365367062024-01-04 00:33:11

    Try the code, I believe the problem is that you are missing the single ampersand (&) for the And operator. Also, if you use is_page to check for a "specific page", you probably don't need the not(!) operator...

    if (is_page(555) && wp_get_referer() !== $value ) {
    

    reply
    0
  • Cancelreply