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
Add a comment  | 

3 Answers 3

Reset to default 3

First 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