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.
Opens WPIDE on
Code
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' ) ? ' – ' . 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 add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.