This code snippet demonstrates how to redirect all affiliate traffic to a custom WordPress page. The referral variable and ID/username are maintained during the redirect so the affiliate can still earn commission for the referral.

In the example below, all affiliate traffic is instantly redirected to a WordPress page with the slug of special-promotion. Change this variable to match your page slug.

<?php
/**
 * Redirect all affiliate traffic to a custom WordPress page.
 *
 * @link https://andrew.dev/snippets/redirect-all-affiliatewp-traffic-to-custom-wordpress-page/
 */
function am_affwp_redirect_all_affiliate_traffic() {

	// Bail if AffiliateWP is not active.
	if ( ! function_exists( 'affiliate_wp' ) ) {
		return;
	}

	// The slug of the WordPress page where all affiliate traffic should redirect to.
	$page_slug = 'special-promotion';

	// Get the referral variable from AffiliateWP.
	$referral_var = affiliate_wp()->tracking->get_referral_var();

	// Redirect the user to the WordPress page specified, but only if not
	// already on the page set via $page_slug.
	if ( isset( $_GET[$referral_var] ) && ! is_page( $page_slug ) ) {

		// Append the referral variable and value to the URL after the redirect.
		$page_slug = add_query_arg( $referral_var, $_GET[$referral_var], $page_slug );

		// Redirect the user to the new page.
		wp_safe_redirect( $page_slug );

	}

}
add_action( 'template_redirect', 'am_affwp_redirect_all_affiliate_traffic' );

Why this snippet may be useful

If you are having a special promotion or sale, you can quickly redirect all affiliate traffic to a custom page on your website. This is hugely beneficial when you don’t have the time to reach out to your affiliates and let them know about the promotion.

It would be a good idea to remove this snippet after your sale has ended so all affiliate traffic returns to normal.

Leave a Reply

Your email address will not be published. Required fields are marked *