[Support request] Create a redirection for a page

Home Forums Support [Support request] Create a redirection for a page

Home Forums Support Create a redirection for a page

Viewing 15 posts - 1 through 15 (of 25 total)
  • Author
    Posts
  • #999897
    Esteban

    Hello, I want to create a redirection on a page, only for people that are not log in.

    For example:

    If somebody click on /my-account or just type that page, would be great to send them to another page, or to login account.

    Is there any change to configure this?

    #1000097
    David
    Staff
    Customer Support

    Hi there,

    you can use a redirect function like this PHP Snippet:

    add_action( 'wp', 'redirect' );
    function redirect() {
      if ( is_page('my-account') && !is_user_logged_in() ) {
          wp_redirect( home_url('/login') );
          die();
      }
    }
    #1000133
    Esteban

    Thanks for that.

    Can you tell me how to configure that and where? I have no idea of coding.

    Where i put the web to be redirected?

    #1000151
    David
    Staff
    Customer Support

    Its a PHP snippet – this explains where to add the code:
    https://docs.generatepress.com/article/adding-php/

    This part of the code is the page the user is trying to go to:

    is_page('my-account')

    Change my-account to the page slug you need.

    This is where they will be redirected to:

    home_url('/login')

    So if you wanted it to go to mywebsite.com/special-page you would change it to:

    home_url('/special-page')

    #1000262
    Esteban

    Hello, i think is not working the code:

    add_action( ‘wp’, ‘redirect’ );
    function redirect() {
    if ( is_page(‘mi-cuenta’) && !is_user_logged_in() ) {
    wp_redirect( home_url(‘/acceder’) );
    die();
    }
    }

    Is okay?

    #1000266
    Esteban

    Now is working thanks

    #1000287
    David
    Staff
    Customer Support

    Glad to be of help.

    #1004946
    Esteban

    Hello,

    I am trying to put this code:

    add_action( ‘wp’, ‘redirect’ );
    function redirect() {
    if ( is_page(‘cursos’) && !is_user_logged_in() ) {
    wp_redirect( home_url(‘/acceder’) );
    die();
    }
    }

    And an error message appear:

    No te preocupes
    El fragmento de código que intentas guardar produce un error fatal en la línea 7:

    Cannot redeclare redirect() (previously declared in /home/estebanu/public_html/sharkopciones/wp-content/plugins/code-snippets/php/snippet-ops.php(361) : eval()’d code:3)
    La versión anterior del fragmento de código no ha cambiado, y el resto del sitio debería funcionar como antes.

    Por favor, usa el botón atrás del navegador para volver a la página anterior e intenta arreglar el error en el código. Si lo prefieres, puedes cerrar esta página y descartar los cambios que acabas de hacer. No se hará ningún cambio en este sitio.

    #1005248
    Tom
    Lead Developer
    Lead Developer

    You can’t have multiple functions with the same name. You’ve used the function redirect() before, so you need to rename it to something else.

    #1007129
    Esteban

    Ohh i get it.

    Can you give an example with the same function?

    Because i do not know if i have to change something else.

    Another question.

    If i want to do the opposite:

    Redirect a person who is logged in to another page.

    Is this possible?

    Thanks

    #1007140
    David
    Staff
    Customer Support

    So here’s the original example i gave:

    add_action( 'wp', 'redirect' );
    function redirect() {
      if ( is_page('my-account') && !is_user_logged_in() ) {
          wp_redirect( home_url('/login') );
          die();
      }
    }

    redirect is the name of the function and must be unique.

    So your next function would need to be called something different e.g redirect_cursos which means the code would start like this:

    add_action( 'wp', 'redirect_cursos' );
    function redirect_cursos() {

    For logged in – again original example:

    if ( is_page('my-account') && !is_user_logged_in() ) {

    This !is_user_logged_in() means NOT logged in.

    So for logged in it would look like this:

    if ( is_page('my-account') && is_user_logged_in() ) {

    ie. just remove the !

    #1007889
    Esteban

    Thanks for the example!

    I do not know the redirect is not working:

    Is something wrong:

    add_action( ‘wp’, ‘redirect_cursos’ );
    function redirect_cursos() {
    if ( is_page(‘cursos’) && !is_user_logged_in() ) {
    wp_redirect( home_url(‘/acceder’) );
    die();
    }
    }

    #1008246
    Tom
    Lead Developer
    Lead Developer
    add_action( 'wp', function() {
        if ( is_page( 'cursos' ) && ! is_user_logged_in() ) {
            wp_redirect( home_url( '/acceder' ) );
            die();
        }
    } );

    In the above code, it should do the redirect if:

    1. The page is yoursite.com/cursos
    2. The user is not logged in.

    #1008877
    Esteban

    Hello!

    I do not know why isnt working.

    I add the code with Code Snippet, but this one in particular is not working.

    This is the link https://sharkopciones.estebanurrutia.com/cursos/

    What would be the problem?

    #1008980
    Tom
    Lead Developer
    Lead Developer

    Try this:

    add_action( 'template_redirect', function() {
        if ( is_page( 'cursos' ) && ! is_user_logged_in() ) {
            wp_redirect( home_url( '/acceder' ) );
            die();
        }
    } );
Viewing 15 posts - 1 through 15 (of 25 total)
  • You must be logged in to reply to this topic.