Archived topic
Create a redirection for a page
24 replies · Started by esteban on September 3, 2019
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?
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();
}
}
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?
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')
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?
Now is working thanks
Glad to be of help.
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.
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.
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
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 !
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();
}
}
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.
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?
Try this:
add_action( 'template_redirect', function() {
if ( is_page( 'cursos' ) && ! is_user_logged_in() ) {
wp_redirect( home_url( '/acceder' ) );
die();
}
} );