I am trying to display an image in a Views RSS feed.

  1. I have added the following fields to my View: field_image, field_title, field_twitter (These fields come from my node).
  2. I then set all fields to "Exclude from display".
  3. I then create a new field in Views called "RSS Content".
  4. I then place tokens for all the fields (from step 1) into "RSS Content".
  5. Finally, I go to "Views >> Show >> Settings". In the drop down menu for "Description field", I select "RSS Content".

When I look at my RSS feed, the image field is displayed like this:

&lt;img src=&quot;http://example.com/sites/files/nodes/content/images/intro/two-hearts-1312984-1280x960_0.jpg&quot; /&gt;

How do I get the image to display properly, instead of the image tags being escaped?

  • 1
    In the view where you have set the fields not to display, what are the settings for the field? Is it displaying the file's URL or the image? Whichever one it is, try switching it.
    – R. Durham
    Oct 28, 2015 at 20:56
  • 1
    @Ruby It was image. I tried file but it still escapes the <img> tag
    – big_smile
    Oct 29, 2015 at 18:59
  • Hmm. Well if it helps, on my main site we have set up the display of the RSS feed as a display on the content type. Then the view that generates the feed is in content mode using that display rather than specifying fields.
    – R. Durham
    Oct 29, 2015 at 19:05
  • Please test with a RSS reader. Even if you see the escaped image html tag in the code preview of Views, the RSS reader should convert this code and display the image properly.
    – user55203
    Dec 10, 2015 at 11:52

4 Answers 4


First add the Media RSS namespace to your views-view-rss.tpl.php

<rss version="2.0" xml:base="<?php print $link; ?>"<?php print $namespaces; ?> xmlns:media="http://search.yahoo.com/mrss/">

Then add the tag to your views-view-row-rss.tpl.php

<media:content url="<?php print image_style_url('my_img_style_id', $node->field_image['und'][0]['uri']); ?>" type="image/jpeg" medium="image" height="800" width="600" lang="en" />

Lastly make the field data available to your theme's template

function mytheme_preprocess_views_view_row_rss(&$vars) {
  $view     = &$vars['view'];
  $options  = &$vars['options'];
  $item     = &$vars['row'];

  // Use the [id] of the returned results to determine the nid in [results]
  $result = &$vars['view']->result;
  $id   = &$vars['id'];
  $node   = node_load( $result[$id-1]->nid );
  $vars['node'] = $node;

Original article: https://blog.sam-thompson.info/images-in-drupal-rss-feed-without-additional-modules/


I have come across similar problems and somehow CDATA wrapping didn't work for me. I had to override the rss view and create the cdata tag and wrap image inside it programmatically.

I have done this by overriding the RSS view created by views_rss and views_data_export.

Here is how my view setup looks like: enter image description here

To RSS via custom tpl display I created another display called as Feed which is a data export view of first display.

Here is how the feed display look like which I have overridden. enter image description here

Next I overrode the Feed display tpl's and created the CDATA image field programmatically.

Here is the view tpl files that I overrode. enter image description here

Now you would need to preprocess rss view in template to make node's nid available to the views.

Here is how I did that.

function bootstrap_preprocess_views_view_row_rss(&$vars) {
$view     = &$vars['view'];
$options  = &$vars['options'];
$item     = &$vars['row'];

// Use the [id] of the returned results to determine the nid in [results]
$result = &$vars['view']->result;
$id   = &$vars['id'];
$node   = node_load( $result[$id-1]->nid );

$vars['title'] = check_plain($item->title);
$vars['link'] = check_url($item->link);
$vars['description'] = check_plain($item->description);
//$vars['description'] = check_plain($node->teaser);
$vars['node'] = $node;
$vars['item_elements'] = empty($item->elements) ? '' : 

Lastly here is how I created the CDATA tag programmatically with my custom image style.

Code for two view templates: 1. Display template

<?php print "<?xml"; ?> version="1.0" encoding="utf-8" <?php print "?>"; ?>
<rss version="2.0" xml:base="<?php print $link; ?>"<?php print $namespaces; 
<title><?php print $title; ?></title>
<link><?php print $link; ?></link>
<description><?php print $description; ?></description>
<language><?php print $langcode; ?></language>
<?php print $channel_elements; ?>
<?php print $items; ?>
  1. Row template. Here I am creating CDATA and item tag programmatically.

enter image description here

Hope this helps.


In case someone is looking for a Drupal 9 version of this, here's the code considering your image field is named field_image and you want to use an image style which is called large in this case:

In your mytheme.theme:

use Drupal\node\Entity\Node;

function mytheme_preprocess_views_view_row_rss(&$variables) {
  if($variables['view']->id() == 'your_view_id'){
    $node = Node::load($variables['item_elements'][2]["value"]->__toString());
    $style = \Drupal\image\Entity\ImageStyle::load('large');
    $variables['media'] = $style->buildUrl($node->field_image->entity->getFileUri());
    $variables['media_size'] = $node->field_image->entity->getSize();

Then in your Twig file views-view-row-rss.html.twig:

  <title>{{ title }}</title>
  <link>{{ link }}</link>
  <description>{{ description }}</description>
  {% for item in item_elements -%}
    <{{ item.key }}{{ item.attributes -}}
    {% if item.value -%}
      >{{ item.value }}</{{ item.key }}>
    {% else -%}
      {{ ' />' }}
    {% endif %}
  {%- endfor %}
  <media:content url="{{ media }}" fileSize="{{ media_size }}" type="image/jpeg" />

wrap it on CDATA tags.

CDATA stands for Character Data and it means that the data in between these strings includes data that could be interpreted as XML markup, but should not be.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.