Skip to main content

Run your own code before and after each Link Health action — dismiss, restore, fix a URL, unlink, track

Premium Intermediate

benecaster_outlinks_health_item_action_before and benecaster_outlinks_health_item_action_after fire once per item, whether the action came from the Link Health page, a bulk selection or the actions route. A bulk request fires them once for each item it writes.

  • before fires once every check on the item has passed, immediately before its write. An item that is refused (not found, the wrong kind for the action, no edit_post on its post, the link no longer in the post, a show where Outlinks is off, a name already taken, an unusable URL) fires neither hook. A malformed request (400) fires nothing.
  • after fires only when the item succeeded. $result is that item’s row from the route’s answer: ok is true, update_url adds url, and create_outlink adds redirect: { id, slug, go_url, destination_url }. A write that fails after before (WordPress could not save the post, or the new link could not be created) fires before with no after.
  • For dismiss and restore the items are written together, so every item’s before fires ahead of the one write and every after follows it.

$item is [ 'type' => 'site_link'|'redirect', 'id' => int ]. $args depends on the action:

$action $args
dismiss, restore []
update_url old_url, url (the cleaned new address), post_id
unlink url, post_id
create_outlink url (the link as it stands in the episode), post_id, show_id (the episode’s show), slug (the cleaned link name)

Code

<?php
add_action( 'benecaster_outlinks_health_item_action_after', function ( string $action, array $item, array $args, array $result ): void {
    if ( 'update_url' === $action ) {
        error_log( sprintf( 'Link Health: post %d now links to %s instead of %s', $args['post_id'], $args['url'], $args['old_url'] ) );
    }
}, 10, 4 );

View on GitHub →

Hooks Used

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