Skip to main content

Drop a user’s badge chips into a custom theme template

Premium Beginner

benecaster_render_user_badges() returns a subscriber’s badge chips as ready-made HTML, so a theme can show them anywhere — a comment byline, an author box, a members directory — without knowing how badges are stored or styled.

It goes through the same lookup as everywhere else, so any add-on that adds or reorders badges is respected, and the chips match the ones on the account page. The output is already escaped — escape it again on the way out and your visitors will see the markup as text instead of badges.

If you already have a subscriber’s badges to hand and only need them rendered, there is a narrower renderer that turns badges into chips without repeating the lookup. That is the one to use in a loop over many subscribers, where doing the lookup per row is how a members directory becomes slow.

Code

<?php
// Theme template (e.g. author.php) — display the post author's badges below their name.
$badges_html = benecaster_render_user_badges( (int) get_the_author_meta( 'ID' ) );
if ( '' !== $badges_html ) {
    // Output is already escaped by BadgeChipRenderer.
    echo $badges_html;
}

View on GitHub →

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