Skip to content

Instantly share code, notes, and snippets.

@alexstandiford
Created January 3, 2020 17:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexstandiford/ee34d36205909cd350521757164ab2ff to your computer and use it in GitHub Desktop.
Save alexstandiford/ee34d36205909cd350521757164ab2ff to your computer and use it in GitHub Desktop.
Redirects any URL that refers an inactive affiliate to the 404 page.
<?php
/*
* Any URL that refers an inactive affiliate will be redirected to the 404 page.
*
* @since 1.0.0
*/
add_action( 'template_redirect', function() {
global $wp_query;
// Return early if not using AffiliateWP.
if ( ! function_exists( 'affiliate_wp' ) ) {
return;
}
// Get the referral variable being used in AffiliateWP.
$referral_var = affiliate_wp()->tracking->get_referral_var();
// Check if the referral var is set in the request.
if ( isset( $_GET[ $referral_var ] ) ) {
$affiliate_id = (int) $_GET[ $referral_var ];
// If the affiliate is not active, redirect to the 404 page.
if ( true !== affwp_is_active_affiliate( $affiliate_id ) ) {
$wp_query->set_404();
status_header( 404 );
get_template_part( 404 );
exit();
}
}
} );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment