从头像获取 BuddyPress activity id

Get the BuddyPress activity id from the avatar

当用户上传新头像时,头像会发布在activity 墙上。 如何使用 userId 获取此 activity ID?

我认为唯一的方法是创建自己的查询,对吗?

您可以编写查询来获取 activity。还有一个你可以挂钩的过滤器,它会在头像上传后被调用(稍后解释):

<?php

global $wpdb;

$query = "SELECT * FROM {$wpdb->prefix}bp_activity WHERE " .
         "`type` = 'new_avatar' AND `user_id` = %d " .
         "ORDER BY `date_recorded` DESC LIMIT 1";

$result = 
$wpdb->get_row(
    $wpdb->prepare($query, $user_id)
);

if ($result) {
    // found an activity item for avatar upload
    var_dump($result);
} else {
    // user has not uploaded an avatar
}

结果如下:

stdClass Object
(
    [id] => 2   <-- this is the activity ID
    [user_id] => 1
    [component] => profile
    [type] => new_avatar
    [action] => admin changed their profile picture
    [content] => 
    [primary_link] => http://example.com/wordpress/members/admin/
    [item_id] => 0
    [secondary_item_id] => 0
    [date_recorded] => 2016-03-29 04:41:53
    [hide_sitewide] => 0
    [mptt_left] => 0
    [mptt_right] => 0
    [is_spam] => 0
)

有一个被调用的操作,您可以挂钩该操作,当 activity 发生时将调用该操作。它是 xprofile_avatar_uploaded 并传递两个参数,$item_id(用户 ID)和 $type(例如裁剪或相机)。此过滤器在头像上传后执行。

在函数的某处添加:

add_action('xprofile_avatar_uploaded', 'callback');

function callback($user_id, $type)
{
    // $user_id uploaded new avatar
}

我发现你也可以打电话:

$img = bp_get_activity_avatar(['user_id' => $user_id]);

获取HTML显示头像。它们存储在 wp-content/uploads/avatars.

您也可以拨打:

$url = bp_core_fetch_avatar(['item_id' => $user_id, 'html' => false]);

只获取头像的完整 URL。