Skip to content

Instantly share code, notes, and snippets.

@frankmeeuwsen
Created October 6, 2019 19:35
Show Gist options
  • Save frankmeeuwsen/a9409c0fced66c8bb560bca9ac82f538 to your computer and use it in GitHub Desktop.
Save frankmeeuwsen/a9409c0fced66c8bb560bca9ac82f538 to your computer and use it in GitHub Desktop.
How to get a specific text at a specific category in your WordPress feed
function dtd_rssclub($content) {
global $wp_query;
$postid = $wp_query->post->ID;
if(is_feed() && has_category('RSS-Club', $postid) ) {
$content = '<div>Dit is een geheim bericht voor iedereen. <a href="/rss-club">Lees alles over de RSS Club</a></div><br /><br />'.$content;
}
else {
$content = $content;
}
return $content;
}
add_filter('the_excerpt_rss', 'dtd_rssclub');
add_filter('the_content_rss', 'dtd_rssclub');
@janboddez
Copy link

janboddez commented Oct 7, 2019

Hi there! I've yet to figure out how to 'correctly' respond to hidden posts, so I'm just going to comment here.

Below code should work. You might get away with hooking both filters to the very same callback function, as you've done above, but there's a slight catch: excerpts don't usually contain HTML, like hyperlinks, and thus aren't usually wrapped in paragraph tags (although all of that is filterable, too). I've chosen to treat excerpts separately.

In short:

  • the_content_rss is deprecated; use the_content_feed instead (which actually accepts a second argument, but we don't need it)
  • no need for is_feed() this filter should never get called outside of a feed context
  • you can directly access the global $post; no need for $wp_query
  • $content = $content; doesn't actually do anything; the entire else branch can be left out ($content will only get modified if the condition is true)
  • get_permalink( get_page_by_path( 'rss-club' ) ) returns an absolute, rather than a root-relative link; this ensures the link works inside all feed readers
  • for that last bit to work, you'll need (!) a page with slug 'rss-club'; if you wanted to make it more robust, you could check for its existence before modifying $content
  • err, that's it :-)
add_filter( 'the_content_feed', 'my_content_feed' );
function my_content_feed( $content ) {
	global $post;

	if ( has_category( 'rss-club', $post->ID ) ) {
		$content = '<p>Dit is een geheim bericht voor iedereen. <a href="' . get_permalink( get_page_by_path( 'rss-club' ) ) . '">Lees alles over de RSS Club.</a></p>' . $content;
	}

	return $content;
}

add_filter( 'the_excerpt_rss', 'my_excerpt_rss' );
function my_excerpt_rss( $content ) {
	global $post;

	if ( has_category( 'rss-club', $post->ID ) ) {
		// Excerpts usually don't contain HTML. Leave out the link.
		// $content = 'Dit bericht is alleen voor abonnees. ' . $content;

		// However, you probably could get away with it, like so:
		$content = '<p>Dit is een geheim bericht voor iedereen. <a href="' . get_permalink( get_page_by_path( 'rss-club' ) ) . '">Lees alles over de RSS Club.</a></p><p>' . $content . '</p>';
	}

	return $content;
}

@frankmeeuwsen
Copy link
Author

Awesome! I will test this and report back! Oh and any comment on any platform is fine by me ;-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment