admin管理员组文章数量:1122832
I'm a complete noob but trying to learn, and I'm stumped.
I have a login page as my static homepage, and want to make an if statement, so that if the user is logged in, and goes to the static home page, they are redirected to another page with content.
So far, this is what I have, but it doesn't work:
if ( is_user_logged_in() && is_page( home_url ) ) {
wp_redirect('/') ;
}
Can you please not only give me an answer, but a little explanation for me to figure out how I went wrong?
Thanks to anyone that can help.
I'm a complete noob but trying to learn, and I'm stumped.
I have a login page as my static homepage, and want to make an if statement, so that if the user is logged in, and goes to the static home page, they are redirected to another page with content.
So far, this is what I have, but it doesn't work:
if ( is_user_logged_in() && is_page( home_url ) ) {
wp_redirect('http://example.com/') ;
}
Can you please not only give me an answer, but a little explanation for me to figure out how I went wrong?
Thanks to anyone that can help.
Share Improve this question asked Feb 3, 2018 at 2:03 StudentStudent 211 silver badge2 bronze badges 3- Do you have debugging enabled? – Milo Commented Feb 3, 2018 at 2:30
- Not that I'm aware of... is that good to do when learning? Also, the answers below totally helped me. :) – Student Commented Feb 3, 2018 at 13:03
- Student or seasoned professional, you should always develop with debugging enabled! – Milo Commented Feb 3, 2018 at 13:48
3 Answers
Reset to default 3First of all, home_url
doesn't provide anything. You should use the is_home()
function instead.
After that, you need to exit the script when you are trying to redirect. So, your full code should be like this:
if ( is_user_logged_in() && is_home() ) {
wp_redirect('http://example.com/') ;
exit();
}
You should also hook into the right action hook. The proper hook to use for redirection is the template_redirect
:
add_action ( 'template_redirect', 'redirect_my_homepage' );
function redirect_my_homepage(){
if ( is_user_logged_in() && is_home() ) {
wp_redirect('http://example.com/') ;
exit();
}
}
Well, you need to check fi the user is logged with 'is_user_logged_in()'. Check the Code Reference
After that, you need to check if the user is viewing the front page with 'is_front_page()'. Check the Code Reference
After that, you need to redirect with 'wp_redirect()'. Check the Code Reference
And last, but not least, you need to 'exit', as it is shown on 'wp_redirect()'.
So, your final code should look like:
if ( is_user_logged_in() && is_front_page() ) {
wp_redirect('http://yoursite.com/another-page') ;
exit;
}
you can use 'exit' or 'die', as they are kinda the same, but you must not forget to use one of them.
Here is the full code, tested and working fine (to be added in functions.php) :
add_action ( 'template_redirect', 'redirect_my_homepage' );
function redirect_my_homepage(){
if ( is_user_logged_in() && is_front_page() ) {
wp_redirect('/my-url');
exit();
}
}
本文标签: PHP If user is logged in amp on home page redirect
版权声明:本文标题:PHP If user is logged in & on home page redirect 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736308546a1933699.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论