Skip to content

Instantly share code, notes, and snippets.

@artlung
Last active May 24, 2024 01:23
Show Gist options
  • Save artlung/b0b5c525cee32ec300786313b8f1a8d8 to your computer and use it in GitHub Desktop.
Save artlung/b0b5c525cee32ec300786313b8f1a8d8 to your computer and use it in GitHub Desktop.
<?php
/*
Plugin Name: Customize Feed Content
Description: I'm looking for a theme-independent plug-in that will let me customize my blog post excerpts for my RSS feed. I would like to be able to show the featured image and customize the length of the preview, as well as add a link underneath the excerpt for a viewer to click on to see the original article.
Version: 1.0
Author: artlung
*/
class CustomizeFeedContent {
/**
* @param $content
*
* @return string
*/
public static function addFeaturedImage( $content ) {
$featured_image_url = get_the_post_thumbnail_url(get_the_ID(), '200');
$featured_image = '';
if ($featured_image_url) {
$featured_image = sprintf('<figure><img src="%s" alt="Featured Image" /></figure>', $featured_image_url);
}
return $featured_image . $content;
}
/**
* @param string $content
*
* @return string
*/
public static function addReadMoreLink( string $content ) {
$permalink = get_permalink();
$read_more = sprintf('<p><a href="%s">Read all of "%s" on "%s"</a></p>', $permalink, get_the_title(), get_bloginfo('name') );
return $content . $read_more;
}
}
/**
* Adds any featured image to the full feed content.
*
* @param string $content The current content of the full feed.
* @return string The modified content of the full feed.
*/
function customize_full_feed_content($content): string {
$content = CustomizeFeedContent::addFeaturedImage($content);
return $content;
}
add_filter('the_content_feed', 'customize_full_feed_content' );
/**
* Adds any featured image to the excerpt feed content.
* Also adds a "Read More" link to the full article.
* @param $content
* @return string
*/
function customize_exerpt_feed_content($content): string {
$content = CustomizeFeedContent::addFeaturedImage($content);
$content = CustomizeFeedContent::addReadMoreLink($content);
return $content;
}
add_filter('the_excerpt_rss', 'customize_exerpt_feed_content' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment