Skip to:
Content
Pages
Categories
Search
Top
Bottom

Using Redirect to make single link for buddypress dynamic links


  • chatty24
    Participant

    @chatty24

    Hi

    I want to redirect from one page to another with the help of if statement in functions.php

    Here is what I want :

    if user is logged in and is on link “/me” then redirect to

    “<?php bp_loggedinuser_link() ?>”

    What I am trying to do is make a single link for buddypress dynamic links. So, if someone goes to “/me” he would be redirected to if own profile page.

    Can anybody tell me that how to write the above in form of code?

    Thanks

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

  • Henry Wright
    Moderator

    @henrywright

    Hi @chatty24

    Regarding /me/, what exactly is that? A page? A post?


    chatty24
    Participant

    @chatty24

    @henrywright

    It could be a blank WordPress page with title, “Redirecting…”


    Henry Wright
    Moderator

    @henrywright

    @chatty24 – I mean, how have you set up /me/? Is it a page, a post, a member’s username etc?


    chatty24
    Participant

    @chatty24

    @henrywright

    It is a page.


    Henry Wright
    Moderator

    @henrywright

    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' );

    shanebp
    Moderator

    @shanebp

    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');

    chatty24
    Participant

    @chatty24

    @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


    chatty24
    Participant

    @chatty24

    @shanebp

    That did not redirected anywhere.


    Henry Wright
    Moderator

    @henrywright

    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 🙂


    chatty24
    Participant

    @chatty24

    @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


    Henry Wright
    Moderator

    @henrywright

    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() )


    chatty24
    Participant

    @chatty24

    @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!


    Henry Wright
    Moderator

    @henrywright

    Great to see you got it working!

    What do you mean by ‘single link’?


    chatty24
    Participant

    @chatty24

    @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….


    chatty24
    Participant

    @chatty24

    @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! 😀

Viewing 15 replies - 1 through 15 (of 15 total)
  • The topic ‘Using Redirect to make single link for buddypress dynamic links’ is closed to new replies.
Skip to toolbar