Creating Custom URL’s with BuddyPress
Sometimes the default URL provided by BuddyPress is not quite what is required. Recently I had a requirement where the given url was not quite right.
I was asked to change www.examplesite.com/members/username/projects to www.example.com/projects/username for a particular use case.
For my solution I was able to use the BuddyPress CatchURI method in order to cantch and repurpose the requested URL in order to support the new format.
It’s important to note that the BP catch URI process triggers prior to WordPresses own permalink rewrites, to to make changes to BP URL’s it’s BP that needs to be told to expect the new format.
BP’s CatchURI process includes a filter – bp_url which passes the requested url prior to BP carrying out it’s own processing.
Here’s my code:
function bp_projects_rewrite_bp_uri($path) { if(strpos($path, 'projects/') === 1 && strlen( $path) > 5 ) { //Find the position of the second '/' and add one to fix the start of the username. $members_start_pos = strpos( $path, '/', 2 ) +1; //Find the end point of the username $members_end_pos = strpos( $path, '/', $members_start_pos ); //Strip the username out of the input URL $username = substr( $path, $members_start_pos, $members_end_pos - $members_start_pos ); //Return the reconstructed URL, which BP will accet as a valid page. return 'members/'.$username.'/card/'; } return $path; } add_filter('bp_uri', 'bp_projects_rewrite_bp_uri');
2 Comments
anisur rahman · August 1, 2021 at 10:52 am
I have BuddyPress installed in my WordPress. I don’t want to display the name or username in the profile of member, instead just User ID, like below Avatar instead of member’s name it will just show user ID.
For example:
User ID: 550
Note that I don’t want name fields hidden from registration page, a member will enter his/her name but it will be hidden on site so that members will not be able to see each other’s name. it will be hiden from urls,Members Page and individual profile page.
please help.
anisur rahman · August 4, 2021 at 2:14 pm
for privacy reasons, I need only admin to be able to see the user name. everyone else will see the user id. for example: User ID: 8564
This will be everywhere (Members Page and individual profile page and profile link/URL is a must for my website).
please help