Skip to main content

Run your own code when a tracked link follows its Reference Library item — a URL edit or a deleted item

Premium Intermediate

Outlinks keeps each tracked link in step with the Reference Library item it tracks, and these hooks fire around those writes. $redirect is the tracked link as it was before the write, as the database returns it (string values: id, show_id, reference_id, slug, destination_url, …). before fires immediately before the write; after only once the link has actually been updated.

  • benecaster_outlinks_redirect_destination_sync_before and …_destination_sync_after ( array $redirect, string $destination ): the library item’s URL was edited. $destination is the new URL, cleaned as http or https, that every published /go/ link now sends visitors to; $redirect['destination_url'] is the old one. Nothing fires for an edit that leaves the URL as it was (a label or description change), for a new URL that is not http or https (emptied, mailto:, no scheme; the link keeps its last safe destination), for an item no tracked link follows, or when the link already points at the new URL.
  • benecaster_outlinks_redirect_detach_before and …_detach_after ( array $redirect ): the library item was deleted, which Benecaster also does once per item when a whole show is deleted. The link is kept, with its clicks and its destination, as a standalone link; $redirect['reference_id'] still holds the deleted item’s id. Nothing fires for an item no tracked link follows.

Code

<?php
add_action( 'benecaster_outlinks_redirect_destination_sync_after', function ( array $redirect, string $destination ): void {
    error_log( sprintf( '/go/%s now sends visitors to %s (was %s)', $redirect['slug'], $destination, $redirect['destination_url'] ) );
}, 10, 2 );

View on GitHub →

Hooks Used

Need this built rather than just documented? See our services →