Buddypress: Limit amount of pictures shown for specific UserIds

21 Views Asked by At

We have a Buddypress Membership Page. I want to limit the number of pictures shown in the user profile for a specific list of users. The list of user-IDs is derived somewhere else.

I can't figure our a relevant HOOK which is unique enough to only affect the pictures from within the Userprofile. I found a starting point, which is the ajax-action media-filter.

This triggers the function bp_nouveau_ajax_object_template_loader which is VERY generic. It seems the User-IDs for which the media is fetched, is derived from the Referer - because when I disable the HTTP Referer in my browser - I dont get any images at all in this AJAX request. I found that this template is being loaded: bp-nouveau/buddypress/media/media-loop.php and within it, the media is loaded into the global media template using this:

if ( bp_has_media( bp_ajax_querystring( 'media' ) ) ) :

In bp_has_media there is a hook at the very end:

return apply_filters( 'bp_has_media', $media_template->has_media(), $media_template, $r );

I thought to hook into it and modify the amount of media:

add_filter('bp_has_media', function($media_template_has_media, $media_template, $r )
{
    if(!wp_doing_ajax())
        return $media_template_has_media;

    if($r['scope'] == 'personal' && !$r['album_id'] && !$r['group_id'] && is_numeric($r['user_id']) && some_condition_on_userid($r['user_id']))
    {
        // Limit to 2:
        if($media_template->total_media_count > 2)
        {
            $media_template->total_media_count = 2;
            $media_template->medias = array_slice($media_template->medias, 0, 2);
        }
    }
    
    return $media_template_has_media; 
}, 1, 3);

Why does this not work? I really dont get it..

0

There are 0 best solutions below