skip to Main Content

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.

3

Answers


  1. 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' );
    

    is_page(‘contact’) => ‘contact’ is page slug.
    is_page(150) => ‘150’ is page ID.

    Login or Signup to reply.
  2. 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 ) {
    
    Login or Signup to reply.
  3. If the link that has to be redirected comes from a page, you could use a redirection plugin like this one: https://it.wordpress.org/plugins/page-links-to/

    Once activated you just edit the page you want to be redirected inserting the correct link in the page links to field.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search