Opened 14 months ago
Last modified 5 months ago
#9276 assigned defect (bug)
error when using $bp->displayed_user->domain
| Reported by: |
|
Owned by: |
|
|---|---|---|---|
| Milestone: | 15.0.0 | Priority: | normal |
| Severity: | normal | Version: | 14.3.2 |
| Component: | Members | Keywords: | |
| Cc: |
Description
Using $bp->loggedin_user->domain like described here:
<a href="https://codex.buddypress.org/developer/the-bp-global/">https://codex.buddypress.org/developer/the-bp-global/</a>
throws an error on my page:
Undefined property: stdClass::$domain
So I am using the function bp_displayed_user_domain() instead which works. I am running PHP 8.2 if that matters.
If $bp->displayed_user->domain is deprecated or whatever can someone please update the docs?
Thanks!
Reason: Tried to use $bp->displayed_user->domain when testing this script here from shanep to add a new tab:
https://gist.github.com/shanebp/5d3d2f298727a0a036e5
Attachments (1)
Change History (8)
This ticket was mentioned in Slack in #buddypress by espellcaste. View the logs.
14 months ago
#3
@
5 months ago
- Component changed from Documentation to Members
- Milestone changed from Awaiting Review to 15.0.0
#4
@
5 months ago
Attaching a patch that fixes the "Undefined property" error on PHP 8.2+ when accessing $bp->displayed_user->domain before a user profile is set.
Issue
$bp->displayed_user is a plain stdClass object. On PHP 8.2+, accessing undefined properties on stdClass triggers warnings/errors. Unlike $bp->loggedin_user which now uses the BP_LoggedIn_User class with magic methods (since 15.0.0), displayed_user remains a plain stdClass.
This Patch (Simple Approach)
Pre-initialize commonly accessed properties with sensible default values in class-buddypress.php:
- id = 0
- domain =
- userdata = null
- fullname =
- front_template = null
This prevents the undefined property errors while maintaining backward compatibility. Default values are falsy so existing checks like if ( ! $bp->displayed_user->domain ) still work correctly.
Alternative Approach
For consistency with the 15.0.0 pattern, a BP_Displayed_User class could be created similar to BP_LoggedIn_User with magic __get() and __isset() methods. This would be more comprehensive but requires a new file.
Let me know if you prefer the class-based approach and I can prepare that patch instead.
Testing
- Verified default values are correct before user is set
- Verified values are correctly overwritten when viewing a profile
- Verified
isset(),empty(), and!valuepatterns all work correctly - Page loads tested (homepage, members directory, user profiles)
- Logged-in user navigation tested
- Viewing another user's profile tested
#5
@
5 months ago
Thanks for the patch!
I tested on PHP 8.2 with WP_DEBUG enabled and couldn’t reproduce any undefined
property warnings in my setup (Members directory and member profile pages load
without notices, and debug.log remains empty).
That said, initializing these common $bp->displayed_user properties is a safe,
defensive change for PHP 8.2+ and shouldn’t affect existing behavior. Looks good to me.
bp_displayed_user_urlis the correct function.bp_displayed_user_domainwill be deprecated in the next release.