Skip to:
Content

BuddyPress.org

Opened 14 months ago

Last modified 5 months ago

#9276 assigned defect (bug)

error when using $bp->displayed_user->domain

Reported by: locker17's profile locker17 Owned by: espellcaste's profile espellcaste
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)

9276.patch (649 bytes) - added by vapvarun 5 months ago.

Download all attachments as: .zip

Change History (8)

This ticket was mentioned in Slack in #buddypress by espellcaste. View the logs.


14 months ago

#2 @espellcaste
14 months ago

  • Owner changed from imath to espellcaste
  • Status changed from new to assigned

#3 @espellcaste
5 months ago

  • Component changed from Documentation to Members
  • Milestone changed from Awaiting Review to 15.0.0

bp_displayed_user_url is the correct function. bp_displayed_user_domain will be deprecated in the next release.

#4 @vapvarun
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 !value patterns all work correctly
  • Page loads tested (homepage, members directory, user profiles)
  • Logged-in user navigation tested
  • Viewing another user's profile tested

@vapvarun
5 months ago

#5 @manhphucofficial
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.

#6 @espellcaste
5 months ago

In 14184:

Fix for a "call to undefined function xprofile_get_field_data()" error at BP_LoggedIn_User.

Let us avoid an error when trying to get a logged-in user fullname, which relies on the XProfile Extended component. If this component is disabled, its functions are not loaded.

See #9276

This ticket was mentioned in Slack in #buddypress by espellcaste. View the logs.


5 months ago

Note: See TracTickets for help on using tickets.