Skip to:
Content
Pages
Categories
Search
Top
Bottom

Global $bp does not contain current_user data


  • Xtremefaith
    Participant

    @xtremefaith

    If I wanted to create a dashboard for my users how would I go about this? I started with creating a page called “Dashboard” and redirecting logged in users to that page as opposed to their profile. I then setup a page-dashboard.php template in which I copied page.php and began to modify its contents.

    The problem is none of the buddypress functions that render user details work because the global $bp is missing all relevant current_user information. If a method accepts a user_id or `item_id then I pass the following variable:

    $user_id = get_current_user_id();

    But short of that this feels extremely limited and I have to believe there is a better way. Any thoughts or suggestions that could help me getting rolling would be greatly appreciated.

Viewing 5 replies - 1 through 5 (of 5 total)

  • Xtremefaith
    Participant

    @xtremefaith

    I believe I found a way to populate the displayed_user data in the $bp object, but the second I do I am redirected to the user’s profile page. Here is my hook:

    add_filter('bp_displayed_user_id', array( $this, 'bp_displayed_outside_bp') );
    function bp_displayed_outside_bp($id){
    	return ($id==0) ? get_current_user_id() : $id;
    }

    As expected, my dashboard page comes back with $id==0 and then the current user id is used to populate the displayed user data, but instead of then rending my dashboard page that I was on, I’m redirected to the profile page.

    I’ve also removed all redirect hooks that I had to make sure none of them are interfering with this. I’m literally stuck right now hoping for some help/direction. So please let me know if you have any thoughts.


    Henry Wright
    Moderator

    @henrywright

    Is your bp_displayed_outside_bp() function a class method? If not, then $this is being used out of context. Instead try:

    add_filter( 'bp_displayed_user_id', 'bp_displayed_outside_bp' );


    Henry Wright
    Moderator

    @henrywright

    I’m also wondering why you want to populate the $bp global with current user data?


    Xtremefaith
    Participant

    @xtremefaith

    @henrywright because I’m using OOP my filter is within a class, so the syntax I have is correct. I’ve created a dashboard page and I want the person viewing that page to see their details throughout it. I’m still pretty new to buddypress so not sure how this is done. But I imagine on any page, or perhaps a widget someone could want to display current user data. So perhaps some documentation or a clear explanation of that would do the trick for me. Thanks!


    r-a-y
    Moderator

    @r-a-y

    It’s because bp_displayed_user_id() is only applicable on member profile pages (eg. example.com/members/USERNAME).

    You’re probably looking to use bp_loggedin_user_id(). Or if you just want to rely on WordPress functions, just use get_current_user_id() / get_user_by().

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Global $bp does not contain current_user data’ is closed to new replies.
Skip to toolbar