Affiliate Earnings Summary Shortcode (AffiliateWP)
Adds a [wpide_affiliate_stats] shortcode that shows the logged-in affiliate their unpaid and paid earnings, referral count, visits and personal referral link on any page.
Opens WPIDE on
Code
add_shortcode( 'wpide_affiliate_stats', function () {
if ( ! function_exists( 'affwp_get_affiliate_id' ) || ! is_user_logged_in() ) {
return '';
}
$affiliate_id = affwp_get_affiliate_id( get_current_user_id() );
if ( ! $affiliate_id || ! affwp_is_active_affiliate( $affiliate_id ) ) {
return '';
}
$rows = array(
__( 'Unpaid earnings' ) => affwp_get_affiliate_unpaid_earnings( $affiliate_id, true ),
__( 'Paid earnings' ) => affwp_get_affiliate_earnings( $affiliate_id, true ),
__( 'Referrals' ) => number_format_i18n( (int) affwp_get_affiliate_referral_count( $affiliate_id ) ),
__( 'Visits' ) => number_format_i18n( (int) affwp_get_affiliate_visit_count( $affiliate_id ) ),
);
$html = '<dl class="wpide-affwp-stats" style="display:grid;grid-template-columns:repeat(auto-fit,minmax(140px,1fr));gap:12px">';
foreach ( $rows as $label => $value ) {
$html .= '<div style="padding:12px;border:1px solid rgba(0,0,0,.1);border-radius:6px"><dt style="font-size:.85em;opacity:.75">' . esc_html( $label ) . '</dt><dd style="margin:0;font-size:1.4em;font-weight:600">' . wp_kses_post( $value ) . '</dd></div>';
}
$html .= '</dl>';
$url = affwp_get_affiliate_referral_url( array( 'affiliate_id' => $affiliate_id ) );
$html .= '<p><label>' . esc_html__( 'Your referral link' ) . '<br><input type="text" readonly value="' . esc_attr( $url ) . '" onclick="this.select()" style="width:100%"></label></p>';
return $html;
} );
Notes
Visitors who are not active affiliates see nothing; place it on a members page or next to the Affiliate Area.
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.