Customizing RSS Feed Links in WordPress

We’re doing the Digital Detox again this year. Like last year, I built a little headless html/js page for the content.

We have something set to syndicate our posts into Slack. Jeni noted that those URLs lead to the main blog rather than the headless site. I don’t think there are many people reading our posts via this method or via a feed reader but I wondered how hard it be to adjust those URLs for posts in the Digital Detox 2023 category.

Turns out it wasn’t too bad.

The following code flips both the URLs in the RSS.

function fix_detox_links($post_permalink) {
    if(has_category( 'digital-detox-2023', get_the_ID() )){//check the category and do something if the post is in that category
    return 'https://dlinq.middcreate.net/detox-2023/post.html?id=' . get_the_ID(); //create new link
   } else {
    return $post_permalink;
   }
    
}
add_filter('the_permalink_rss', 'fix_detox_links');//replace the link
add_filter('get_the_guid', 'fix_detox_links');//replace the guid

You can see how it impacts the RSS in the image below. The first post is the detox post. The second post shows that a normal post keeps the regular links.
A screenshot of the RSS feed showing that the first post has the modified links in two places because it's in a particular category. The second post has regular URLs and is not in the detox category.