Skip to:
Content
Pages
Categories
Search
Top
Bottom

How to get userid regardless of context

  • I want to add some filters to customize the display of names in my buddypress install using a function I’m creating called my_displayname.

    add_filter('bp_core_get_user_displayname','my_displayname');

    add_filter('bp_member_name','my_displayname');

    However, I can’t seem to figure out how to get the current user id to work with inside my function. Loggedin_user and displayed_user don’t seem to do the trick..

Viewing 11 replies - 1 through 11 (of 11 total)
  • make sure you have the global $bp in the function and

    $bp->displayed_user->id

    or

    $bp->loggedin_user->id


    mrjarbenne
    Participant

    @mrjarbenne

    Can you post this function after you get it worked out for the code-inept members of the community. I’m trying to do the same thing, in combination with this function.php line Andy posted in another thread.

    <?php
    function disable_name_change( $data ) {
    if ( 1 == $data->field_id )
    $data->field_id = false;

    return $data;
    }
    add_action( 'xprofile_data_before_save', 'disable_name_change' );
    ?>

    Hi etiviti,

    $bp->displayed_user->id works if I’m calling the function on a profile page, however if I load the buddypress homepage or activity stream for example, displayed_user no longer works because I’m not looking at a user profile. I want a way to be able to know the id of the user in question regardless of what page I’m on, so that their name can be manipulated by my function on every page. Any ideas?

    Hi etiviti,

    $bp->displayed_user->id works if I’m calling the function on a profile page, however if I load the buddypress homepage or activity stream for example, displayed_user no longer works because I’m not looking at a user profile. I want a way to be able to know the id of the user in question regardless of what page I’m on, so that their name can be manipulated by my function on every page. Any ideas?


    r-a-y
    Keymaster

    @r-a-y

    $bp->loggedin_user->id

    Read this for more about the global $bp variable:

    https://codex.buddypress.org/developer-docs/the-bp-global/


    r-a-y
    Keymaster

    @r-a-y

    $bp->loggedin_user->id

    Read this for more about the global $bp variable:

    https://codex.buddypress.org/developer-docs/the-bp-global/

    Hi Ray,

    That will return the info for the logged in user only. Let me give a clear example:

    I am looking at the “members” page that lists 10 members. I want to write a function that will manipulate the names displayed on the members page for those 10 members.

    Neither loggedin_user or displayed_user is what I want.. does that make sense?

    Hi Ray,

    That will return the info for the logged in user only. Let me give a clear example:

    I am looking at the “members” page that lists 10 members. I want to write a function that will manipulate the names displayed on the members page for those 10 members.

    Neither loggedin_user or displayed_user is what I want.. does that make sense?

    on the members page is a loop then inside of that you can call: bp_get_member_user_id() which will return the user_id for the current record

    fwiw – the call will be different depending on what page, loop, etc you are in.

    on the members page is a loop then inside of that you can call: bp_get_member_user_id() which will return the user_id for the current record

    fwiw – the call will be different depending on what page, loop, etc you are in.

    Thanks for your help everyone!

    OK, I think I have it working, however its not super desirable. I had to add 4 filters (and corresponding custom functions) in bp-custom.php and I also had to modify bp_core_get_user_displayname in bp-core.php. Adding a filter for it doesn’t do the trick because the passed in $user_id_or_username gets modified by the function the first time it is run in the core, and when using the filter $user_id_or_username actually contains the displayname, not the id or username.

    so I now have the following entries in bp-custom.php:

    // also had to modify bp_core_get_user_displayname in bp-core.php

    add_filter('bp_get_member_name','my_bp_get_member_name');

    add_filter('bp_get_group_member_link','my_bp_get_group_member_link');

    add_filter('bp_get_the_topic_last_poster_name','my_bp_get_the_topic_last_poster_name');

    add_filter('bp_get_the_topic_post_poster_name','my_bp_get_the_topic_post_poster_name');

    It would be great if:

    1. bp_core_get_user_displayname was used by all other functions that need a display name (currently I have to re-define the 4 additional functions above).

    2. bp_core_get_user_displayname could be changed in the filter and not the core (as explained above).

    Thoughts?

Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘How to get userid regardless of context’ is closed to new replies.
Skip to toolbar