Skip to main content

Map transcript speakers to guests, as Transcription’s speaker mapping does

Premium Intermediate Guest Manager

The whole contract between Guest Manager and an add-on that lets a podcaster say who a transcript’s Speaker 1 and Speaker 2 are. The mapping panel and the mapping belong to the other add-on: the Transcription add-on draws the panel in its own Transcript tab and stores the mapping per transcript, cleared when transcription runs again. Guest Manager draws nothing there and stores nothing about speakers; it supplies the readers below. Guard every call with function_exists(), which covers Guest Manager not being installed.

  1. Offer the episode’s own guests first with benecaster_guest_manager_get_episode_guests() ( $episode_id ): the guests linked to the episode, in the episode’s order, [] where Guest Manager is off for the episode’s show. Show the panel’s guest choices only when Guest Manager is on for the show (benecaster_addon_is_active() ( 'benecaster-addon-guest-manager', $show_id )); elsewhere offer typed names only.

  2. Find any other guest with GET /wp-json/benecaster-guest-manager/v1/guests?search={text}&per_page={1–100} (the Guests routes): guests whose name or organization matches, A–Z, each with id, name, title, organization and photo_url. Admin only (manage_options and the REST nonce), so call it from the admin screen, never a public page. It lists the whole site’s roster: guests are not a show’s.

  3. Store the guest’s id and the name at that moment, both, in your own storage. The id stays true across a rename; the name is the fallback.

  4. Show the current name, falling back to the stored one, with benecaster_guest_manager_get_guest() ( $guest_id ). Use its name when it returns an array and Guest Manager is on for the episode’s show. Use the stored name when it returns null (the guest was deleted, trashed or unpublished, or Guest Manager is off on every show) or Guest Manager is off for the episode’s show. A rename then reaches every transcript that maps the guest (the page, the exports, <podcast:transcript>), and a deleted guest leaves the name the podcaster saw when mapping. Do this wherever the name is drawn, not once at mapping time.

  5. Refresh caches on a rename or deletion with benecaster_guest_manager_after_save_guest ( int $guest_id, array $fields, bool $created ) and benecaster_guest_manager_after_delete_guest ( int $guest_id ). They fire for changes made on the Guests screen and through its REST routes, including approved Guest portal changes. A change made another way (wp post update from WP-CLI, a plugin calling wp_update_post()) fires only WordPress’s own save_post_benecaster_guest; hook that too if a stale cached name would matter. Live reads need no hook: only something you cache does, such as a rendered transcript or a feed.

Code

<?php
// Where a transcript is drawn: one speaker's label.
// $mapping = [ 2 => [ 'guest_id' => 42, 'name' => 'Alice Smith' ], 1 => [ 'guest_id' => 0, 'name' => 'Pat (host)' ] ].
function my_addon_speaker_label( int $speaker, array $mapping, int $episode_id ): string {
    $mapped = $mapping[ $speaker ] ?? null;
    if ( null === $mapped ) {
        /* translators: %d: speaker number. */
        return sprintf( __( 'Speaker %d', 'my-addon' ), $speaker );
    }

    $show_id = (int) get_post_field( 'post_parent', $episode_id );
    $live    = $mapped['guest_id'] > 0
        && function_exists( 'benecaster_guest_manager_get_guest' )
        && benecaster_addon_is_active( 'benecaster-addon-guest-manager', $show_id )
        ? benecaster_guest_manager_get_guest( (int) $mapped['guest_id'] )
        : null;

    return null !== $live ? $live['name'] : (string) $mapped['name'];
}

// A rename or deletion on the Guests screen: drop rendered transcripts that name the guest.
add_action( 'benecaster_guest_manager_after_save_guest', 'my_addon_forget_guest_renders' );
add_action( 'benecaster_guest_manager_after_delete_guest', 'my_addon_forget_guest_renders' );

View on GitHub →

Hooks Used

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