0

I have a custom profile page for my users. I need to have wordpress redirect the homepage to the user's profile page if they are logged in. If they are not logged in or registered, wordpress would show the normal home page.

My homepage url string would look like this for logged in users: http://my_site.com/user/some_user ... with of course "some_user" being called up as the logged in user's user name.

I've been trying to piece together bits of code I've found on this site but nothing has worked so far.

Thank you for any suggestions

  • Try using this plugin: Peter's Login Redirect – m4n0 May 7 '15 at 17:54
  • I've tried Peter's Login Redirect.. It doesn't work because it doesn't allow me to dynamically change the page it's redirecting to based on user. – Sam May 7 '15 at 18:18
  • You need to break your problem down into easier smaller problems, e.g. "How do I test if a user is logged in" or "How do I redirect a user from one page to another?" – Tom J Nowell May 8 '15 at 9:44
1

Check that your are on the home page, and that the user is logged in, and redirect if both are true:

function redir_user_wpse_187622() {
  if (is_home() && is_user_logged_in()) {
    $user_id = get_current_user_id();
    wp_safe_redirect(get_author_posts_url( $user_id));
  }
}
add_action('template_redirect','redir_user_wpse_187622');
| improve this answer | |
  • Thanks for your quick reply. I'm a novice and want to make sure I understand... I'm guessing I put this somewhere on my functions.php file and then call up the function on my header.php page? is that right? – Sam May 7 '15 at 18:32
  • You can put the whole thing in functions.php. No need to "call" anything. That is what the add_action() is for. – s_ha_dum May 7 '15 at 18:35
  • Thanks again, that helps. I'm almost there... so I'm also guessing that I need to swap out 'abc' to the page I'm wanting to redirect to, I'm just not sure how to code the string to bring in the user name at the end of the url string. for that line I've unsuccessfully tried this: wp_safe_redirect(home_url() . '/user/' . wp_get_current_user()->ID ); – Sam May 7 '15 at 19:10
  • @Sam: See the edit. – s_ha_dum May 7 '15 at 19:35
  • 1
    @Sam this code redirects to the author archive page, which is what you should be using to build your profile page. Your responses imply instead that you've used a page and a custom page template. If so you'll need to replaced the get_author_posts_url part with code to grab the permalink of that page ( or do things the recommended way ). If you don't know how to do that, I recommend creating a new question with "How do I get the permalink of a page if I know it's slug?" – Tom J Nowell May 8 '15 at 9:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.