0

I need a little help with what I believe would be PHP.

http://www.domain.com/author/username/achievements/

Depending on the username of the person logged in, I would like to create a URL that redirects a user to the aforementioned page. To do this, username needs to be replaced with their logged in username.

Is there an easy way to do this?

1

2 Answers 2

1

WordPress has several functions for getting user information, for example the following ones:

1

You can use wp_get_current_user()
This should do the trick:

$current_user = wp_get_current_user();
if ( is_user_logged_in() ) {
    wp_redirect( 'http://www.domain.com/author/'.$current_user->user_login.'/achievements/');  
    exit;
} else {
    echo 'You have no business being here';
}

Also see the wp_get_current_user() documentation.

If you run in a:Warning: Cannot modify header information - headers already sent by

Add this to your functions.php:

add_action('init', 'do_output_buffer');
function do_output_buffer() {
    ob_start();
}
3
  • The »logged-in»-check should be done via is_user_logged_in Commented Apr 13, 2015 at 11:59
  • Yes I agree. I changed my answer Commented Apr 13, 2015 at 12:01
  • Note that output buffering rarely is needed, just do your redirect early enough, so before the header has been send. @JimFender Commented Apr 13, 2015 at 12:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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