Created
August 22, 2024 08:58
-
-
Save Dan-Q/841bca74809584884ad734d41f240532 to your computer and use it in GitHub Desktop.
Subset of the code included in DanQ.me's theme, providing podcasting functionality into an RSS feed. See https://danq.me/dan-q-the-podcast for further explanation.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
define('PODCAST_TAG', 'dancast'); | |
define('PODCAST_DEFAULT_IMAGE', 'https://danq.me/wp-content/uploads/2024/05/podcast-art.jpg'); | |
define('PODCAST_SUMMARY', "A podcast nobody asked for about things only Dan cares about. A spin-off of the blog of Dan Q, who's been making random stuff on the Internet since the 1990s."); | |
define('PODCAST_URL', 'https://danq.me/tag/dancast/'); | |
define('PODCAST_TITLE', 'Dan Q - The Podcast'); | |
/** | |
* If the 'podcast_link' postmeta is filled, advertise it appropriately in RSS: | |
*/ | |
function q23_podcast_in_rss() { | |
// Don't apply when not requesting a particular tag, in a feed | |
if ( ! is_feed() || ! is_tag(PODCAST_TAG) ) return; | |
// Get the podcast link | |
global $wp_query; | |
$postid = $wp_query->post->ID; | |
$podcast_link = get_post_meta($postid, 'podcast_link', true); | |
$podcast_duration = get_post_meta($postid, 'podcast_duration', true); | |
$podcast_image = get_post_meta($postid, 'podcast_image', true); | |
$podcast_explicit = get_post_meta($postid, 'podcast_explicit', true); | |
// If it's blank, return the content as-is: | |
if ( empty($podcast_link) ) return; | |
// Ensure the file exists! | |
$filepath = $_SERVER['DOCUMENT_ROOT'] . '/wp-content/uploads/'. $podcast_link; | |
if ( ! file_exists( $filepath ) ) return; | |
// Get its size in bytes: | |
$filesize = filesize($filepath); | |
// Otherwise, add it to the content: | |
?> | |
<enclosure url="https://danq.me/_q23u/<?php echo $podcast_link; ?>" length="<?php echo $filesize; ?>" type="audio/mpeg" /> | |
<?php if( ! empty( $podcast_duration ) ) echo "<itunes:duration>$podcast_duration</itunes:duration>"; ?> | |
<?php if( ! empty( $podcast_image ) ) echo "<itunes:image href=\"$podcast_image\" />"; ?> | |
<?php if( ! empty( $podcast_explicit ) ) echo "<itunes:explicit>$podcast_explicit</itunes:explicit>"; ?> | |
<?php | |
} | |
add_action('rss2_item', 'q23_podcast_in_rss'); | |
/** | |
* Update the descriptions of podcasts in their RSS feed: | |
*/ | |
function q23_podcast_in_rss_content( $output ) { | |
// Don't apply when not requesting a particular tag, in a feed | |
if ( ! is_feed() || ! is_tag(PODCAST_TAG) ) return $output; | |
// Update the content: | |
$permalink = get_the_permalink(); | |
$output = "Based on an original blog post at: $permalink<br><br>" . $output; | |
$podcast_desc = get_post_meta( get_the_ID(), 'podcast_desc', true ); | |
if( ! empty( $podcast_desc ) ) $output = $output . "<br><br>" . nl2br( $podcast_desc ); | |
return $output; | |
} | |
add_action('the_excerpt_rss', 'q23_podcast_in_rss_content'); | |
/** | |
* Podcasts must only have one <enclosure>; let's ensure the others don't occur. | |
*/ | |
function q23_podcast_remove_other_enclosures( $html_link_tag ) { | |
// Don't apply when not requesting a particular tag, in a feed | |
if ( ! is_feed() || ! is_tag(PODCAST_TAG) ) return $html_link_tag; | |
return ''; | |
} | |
add_filter('rss_enclosure', 'q23_podcast_remove_other_enclosures'); | |
/** | |
* Don't need full HTML content in a podcast feed. | |
*/ | |
function q23_podcast_remove_content_encoded( $content ) { | |
// Don't apply when not requesting a particular tag, in a feed | |
if ( ! is_feed() || ! is_tag(PODCAST_TAG) ) return $content; | |
return ''; | |
} | |
add_filter('the_content_feed', 'q23_podcast_remove_content_encoded', 100); | |
/** | |
* Add the iTunes namespace to the RSS feed: | |
*/ | |
function q23_podcast_namespace() { | |
// Don't apply when not requesting a particular tag, in a feed | |
if ( ! is_feed() || ! is_tag(PODCAST_TAG) ) return; | |
echo " xmlns:itunes=\"http://www.itunes.com/dtds/podcast-1.0.dtd\" xmlns:podcast=\"https://podcastindex.org/namespace/1.0\"\n"; | |
} | |
add_action('rss2_ns', 'q23_podcast_namespace'); | |
/** | |
* Add the iTunes-specific tags to the RSS feed as a whole: | |
*/ | |
function q23_podcast_head() { | |
// Don't apply when not requesting a particular tag, in a feed | |
if ( ! is_feed() || ! is_tag(PODCAST_TAG) ) return; | |
?> | |
<podcast:locked>no</podcast:locked> | |
<podcast:guid>6bd2323b-1060-504d-8d3c-5dbae5c420be</podcast:guid> | |
<itunes:category text="Society & Culture"> | |
<itunes:category text="Personal Journals"/> | |
</itunes:category> | |
<itunes:category text="Technology" /> | |
<itunes:image href="<?php echo PODCAST_DEFAULT_IMAGE; ?>" /> | |
<itunes:author>Dan Q</itunes:author> | |
<itunes:type>episodic</itunes:type> | |
<itunes:explicit>true</itunes:explicit> | |
<copyright>© Dan Q <?php echo date('Y'); ?>; all original content CC-BY-AT; details at https://danq.me/license/</copyright> | |
<podcast:license>https://danq.me/license/</podcast:license> | |
<podcast:medium>blog</podcast:medium> | |
<itunes:owner> | |
<itunes:name>Dan Q</itunes:name> | |
<itunes:email>podcast@danq.me</itunes:email> | |
</itunes:owner> | |
<?php | |
} | |
add_action('rss2_head', 'q23_podcast_head'); | |
function q23_podcast_description( $current, $show ){ | |
if ( $show != 'description' ) return $current; | |
if ( ! is_feed() || ! is_tag(PODCAST_TAG) ) return $current; | |
return PODCAST_SUMMARY; | |
} | |
add_action('bloginfo_rss', 'q23_podcast_description', 10, 2); | |
function q23_get_podcast_url( $current, $show ){ | |
if ( $show != 'wpurl' && $show != 'url' && $show != 'siteurl' && $show != 'home' ) return $current; | |
if ( ! is_feed() || ! is_tag(PODCAST_TAG) ) return $current; | |
return PODCAST_URL; | |
} | |
add_action('get_bloginfo_rss', 'q23_get_podcast_url', 10, 2); | |
function q23_get_podcast_name( $current, $show ){ | |
if ( $show != 'name' ) return $current; | |
if ( ! is_feed() || ! is_tag(PODCAST_TAG) ) return $current; | |
return PODCAST_TITLE; | |
} | |
add_action('get_bloginfo_rss', 'q23_get_podcast_name', 10, 2); | |
function q23_podcast_title($title) { | |
// Don't apply when not requesting a particular tag, in a feed | |
if ( ! is_feed() || ! is_tag(PODCAST_TAG) ) return $title; | |
return PODCAST_TITLE; | |
} | |
add_filter('wp_title_rss', 'q23_podcast_title'); | |
add_action('get_wp_title_rss', 'q23_podcast_title'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This code above is released into the public domain under the Unlicense. Do whatever you like with it.