Restyle or change a sponsor message where its tag shows it
benecaster_sponsor_message shows a sponsor message’s current copy (see Read or write a show’s sponsor messages from code). The copy is read each time the page is built, never stamped in, so an edit on the Sponsors screen reaches every episode that carries the tag.
It takes two attributes: id, the message, and episode_id, the episode the tag sits in (left out or 0 for the current post).
It shows the copy only when all of these hold, and nothing otherwise, never the raw tag:
- the message exists and is not archived
- the episode belongs to the message’s own show
- Sponsor Manager is on for that show
- today, in the site’s time zone, is on or between the message’s start and end dates (an empty date is open)
The tag is registered whether or not Sponsor Manager is on for any show, so a tag left in show notes disappears quietly when the add-on is off everywhere. The episode’s post status is not checked. The body is not run through do_shortcode(), so a message cannot nest tags.
- Output:
<div class="benecaster-sponsor-message">{body}</div>, the body passed throughwp_kses_postagain on the way out. Style it with that class. shortcode_atts_benecaster_sponsor_messageis WordPress’s own filter over the attributes.- benecaster_sponsor_manager_message_output
( string $html, int $message_id, int $episode_id, array $atts )filters the HTML, only when the copy is shown. It never runs for a tag that shows nothing, so it cannot make a message appear outside its show, its dates or the shows where Sponsor Manager is on. A return that is not a string is cast to one.
Code
<?php
// Label every sponsor read on the episode page.
add_filter( 'benecaster_sponsor_manager_message_output', function ( string $html, int $message_id, int $episode_id, array $atts ): string {
return '<p class="sponsor-label">' . esc_html__( 'Sponsored', 'my-theme' ) . '</p>' . $html;
}, 10, 4 );
Hooks Used
Need this built rather than just documented? See our services →