在不同的上下文中使用用户的 ID

在不同的上下文中使用用户的 ID

Codex Home → Developer Resources → Playing with the user』s ID in different contexts
Playing with the user』s ID in different contexts

BuddyPress Core is first taking care of the members. As a result, when you write a BuddyPress plugin, there is a very good chance that you』ve got to play with the finest element identifying the users : their ID.
There are two scenarios: the logged in user and the user whose profile is displayed. Thankfully, BuddyPress provides you with two interesting functions to get the user ID for these scenarios. Then there are other specific cases in which you must handle these two main functions with caution. Fortunately again, BuddyPress provides other functions to avoid some 「unwanted」 behaviors.
Steps

Explore the two main functions to get the user』s ID
Discover other useful functions when playing inside some BuddyPress loops
Additionnal resources

Explore the two main functions to get the user』s ID
The function bp_loggedin_user_id() will return you the current logged in user ID that has been set thanks to the BP_Core class using the WordPress function wp_get_current_user().
The function bp_displayed_user_id() will return the ID of the displayed user that has been set thanks to the bp_core_set_uri_globals() function when a profile page is requested. This also means that you can simply know if you are on a profile page by checking if the bp_displayed_user_id() function is not empty. The function bp_is_user(), informing you that you』ve reached a profile page is actually using this check.
When the two are equal, it means that the logged in user is visiting one of his profile pages, the BuddyPress function bp_is_my_profile() is using this equality to inform you if that』s the case.
Let』s begin an example to illustrate the use of the two functions. In the member』s header there』s a template tag BuddyPress is using to display the latest activity the displayed user posted : bp_activity_latest_update(). Like most of BuddyPress template tags, you can filter the returned value to eventually extend it in your plugin. Here, you will simply print user IDs. In the functions.php of your active theme (or in your plugin), you can write :
function bp_plugin_filter_latest_update( $update = '' ) {

$update .= ' logged_in: ' . bp_loggedin_user_id() .' | displayed: ' . bp_displayed_user_id() . '';

return $update;
}

add_filter( 'bp_get_activity_latest_update', 'bp_plugin_filter_latest_update', 10, 1 );

Here』s a screen capture of the result of this 「great extension」
bp_loggedin_user_id() Vs bp_displayed_user_id()
 
As you can see, if your goal is to display information about the user displayed, as shown in this case, you should definitively use bp_displayed_user_id() to avoid having the information of the logged in user on each profile ;). You can also see that 「imath」 is the logged in user as the two functions return the same result. In a user』s profile the bp_loggedin_user_id() will be useful to add some interaction with the user displayed. For instance, the 「Add Friend」 functionality is using the logged in user ID to check if the current user is friend with the displayed user.
Discover other great functions when extending some BuddyPress loops
To go straight to the point, let』s edit our previous code by adding a filter to the bp_member_latest_update() template tag. This function is doing the same thing than the one you saw in first step, except that it runs inside the members loop.
function bp_plugin_filter_latest_update( $update = '' ) {
// In step 1, you saw that bp_displayed_user_id() was the best function to use
// when displaying information about the user in his profile page
$update .= ' displayed: ' . bp_displayed_user_id() . '';

return $update;
}

add_filter( 'bp_get_activity_latest_update', 'bp_plugin_filter_latest_update', 10, 1 );
add_filter( 'bp_get_member_latest_update', 'bp_plugin_filter_latest_update', 10, 1 );

Take a few seconds to check the result of this code in the three parts of this screen capture.
bp_displayed_user_id() and the members loop
 
As you can see, while bp_displayed_user_id() works fine in the member』s header, it is empty in the members loop. So we need to call two functions to the rescue! The first one is a template tag specific to the member component: bp_get_member_user_id() and the second one is the function, you discovered earlier, to check you are on a user』s profile page: bp_is_user(). So, you need to edit the filter this way:
function bp_plugin_filter_latest_update( $update = '' ) {
if( bp_is_user() ) {
$user_id = 'displayed: '. bp_displayed_user_id();
} else {
$user_id = 'get_member_user: '. bp_get_member_user_id();
}
$update .= ' ' . $user_id . '';

return $update;
}

add_filter( 'bp_get_activity_latest_update', 'bp_plugin_filter_latest_update', 10, 1 );
add_filter( 'bp_get_member_latest_update', 'bp_plugin_filter_latest_update', 10, 1 );

If you check the result of this code, you will surely wonder why i』ve changed the color of the ID to red as it seems to solve the issue in the members loop and in the member』s header. To understand why, you simply need to activate the friends navigation of the user』s profile, and here』s what you』ll see:
Members loop in member』s profile : friends
 
「mathieu」, 「imath」 and 「Moi」 have the same ID! Actually, only 「mathieu」 has the good ID as you are visiting his profile. In the friends tab, although you are on a user』s profile, you also are in a members loop displaying user』s friends. So you need to adapt your code to this particular case by also checking the bp_get_member_user_id() before using bp_displayed_user_id().
function bp_plugin_filter_latest_update( $update = '' ) {
if( bp_is_user() && ! bp_get_member_user_id() ) {
$user_id = 'displayed: '. bp_displayed_user_id();
} else {
$user_id = 'get_member_user: '. bp_get_member_user_id();
}
$update .= ' ' . $user_id . '';

return $update;
}

add_filter( 'bp_get_activity_latest_update', 'bp_plugin_filter_latest_update', 10, 1 );
add_filter( 'bp_get_member_latest_update', 'bp_plugin_filter_latest_update', 10, 1 );

Now you can check again: the logged in user』s profile, another profile, the friends tab of a user』s profile and the main members loop, each user has the good ID in the different areas! Bravo
The Activity and the Group members loop also offer their specific functions to get the good user ID in their context :

Activity loop : bp_get_activity_user_id()
Group members loop : bp_get_group_member_id()

Additional resources

wp_get_current_user() in WordPress Codex
the Members loop
the Group Members loop
the Activity loop

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注