从头像中获取BuddyPress活动ID

时间:2016-03-27 03:22:36

标签: wordpress userid avatar

当用户上传新头像时,头像会张贴在活动墙中。 如何使用userId?

获取此活动ID

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

1 个答案:

答案 0 :(得分:6)

您可以编写查询来获取该活动。还有一个可以挂钩的过滤器将在上传头像后调用(稍后解释):

<?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
)

当您执行此活动时,会调用一个可以挂钩的动作。它是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]);

获取头像的完整网址。