- This topic has 12 replies, 5 voices, and was last updated 6 years, 5 months ago by
David.
-
AuthorPosts
-
August 16, 2015 at 2:42 am #129685
stardrive
Dear Tom,
Please, when a user wants or clicks to go to a specific page (e.g. page1), I want the user to be re-directed to another page (e.g. page2) automatically.
Please, kindly assist
Regards,
Stardrive
August 16, 2015 at 6:32 am #129720Jean Paiva Developer
Hey Stardrive, you can use this function:
function my_page_redirect() { if( is_page( #ID ) ) { wp_redirect( home_url( '/yourpage/' ) ); exit(); } } add_action( 'template_redirect', 'my_page_redirect' );If is a post page just change
is_page()foris_single()The function goes into your child theme functions file.
Jean
August 16, 2015 at 6:35 am #129721stardrive
Hello Jean,
I will try your code and get back to you
Regards,
Stardrive
August 16, 2015 at 9:40 am #129763stardrive
Thank you so much, Jean. It is OK now
Regards,
Stardrive
August 16, 2015 at 10:27 am #129781Tom
Lead DeveloperLead DeveloperGreat function, Jean 🙂
There’s also tons of redirection plugins out there for anyone who doesn’t want to use a function – just do a quick Google for “WordPress redirection plugin”.
October 31, 2019 at 6:44 pm #1050158Patrik
Hi!
I am wondering – can this also be done with the new “Elements” from GeneratePress? I am asking because if possible, I’d prefer to avoid a plugin.
Thanks a lot.
November 1, 2019 at 5:18 am #1050396David
StaffCustomer SupportHi there,
yeah – i can’t see why not.
You could use the Custom Hook to apply your function to thetemplate_redirecthook.November 1, 2019 at 8:01 am #1050568Patrik
Thanks for the quick reply David. I think this would require me some more digging, as I’m a newbie to WP and hooks. I found out that it’s better to use
wp_safe_redirectinstead ofwp_redirect, and also the documentation says it’s possible to do 301 redirects with that function (https://developer.wordpress.org/reference/functions/wp_redirect/). So that’s great in principle. I just don’t know how to execute it with GP’s Elements hooks 🙂I am using https://en-gb.wordpress.org/plugins/redirection/ for now – the plugin is from an Automattic
employee and has been around for ages and been maintained so it looks like quite a safe bet.November 1, 2019 at 8:58 am #1050614David
StaffCustomer SupportThe
template_redirectis a Hook so you can add any function ( that is relative ) to that hook. Whether you call the wp_redirect or wp_safe_redirect function it doesn’t matter.Personally i would stick with the plugin as its a lot less aggravation and will have covered all the complexities of 301s etc.
November 4, 2019 at 6:18 am #1052748Patrik
Thanks David. What I meant was that I do not know how/where/what code to put in to the Elements screen – I can see Jean’s snippet above but it’s not clear to me how to put this into GP Elements.
Anyway I am definitely sticking with the plugin, it’s quite great, and as mentioned secure/trustworthy/maintained enough – but might be good to know for reference or future readers how to do it in GP Elements.
Thanks again!
November 4, 2019 at 6:31 am #1052761David
StaffCustomer SupportJust tested it – and unfortunately not possible – looks like Tom kept these hooks out of harms way for security reasons.
November 4, 2019 at 8:06 am #1052967Patrik
Thank you for checking, good to know!
November 4, 2019 at 8:08 am #1052972David
StaffCustomer SupportYou’re welcome
-
AuthorPosts
- You must be logged in to reply to this topic.