Created
January 3, 2020 17:54
-
-
Save alexstandiford/ee34d36205909cd350521757164ab2ff to your computer and use it in GitHub Desktop.
Redirects any URL that refers an inactive affiliate to the 404 page.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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