benecaster_get_user_badges()
Returns the merged tier_auto + manual badge rows for a subscriber, ordered by granted_at ASC, id ASC. The result passes through the benecaster_user_badges filter before being returned, allowing add-ons to inject, remove, or reorder badges.
Access: Free
Since: 1.0.0
Signature
benecaster_get_user_badges( int $user_id = 0, ?int $show_id = null ): array
Parameters
| Parameter | Type | Description |
|---|---|---|
$user_id |
int |
WordPress user ID. Defaults to 0, which resolves to the current logged-in user. |
$show_id |
?int |
Optional show context. Passed through to the benecaster_user_badges filter only — badge storage is not show-scoped. |
Return Value
Array of badge rows from SubscriberBadgeRepository::find_for_user(), each entry containing at minimum:
| Key | Type | Description |
|---|---|---|
label |
string |
Display label |
icon_slug |
string |
Icon identifier |
icon_attachment_id |
int|null |
Attachment ID for custom icon, or null |
color |
string |
Hex color string |
source |
string |
'tier_auto' or 'manual' |
Returns an empty array (no DB query) when no user is resolved.
Usage
$badges = benecaster_get_user_badges( get_current_user_id() );
foreach ( $badges as $badge ) {
echo esc_html( $badge['label'] );
}
// Show-scoped call — $show_id is passed to the filter but does not change which badges are fetched
$badges = benecaster_get_user_badges( $user_id, $show_id );
Notes
$user_id = 0defaults to the current logged-in user. Pass an explicit ID when calling outside a logged-in request.- Core badge storage is not show-scoped — the same badges are returned regardless of
$show_id. The$show_idargument is available tobenecaster_user_badgesfilter callbacks so per-show surfaces (forums, community integrations) can scope display without changing the schema. - To inject, remove, or reorder badges programmatically, use the
benecaster_user_badgesfilter rather than post-processing the return value.
Related
benecaster_user_badges— filter to modify the badge list before it is returned- Tier Badges — how badges are assigned automatically
BadgeChipRenderer— renders badge chips in admin and account pages