Skip to main content

benecaster_get_user_badges()

Membership Free
benecaster_get_user_badges( int $user_id = 0, ?int $show_id = null ): array

Returns the badges a subscriber has earned, both the ones their tier grants automatically and any an admin gave them by hand, merged and ordered oldest first.

Mind the name. This function is benecaster_get_user_badges(); benecaster_user_badges without the get_ is the filter it passes through. They are one letter apart in conversation and entirely different things in code.

To add, remove or reorder badges, hook that filter rather than post-processing what this returns — then every surface that displays badges agrees, including the ones you did not write.

Parameters

Name Type Default Required Description
$user_id int 0 No WordPress user ID. Defaults to the current logged-in user. Pass an explicit ID when calling outside a logged-in request, where there is no current user to resolve.
$show_id ?int null No Optional show context. It does not change which badges come back — badges are not stored per show — but it is handed to the filter, so a per-show surface can scope what it displays.

Return Value

Type: array

A subscriber's badges, oldest grant first. Each entry carries label, icon_slug, icon_attachment_id, color, and source — where source is tier_auto for a badge earned by tier and manual for one an admin granted. An empty array when there is no user to resolve.

Example

foreach ( benecaster_get_user_badges( get_current_user_id() ) as $badge ) {
    printf(
        '<span class="badge" style="color:%s">%s</span>',
        esc_attr( $badge['color'] ),
        esc_html( $badge['label'] )
    );
}

Hooks Fired

Need this built rather than just documented? See our services →