Search code examples
phpwordpressurldynamicbuddypress

Create a dynamic buddypress tab


I am trying to create a dynamic buddypress tab linked to a dynamic url that looks like this localhost/wordpress/slug/username

this tab should appear to any user visiting another user's profile,, and when clicked it should redirect to the user visited and not the user visiting , I mean that the username at the end of my URL should be replaced by the username of the current profile visited

I am using the wp_redirect function to redirect the user to this url but the probleme is that i don't know how to write this url ?

I've tried to create a variable like so $user = wp_get_current_user(); to includ it like so wp_redirect( "http://localhost/wordpress/slug/$user->display_name" ); but the final result is http://localhost/wordpress2/slug/ i'm not even sure i should call this function current user so please someone help me

keep in mind that i'm writing my code in the function.php file in my theme folder

thats the code i added

$user = wp_get_current_user();




add_action( 'bp_setup_nav', 'create_tab3', 303 );
function create_tab3() {
    global $bp;
    bp_core_new_nav_item(
        array(
            'name'                    => 'my name',
            'slug'                    => 'slug', 
            'position'                => 21, 
            'default_subnav_slug'     => 'mySlug', // We add this submenu item below 
            'screen_function'         => 'redirect_user_to_tab3',
        )
    );

}
function redirect_user_to_tab3(){
    global $bp;
    wp_redirect( "http://localhost/wordpress2/slug/$user->display_name" );
    exit;
}

Thanks


Solution

  • Your redirect_user_to_tab3 function doesn't have a $user variable in its scope, so $user->display_name is undefined.

    You'll need to set $user = wp_get_current_user(); within the function.

    After that, read the docs on variable scope as they're quite important in PHP.