Skip to content

Instantly share code, notes, and snippets.

@henkealg
Last active May 15, 2017 12:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save henkealg/7d865502c9b5a424cffa1a0f12473bf0 to your computer and use it in GitHub Desktop.
Save henkealg/7d865502c9b5a424cffa1a0f12473bf0 to your computer and use it in GitHub Desktop.
Adds custom XML tags to the default WP RSS feed output.
<?php
/*
Adds custom tags to the default WP RSS feed output
Reference: https://codex.wordpress.org/Plugin_API/Action_Reference/rss2_item
*/
function rss_add_custom_tags() {
global $post;
// add post featured image to the feed item if present
if(has_post_thumbnail($post->ID)):
$thumbnail = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), 'full');
echo"\t<image>{$thumbnail[0]}</image>\n";
endif;
// add post categories if present
$cats = wp_get_post_categories($post->ID, array('fields' => 'all'));
foreach ($cats as $cat) echo"\t<categoryLink>" . get_category_link($cat->term_id) . "::" . $cat->name . "</categoryLink>\n";
// add post tags if present
$tags = wp_get_post_tags($post->ID);
foreach ($tags as $tag) echo"\t<tagLink>" . get_tag_link($tag->term_id). "::" . $tag->name . "</tagLink>\n";
}
add_action('rss2_item', 'rss_add_custom_tags');
add_action('rss_item', 'rss_add_custom_tags');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment