Light-weight, fast, simple and powerful!
Translations of this page?:

Generate valid Atom feeds for archive pages


Wolf comes with a predefined page for RSS feed, however, I didn't find it to be satisfying enough. There were mostly syntactic issues I wasn't very fond of. The old code though has long since been rewritten and I don't have any copies lying around so I can't comment on specific issues. Also I'm to lazy to search Wolf's source for the sake of this tutorial right now.

Here's how you make a better Atom feed:

1. Create a new Layout Name: Atom XML Content type: application/atom-xml Content:

<?php echo $this->content(); ?>

2. Create (or edit existing) a Page for your feed (should be a child to root (Home Page)) Title: Atom Feed Filter: -none- Status: Hidden On Metadata tab set slug to feed.xml On Settings tab set Layout to Atom XML Page content:

<?php echo "<?"; ?>xml version="1.0" encoding="utf-8" <?php echo "?>\n"; ?>
<feed xmlns="">
  <title type="text">{SITE TITLE}</title>
  <subtitle type="text">{SUBTITLE}</subtitle>
  <updated><?php echo strftime('%Y-%m-%dT%H:%M:%S{+02:00}'); ?></updated>
  <rights>{CONTENT LICENSE}</rights>
  <link rel="alternate" type="text/html" href="<?php echo BASE_URL; ?>" />
  <link rel="self" type="application/atom+xml" href="<?php echo BASE_URL; ?>{FEED SLUG}" />
  <?php $articles = $this->find('{ARTICLES}'); ?>
  <?php foreach ($articles->children(array('limit' => 10, 'order' => 'page.created_on DESC')) as $article): ?>
      <id>tag:{DOMAIN.COM},<?php echo $article->date('%Y-%m-%d', 'published'); ?>:/{ARTICLES}/<?php echo $article->slug(); ?>/</id>
      <title><?php echo $article->title(); ?></title>
      <link rel="alternate" type="text/html" href="<?php echo $article->url(); ?>" />
      <published><?php echo $article->date('%Y-%m-%dT%H:%M:%S{+02:00}', 'published'); ?></published>
      <updated><?php echo $article->date('%Y-%m-%dT%H:%M:%S{+02:00}', 'updated'); ?></updated>
        <name><?php echo $article->author(); ?></name>
      <content type="xhtml">
        <div xmlns="">
            echo $article->content();
            if ($article->hasContent('extended')) { echo '<p><a href="' . $article->url() . '">{READ COMPLETE ARTICLE ON WEBSITE LINK TEXT}</a></p>'; 
  <?php endforeach; ?>

You need to edit the values in curly brackets:

{DOMAIN.COM}, {YEAR} and {ID_TITLE} are part of the ID tag. - Inside <feed> the ID should look like this: <id>,2011:My_Site_Title</id>. - Inside <entry> the ID should look like this: <id>,2011-05-03:/articles/my-article-slug-name/</id> so make sure to set {DOMAIN.COM} and {ARTICLES} (the slug for your articles page - by default it's “articles” unless you changed it as described in this guide) to appropriate values. More about Atom IDs.

{SITE TITLE} - Set this to your site's title.

{SUBTITLE} - Your site's subtitle or slogan.

{+02:00} - This is the Timezone offset relative to UTC. Central European Summer Time is +02:00, while Eastern Standard Time would be -05:00. If you're in UTC, replace offset with the letter “Z”. Make sure you remove the curly brackets.

{CONTENT LICENSE} - Human readable licensing terms for your content for instance: Creative Commons - attribution (CC-by).

{FEED SLUG} - Set this to whatever you set the slug for the feed page (feed.xml).

{ARTICLES} - As already explained, this should be set to whatever the name (slug) of your articles page is.

{READ COMPLETE ARTICLE ON WEBSITE LINK TEXT} - By default the feed shows the same content as your “articles” page, so this will act the same as the “Read more” links if you use them.

3. By default, if you followed this guide, your feed url will be <Wolf's BASE_URL>{FEED SLUG} ( To make your feed visible to browsers add this to the <head> section of your HTML layout: <link rel=“alternate” type=“application/atom-xml” title=”{SITE TITLE}” href=”<?php echo BASE_URL; ?>{FEED SLUG}” />

Now you have an Atom feed on your site that doesn't strip HTML tags, follows the spec and validates.

Note, however, that we use XHTML content type for feed entries. Your articles should conform to the XHTML spec. You can also use HTML content type, but then the code would be a little different:

<content type="html">
    echo htmlspecialchars($article->content(),ENT_NOQUOTES);
    if ($article->hasContent('extended')) { echo '&lt;p>&lt;a href="' . $article->url() . '">{READ COMPLETE ARTICLE ON WEBSITE LINK TEXT}&lt;/a>&lt;/p>'; }

This still won't validate the new HTML5 tags though.

The content of this post is in public domain. You can do whatever you want with it.

tutorials/generate_valid_atom_feeds.txt · Last modified: 2012-11-15 13:19 by Fortron
Except where otherwise noted, content on this wiki is licensed under the following license:GNU Free Documentation License 1.2
Copyright 2010 / design by yello studio / Wolf CMS Inside