# Top Affiliates Leaderboard (AffiliateWP)

> Adds a [wpide_affiliate_leaderboard] shortcode that ranks your active affiliates by earnings, showing first names only for privacy, to spark some friendly competition.

- Type: PHP
- Runs: Run everywhere
- Category: [Plugin Integrations](https://wpide.com/library/category/integrations/)
- Tags: affiliatewp, leaderboard, shortcode
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/affwp-top-affiliates-leaderboard/

## Code

```php
add_shortcode( 'wpide_affiliate_leaderboard', function ( $atts ) {
    if ( ! function_exists( 'affiliate_wp' ) ) {
        return '';
    }

    $atts   = shortcode_atts( array( 'number' => 10, 'amounts' => '0' ), $atts, 'wpide_affiliate_leaderboard' );
    $number = min( 50, max( 1, absint( $atts['number'] ) ) );
    $key    = 'wpide_affwp_board_' . $number;
    $rows   = get_transient( $key );

    if ( false === $rows ) {
        $rows       = array();
        $affiliates = affiliate_wp()->affiliates->get_affiliates( array( 'number' => $number, 'status' => 'active', 'orderby' => 'earnings', 'order' => 'DESC' ) );
        foreach ( (array) $affiliates as $affiliate ) {
            $user   = get_userdata( $affiliate->user_id );
            $rows[] = array(
                'name'     => $user ? ( $user->first_name ? $user->first_name : $user->display_name ) : '—',
                'earnings' => (float) $affiliate->earnings,
            );
        }
        set_transient( $key, $rows, 6 * HOUR_IN_SECONDS );
    }

    if ( ! $rows ) {
        return '';
    }

    $html = '<ol class="wpide-affwp-leaderboard">';
    foreach ( $rows as $row ) {
        $html .= '<li>' . esc_html( $row['name'] ) . ( $atts['amounts'] && function_exists( 'affwp_currency_filter' ) ? ' &ndash; ' . esc_html( affwp_currency_filter( affwp_format_amount( $row['earnings'] ) ) ) : '' ) . '</li>';
    }

    return $html . '</ol>';
} );
```

## Notes

Use number="10" to set how many affiliates to show. Cached for six hours. Earnings amounts are hidden unless you add amounts="1".

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Top Affiliates Leaderboard (AffiliateWP)" in its snippet library and add it in one click; or create a new PHP snippet in WPIDE (or any code snippets plugin) and paste the code above. More snippets: https://wpide.com/library/
