Skip to main content

Run your own code when a Link Health check finishes, with everything it found broken

Premium Intermediate

benecaster_outlinks_health_check_completed fires once per finished Link Health check, after old check history is pruned and after the Link Health notice and the email digest have been handled. A check that runs out of time carries on in a later request; the action fires only at the end of the request that finishes it. $finished_at is a Unix time.

$broken_items is everything Link Health counts as broken now, the same list the notice counts and the Outlinks tab’s summary line reads: [] when nothing is. Tracked links come first, then site links, each in id order. Every item has key (redirect:{id} or site_link:{id}, stable from run to run) and kind:

  • a tracked link (redirect; broken, not dismissed) also has id, slug and destination_url;
  • a site link (site_link; last check broken, not dismissed, not on an excluded domain) also has id, url, item_type (link or image), source_type (episode, page or post), source_id (the post) and http_status (null for no response).

Code

<?php
add_action( 'benecaster_outlinks_health_check_completed', function ( array $broken_items, int $finished_at ): void {
    $redirects = array_filter( $broken_items, fn( array $item ): bool => 'redirect' === $item['kind'] );
    wp_remote_post( 'https://status.example.com/hooks/link-health', [
        'body' => wp_json_encode( [ 'at' => gmdate( 'c', $finished_at ), 'broken' => count( $broken_items ), 'tracked_links' => array_column( $redirects, 'slug' ) ] ),
    ] );
}, 10, 2 );

View on GitHub →

Hooks Used

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