Hi @chatty24
Regarding /me/
, what exactly is that? A page? A post?
@henrywright
It could be a blank WordPress page with title, “Redirecting…”
@chatty24 – I mean, how have you set up /me/
? Is it a page, a post, a member’s username etc?
OK cool
Try this:
function my_redirect() {
if ( is_page( 'me' ) && is_user_logged_in() ) {
$user = get_userdata( get_current_user_id() );
bp_core_redirect( home_url() . '/members/' . $user->user_login );
}
}
add_action( 'template_redirect', 'my_redirect' );
If you’re simply redirecting, then you don’t need a ‘me’ destination. Try:
function chatty_me_check() {
if( is_user_logged_in() ) {
global $wp;
$request = $wp->request;
$me = stripos( $request, '/me/' );
if( $me !== false ) {
wp_redirect( bp_loggedin_user_domain() );
exit();
}
}
}
add_action('wp', 'chatty_me_check');
@henrywright
Because on my website the usernames are on the root. That is —> example.com/username.
I removed . '/members/'
from the code. So, the final code was –
function my_redirect() {
if ( is_page( 'me' ) && is_user_logged_in() ) {
$user = get_userdata( get_current_user_id() );
bp_core_redirect( home_url() . $user->user_login );
}
}
But it took me to the /wp-admin/. Is there something wrong that I am doing?
Thanks
@shanebp
That did not redirected anywhere.
Because on my website the usernames are on the root
OK, this should do it:
function my_redirect() {
if ( is_page( 'me' ) && is_user_logged_in() )
bp_core_redirect( bp_loggedin_user_domain() );
}
add_action( 'template_redirect', 'my_redirect' );
Borrowed the bp_loggedin_user_domain()
idea from @shanebp 🙂
@henrywright
The code is working fine. But, now the website is showing this strange behavior. Whenever I try to go to something like, example.com/username/profile or /notifications etc.
It gets me back to, example.com/username
And if I remove the code the error goes away.
Any ideas why is that happening?
Thanks
That’s strange. The following condition should make sure the redirect occurs only if the user is logged-in and viewing page /me/
if ( is_page( 'me' ) && is_user_logged_in() )
@henrywright
I edited the code and now it is working as it should be. So, the final code is –
function my_redirect() {
if ( is_page( 'me' ) && is_user_logged_in() )
bp_core_redirect( bp_loggedin_user_domain() );
}
add_action( 'wp', 'my_redirect' );
I have replaced
add_action( 'template_redirect', 'my_redirect' );
with
add_action( 'wp', 'my_redirect' );
Thanks for your help, @henrywright & @shanebp
Just wanted to ask another question. If I want a single link to redirect to other things like notifications, then all I need is to do is this right,
bp_core_redirect( bp_loggedin_user_domain() . '/notifications' );
Thanks!
Great to see you got it working!
What do you mean by ‘single link’?
@henrywright
By single link I mean. Like, example.com/me/ redirects to the username of the person who is logged in. Now, I can just put this example.com/me/ link anywhere and it would redirect to the respective username of the people. This is what I mean by single link….
@henrywright
Just ignore the other question that I asked. I got it figured out.
Thank you so much for your help. Have a great weekend.
Thanks! 😀