Skip to main content

Keep your own rows in step with the reference library

Free Intermediate

For an add-on that stores its own data against a show’s references: a redirect per link, click counts, a cached link check. A library item’s ID stays the same however many episodes cite it, which makes it the right thing to key your data to. These two actions tell you when that item changes or disappears.

The per-episode hook does not cover this. benecaster_episode_references_updated fires when one episode’s list of references changes. Editing the library item’s own URL changes what every citing episode shows and fires nothing per episode, so an integration listening only to that hook keeps an out-of-date destination with no error.

  • The delete action fires after the item is gone. Reading it inside your listener
    returns nothing, and there is no hook before the delete. Keep the label or URL from the updated action if you need them then.
  • Don’t do per-episode work on delete. One delete can remove the reference from any
    number of episodes, and none of them is named. Drop everything you hold for the item.
  • The updated action only fires for real edits, so a listener doesn’t need to guard
    against the unchanged re-save that happens on every episode save. It still receives label and description edits, so check whether the field you care about changed.

Code

<?php
add_action(
    'benecaster_reference_library_item_updated',
    function ( int $item_id, int $show_id, array $old, array $new ): void {
        // Only the destination matters to a redirect; a label edit does not.
        if ( ( $old['url'] ?? null ) === ( $new['url'] ?? null ) ) {
            return;
        }

        my_addon_repoint_redirect( $item_id, (string) ( $new['url'] ?? '' ) );
        my_addon_clear_link_check_cache( $item_id );
    },
    10,
    4
);

add_action(
    'benecaster_reference_library_item_deleted',
    function ( int $item_id, int $show_id ): void {
        // The library item and every episode reference citing it are already
        // gone. Retire the redirect rather than leave it pointing at a
        // reference nobody can see any more.
        my_addon_retire_redirect( $item_id );
    },
    10,
    2
);

View on GitHub →

Hooks Used

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