benecaster_outlinks_before_redirect
Fires when a visitor follows a tracked /go/ link, immediately before they are redirected and after benecaster_outlinks_click_logged. Use it to inspect or log where the visitor is about to go.
It is a notification, not a filter: it cannot change the destination. $redirect_data['target_url'] is the final address, after the link’s expiry, its country rule, its affiliate URL and any URL parameters the visitor brought with them.
It fires even when the click could not be recorded, because the visitor is still redirected. It does not fire when the link’s stored address is not a safe http or https URL, because that visitor gets a 404 instead. Like the click hook, it fires only for a link whose show has Outlinks switched on and while the licence is current.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
$slug |
string |
— | The link's name, the part after `/go/` |
$episode_id |
int|null |
— | The episode the click came from, once Outlinks has checked it belongs to the link's show; null when there is none |
$country_code |
string|null |
— | The visitor's country as an ISO 3166-1 alpha-2 code from Benecaster's GeoIP lookup; null when GeoIP is off or had no answer |
$redirect_data |
array |
— | The link's stored settings (`id`, `slug`, `show_id`, `reference_id`, `destination_url`, `affiliate_url`, `expiry_date`, `expiry_fallback_url` and the rest, as the database returns them), plus `target_url`, the address the visitor is sent to |
Examples
Log clicks that leave through an affiliate URL
add_action( 'benecaster_outlinks_before_redirect', function ( string $slug, ?int $episode_id, ?string $country_code, array $redirect_data ): void {
if ( str_contains( $redirect_data['target_url'], 'tag=' ) ) {
my_affiliate_log( $slug, $country_code, $redirect_data['target_url'] );
}
}, 10, 4 );
Notes
Need this built rather than just documented? See our services →