How to add featured image or custom field to xml feed?

I already tried the plugins RSS Manager, Add featured image to RSS feed and Featured Image In Rss Feed. But all they do is add the image inside the <description> attribute of the xml feed.

There is at least 2 problems with that:

Read More
  • The blog that’s pulling the feed truncates the <description> at about 300 characters which is fine.
  • The blog that’s pulling the feed strips html tags. I have to strip them because if there is an image inside the body of the post, it will display it. So if there are two images at the beginning of the post, it will display them and it will also display the featured image. Also, the img tag will eat up characters of the 300 character limit.

So I need to generate the RSS xml feed with the added attribute <featuredimage> or <thumbnail> or something like that. NOT embedded inside <description>.

I have searched for plugins but can’t find any that solve this problem. Thanks in advance.

Related posts

3 comments

  1. The RSS2 feed is generated in the wp-includes/feed-rss2.php file. In this file, there is an action hook named rss2_item. You can use this action hook to add tags to each item in your feed.

    There is a codex article on rss2_item with examples, including this one for adding an <image> tag:

    <?php
    add_action('rss2_item', 'add_my_rss_node');
    
    function add_my_rss_node() {
        global $post;
        if(has_post_thumbnail($post->ID)):
            $thumbnail = get_attachment_link(get_post_thumbnail_id($post->ID));
            echo("<image>{$thumbnail}</image>");
        endif;
    }
    ?>
    
  2. Working code:

    add_action('rss2_item', 'custom_thumbnail_tag');
    
    function custom_thumbnail_tag() {
    
        global $post;
        if(has_post_thumbnail($post->ID)):
            $thumbnail_ID = get_post_thumbnail_id( $post->ID );
            $thumbnail = wp_get_attachment_image_src($thumbnail_ID, 'thumbnail');
            echo("<thumbnail>{$thumbnail['0']}</thumbnail>");
        endif;
    
    }
    
  3. I am attempting to do this same thing.
    A couple resources I have been looking at so far:

    First learn the valid RSS syntax and structure. RSS2.0 Tag Syntax

    Create a hidden page in WordPress to create your own layout and call any and all fields and images you wish. Yoast Custom RSS Feed tut

Comments are closed.