Home > Mobile >  How to redirect to another page if referrer url is not a specific url in wordpress?
How to redirect to another page if referrer url is not a specific url in wordpress?

Time:06-29

I'm using WordPress with Elementor, I want a certain page to be accessible only if it comes from a certain url. I saw from other answers in similar questions that I can 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 the function.php of the theme but it returns the error "Unable to communicate with server to check for fatal errors". I tried with all plugins deactivated except elementor but same result. I tried without the add_action call but, despite not giving errors, it also does nothing. I can't seem to find the right place/way to use this function.

CodePudding user response:

function custom_redirects() {
 
    if ( is_front_page() ) {
        wp_redirect( home_url( '/dashboard/' ) );
        die;
    }
 
    if ( is_page('contact') ) {
        wp_redirect( home_url( '/new-contact/' ) );
        die;
    }
 
}
add_action( 'template_redirect', 'custom_redirects' );

CodePudding user response:

Trying out the code, I believe the problem is that you're missing a single ampersand(&) for the And operator. Also, if is_page is used to check for the "certain page", maybe the not(!) operator isn't necessary...

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