Search code examples
phpwordpressrssfeed

Add thumbnail to WordPress RSS with another tag


I am using a Wordpress RSS to use in my iOS project. The feed doesn't have thumbnail links in it, so I searched and found this code to add thumbnail links to feed.

/* include thumbnail in RSS feed */
function add_thumb_to_RSS($content) {
   global $post;
   if ( has_post_thumbnail( $post->ID ) ){
      $content = '' . get_the_post_thumbnail( $post->ID, 'thumbnail' ) . '' . $content;
   }
   return $content;
}
add_filter('the_excerpt_rss', 'add_thumb_to_RSS');
add_filter('the_content_feed', 'add_thumb_to_RSS')

This code adds the image link in feed, but it adds as html code in the beginning of description tag like this:

<description>
<![CDATA[
<img width="150" height="150" src="http://www.ipadia.co/wp-content/uploads/2012/02/sayfada-bul-150x150.png" class="attachment-thumbnail wp-post-image" alt="sayfada bul" title="sayfada bul" />Some text some text Some text some text Some text some text Some text some text Some text some text Some text some text ....
]]>
</description>

I want to add image link with another tag like <image> or <thumb> the link </thumb>. So I can parse this more easily.

How can I do this? Thanks in advance.


Solution

  • I solved it finally :) I changed the function that I posted before. The new function is this:

    add_action('rss2_item', function(){
      global $post;
    
      $output = '';
      $thumbnail_ID = get_post_thumbnail_id( $post->ID );
      $thumbnail = wp_get_attachment_image_src($thumbnail_ID, 'thumbnail');
      $output .= '<post-thumbnail>';
        $output .= '<url>'. $thumbnail[0] .'</url>';
        $output .= '<width>'. $thumbnail[1] .'</width>';
        $output .= '<height>'. $thumbnail[2] .'</height>';
        $output .= '</post-thumbnail>';
    
      echo $output;
    });
    

    This gives the image link in new tag <post-thumbnail> like I wanted.