'Buddypress single profile page show 404 error

I just put this code to get user id.

<a href="<?php echo bp_get_member_user_id() ?>" target="_blank"
> class="">View Profile</a>

Now it's show the permalink as user id.

localhost/demo/members/5

But when I click view profile it's show 404.

How to solve this problem?



Solution 1:[1]

the function bp_get_member_user_id only return an integer (the userid)

in case your target is localhost/demo/members/5, try to use this code:

<a href="/demo/members/<?php echo bp_get_member_user_id(); ?>" target="_blank"
> class="">View Profile</a>

Solution 2:[2]

BuddyPress uses the user_name in a profile url. Unless you made changes to that functionality, an integer won't work. Try:

<a href="<?php echo bp_core_get_user_domain( bp_get_member_user_id() ); ?>" target="_blank"> class="">View Profile</a>

Solution 3:[3]

if you need to check response code during unit test you can use assertStatus in your test. for the example:

$response = $this->getJson($program_url);

$response->assertStatus(404);

Or use Http::get and check status like this. (you can use this anywhere in the code, not only in the test):

$response = Http::get($program_url);
if($response->status()==404) {
........
}

Or you can use php function: get_headers. You can also use this anywhere in your code and it's a bit faster than the previous one:

private function pageExists($url) {
    $headers=@get_headers($url);
    return $headers && $headers[0] != 'HTTP/1.1 404 Not Found';
}

you can use this method like this:

if($this->pageExists($program_url)) {
  .........
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Codeschreiber.de
Solution 2 shanebp
Solution 3