Redirect to another page in WordPress if referrer is not a specific URL
P粉407936281
P粉407936281 2024-01-03 09:59:50
0
1
408

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粉407936281
P粉407936281

reply all(1)
P粉336536706

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 ) {
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template