# 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.

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

## Code

```php
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 use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Copyable Referral Link for Any Page (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/
