Title: use variables in php redirect?
Last modified: August 22, 2016

---

# use variables in php redirect?

 *  Resolved [tanmccuin](https://wordpress.org/support/users/tanmccuin/)
 * (@tanmccuin)
 * [11 years, 11 months ago](https://wordpress.org/support/topic/use-variables-in-php-redirect/)
 * I’m attempting write a bit of PHP in the template to perform a redirect – essentially,
   once users are logged in, they are redirected to (for example) site.com/staff/
   staffnamevariable – this is all working great.
 *  However, what i’m attempting to do is to first check that the user is logged
   in already or not, using is_user_logged_in – from there running wp_redirect –
   to send the user, ideally back to their unique redirect address. (eg site.com/
   staff/staffnamevariable) so that the user is always pushed to their appropriate
   user page instead of being presented with the “you are already logged in” screen.
 * I’m unsure however, how to access this variable outside of the plugin.
 * [https://wordpress.org/plugins/peters-login-redirect/](https://wordpress.org/plugins/peters-login-redirect/)

Viewing 2 replies - 1 through 2 (of 2 total)

 *  Thread Starter [tanmccuin](https://wordpress.org/support/users/tanmccuin/)
 * (@tanmccuin)
 * [11 years, 11 months ago](https://wordpress.org/support/topic/use-variables-in-php-redirect/#post-5169628)
 * So i’ve managed to answer my own question – and the answer is outside of the 
   plugin, and just uses WP calls. But for anyone looking to accomplish a similar
   redirect here’s what i’ve got.
 *     ```
       <!-- Already logged in redirect -->
       <?php
       $current_user = get_currentuserinfo();
       if ( is_user_logged_in() ) {
       	wp_redirect( home_url('/client/'. $current_user->user_login .'') ); exit;
       	} else { ?>
       <!-- end Redirect-->
       ```
   
 *  [meglets](https://wordpress.org/support/users/meglets/)
 * (@meglets)
 * [11 years, 11 months ago](https://wordpress.org/support/topic/use-variables-in-php-redirect/#post-5170001)
 * Newbie question here. Do I put this code in header.php? If so, can I edit it 
   to read something like
 * `<!-- Already logged in redirect --><?php$current_user = get_currentuserinfo();
   if ( is_user_logged_in() && is_page('login')) { wp_redirect( home_url('/client/'.
   $current_user->user_login .'') ); exit; } else { ?><!-- end Redirect—>`
 * Right now when I try that, the whole site just breaks. If I remove the ‘else {‘
   portion, the site works again, but the login page doesn’t. I know this question
   has been asked millions of times, but I can’t find the answer on the internet
   and I’ve been looking for hours! (Full disclosure: I do not know php, but I do
   know other languages).

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘use variables in php redirect?’ is closed to new replies.

 * ![](https://ps.w.org/peters-login-redirect/assets/icon-256x256.png?rev=2597671)
 * [LoginWP (Formerly Peter's Login Redirect)](https://wordpress.org/plugins/peters-login-redirect/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/peters-login-redirect/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/peters-login-redirect/)
 * [Active Topics](https://wordpress.org/support/plugin/peters-login-redirect/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/peters-login-redirect/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/peters-login-redirect/reviews/)

 * 2 replies
 * 2 participants
 * Last reply from: [meglets](https://wordpress.org/support/users/meglets/)
 * Last activity: [11 years, 11 months ago](https://wordpress.org/support/topic/use-variables-in-php-redirect/#post-5170001)
 * Status: resolved