Copyable Referral Link for Any Page (AffiliateWP)
Adds a [wpide_affiliate_link] shortcode that shows logged-in affiliates their referral link for the current page (or a page you choose) with a copy button.
Opens WPIDE on
Code
add_shortcode( 'wpide_affiliate_link', function ( $atts ) {
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 '';
}
$atts = shortcode_atts( array( 'url' => '' ), $atts, 'wpide_affiliate_link' );
$base = $atts['url'] ? esc_url_raw( $atts['url'] ) : get_permalink();
$link = affwp_get_affiliate_referral_url( array( 'affiliate_id' => $affiliate_id, 'base_url' => $base ) );
$id = wp_unique_id( 'wpide-afflink-' );
return sprintf(
'<div class="wpide-affiliate-link"><label for="%1$s">%2$s</label><div style="display:flex;gap:6px"><input id="%1$s" type="text" readonly value="%3$s" style="flex:1;min-width:0"><button type="button" onclick="var i=document.getElementById(\'%1$s\');i.select();if(navigator.clipboard){navigator.clipboard.writeText(i.value);}this.textContent=\'%5$s\';">%4$s</button></div></div>',
esc_attr( $id ),
esc_html__( 'Your referral link for this page' ),
esc_attr( $link ),
esc_html__( 'Copy' ),
esc_js( __( 'Copied' ) )
);
} );
Notes
Use [wpide_affiliate_link url="https://example.com/product/"] to build a link to another page. Non-affiliates see nothing.
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.