Halloween Sale 15% off Ends in See the plans
PHP

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.

Don’t have WPIDE yet? Install it on your site

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

  1. Install WPIDE on your WordPress site (it is free) and open Code Snippets.
  2. Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
  3. It is added inactive: review the code and settings, then switch it on.

Have a snippet worth sharing?

Publish it to the library from WPIDE or right here. Sign in with any WordPress site that runs WPIDE, free or Pro. Public snippets are reviewed by the WPIDE team before they appear.

Which site should get this snippet?

Enter the address of your WordPress site. WPIDE opens there in a new tab with the snippet ready to review and save.

Remembered in this browser only, so you are asked once.

Report this snippet

Tell the WPIDE team what is wrong: broken code, something harmful, spam or copied work.