- This topic has 24 replies, 2 voices, and was last updated 3 years, 2 months ago by David.
-
AuthorPosts
-
September 3, 2019 at 6:25 am #999897Esteban
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?
September 3, 2019 at 8:01 am #1000097DavidStaffCustomer SupportHi 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(); } }
September 3, 2019 at 8:47 am #1000133EstebanThanks 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?
September 3, 2019 at 9:10 am #1000151DavidStaffCustomer SupportIts 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')
September 3, 2019 at 11:30 am #1000262EstebanHello, 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?
September 3, 2019 at 11:39 am #1000266EstebanNow is working thanks
September 3, 2019 at 12:09 pm #1000287DavidStaffCustomer SupportGlad to be of help.
September 9, 2019 at 9:13 am #1004946EstebanHello,
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.
September 9, 2019 at 4:06 pm #1005248TomLead DeveloperLead DeveloperYou 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.September 11, 2019 at 1:00 pm #1007129EstebanOhh 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
September 11, 2019 at 1:18 pm #1007140DavidStaffCustomer SupportSo 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
!
September 12, 2019 at 9:14 am #1007889EstebanThanks 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();
}
}September 12, 2019 at 5:29 pm #1008246TomLead DeveloperLead Developeradd_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.September 13, 2019 at 11:18 am #1008877EstebanHello!
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?
September 13, 2019 at 3:37 pm #1008980TomLead DeveloperLead DeveloperTry this:
add_action( 'template_redirect', function() { if ( is_page( 'cursos' ) && ! is_user_logged_in() ) { wp_redirect( home_url( '/acceder' ) ); die(); } } );
-
AuthorPosts
- You must be logged in to reply to this topic.