Jump to content
ryan

RSS Feed Module

Recommended Posts

Given a PageArray of pages, this module will render an RSS feed from them. This is intended to be used directly from a template file.

Installation

Download the RSS feed module at the bottom of this message. To install, unzip and place the file in /site/modules/. Go into Admin > Modules and click "Check for new modules" at the bottom. Then click the "install" button for the "MarkupRSS" module. From there, you'll be able to configure several aspects of the module.

Usage

Create a template in ProcessWire and use the following example as a guide. See the actual .module file for more configuration options too.

<?php

// retrieve the RSS module
$rss = $modules->get("MarkupRSS");

// configure the feed. see the actual module file for more optional config options.
$rss->title = "Latest updates";
$rss->description = "The most recent pages updated on my site";

// find the pages you want to appear in the feed.
// this can be any group of pages returned by $pages->find() or $page->children(), etc.
$items = $pages->find("limit=10, sort=-modified");

// send the output of the RSS feed, and you are done
$rss->render($items);

This module is now included in the ProcessWire core.

  • Like 2

Share this post


Link to post
Share on other sites

I used for two website and i cannot find any fault, it's perfect and easy to use, maybe you can add a function to cut some long text in itemDescriptionField because sometimes i could not have a summary text in my template in addition to body field and for feed it's good to have small text, but this is not a big problem.

Share this post


Link to post
Share on other sites

Thanks for your feedback. I've added an option to limit the length of the item description field. You can now set this in the module config screen. It will attempt to truncate to the closest punctuation within your max length, or at the closest space if no punctuation is close by. If you download the current ZIP file attached to the original message, that will have it (MarkupRSS-module3.zip).

Share this post


Link to post
Share on other sites

This is great news and great to get more well coded and commented examples to help module development. Haven't tried yet, but if I read correctly it should be trivial to create normal template, but add in top check if $input->get->format == rss and render rss instead of normal template.

Show we could get urls like: www.domain.com/news/ (normal news listing) and www.domain.com/news/?format=rss (rss feed).

Other way to handle rss for whole site would be one rss-template: www.domain.com/rss/?page=321 or www.domain.com/rss/?template=news etc...

I think after little testing this should come with processwire as default module (or do we call them core modules?).

Share this post


Link to post
Share on other sites

Thanks, glad you like it so far. The examples you mentioned should work well. Though I would probably recommend using URL segments rather than GET vars, so that the template output can be cachable. (GET vars aren't cachable at the template level). For example, lets say that domain.com/news/ is a page that lists an index of the latest news items. In your template, you could have it detect a URL segment "rss", so that domain.com/news/rss/ displays an RSS version of it instead:

<?php
if($input->urlSegment1 == 'rss') {
    // display the RSS feed
    $rss = $modules->get("MarkupRSS"); 
    // ...     
    $rss-render($page->children("limit=10")); 

} else {
    // display the regular news list template output
    include("./news-list.php"); 
}

I agree that this should probably be a core module (/wire/modules/) so I will add it to the source and include an RSS feed example in the default site profile.

Share this post


Link to post
Share on other sites

Thanks, glad you like it so far. The examples you mentioned should work well. Though I would probably recommend using URL segments rather than GET vars, so that the template output can be cachable. (GET vars aren't cachable at the template level).

This is good to know and thanks for clarification. So if I have any GET vars then template is never cached?

Share this post


Link to post
Share on other sites

The template will be cached, but it might never have an opportunity to see the GET vars (except when regenerating the cache). Since the output is cached, it would deliver cached output and your code that looks at the value of a GET or POST var would only run when the cache is rebuilt. This is why I usually leave off template caching for any pages that need to examine GET, POST, COOKIE or SESSION vars. Examples would be search engines, forms, etc.

Btw, the template cache is always off when you are logged in. Template caching is applicable only to anonymous users visiting the site.

The reason why ProcessWire doesn't cache GET vars is because there is an infinite number of them possible. We don't want to make it possible for someone to fill up your hard drive with a million different cached variations of the same page with different GET vars.

On the other hand, ProcessWire will cache up to 999 URL segments or page numbers, for each page that is using them (when enabled for it's template). However, unless your page is slow to render, it's simpler just to leave off template caching when pages using it need to generate different output according to the request.

Share this post


Link to post
Share on other sites

hey Ryan, i'm using this module but now i need to change the title of each item to a custom title composed by the title of the page + a custom fixed text.

Now i'm using:

$rss->itemTitleField = "title";

i would like to do something like =>  title+'my text';

Thanks

Share this post


Link to post
Share on other sites

You can modify the titles of the items before sending them to $rss->render:

<?php

// retrieve the RSS module
$rss = $modules->get("MarkupRSS"); 

// configure the feed. see the actual module file for more optional config options.
$rss->title = "Latest updates";
$rss->description = "The most recent pages updated on my site"; 

// find the pages you want to appear in the feed. 
// this can be any group of pages returned by $pages->find() or $page->children(), etc. 
$items = $pages->find("limit=10, sort=-modified"); 

// add something to the item titles
foreach($items as $item) {
    $item->title = $item->title . " my text";
}

// send the output of the RSS feed, and you are done
$rss->render($items); 
  • Like 1

Share this post


Link to post
Share on other sites

I was just wondering if this module supports more than 1 rss feed per template. ie, could i do something like this?

$rss = $modules->get("MarkupRSS");

$rss2 = $modules->get("MarkupRSS");

$rss3 = $modules->get("MarkupRSS");

...

Share this post


Link to post
Share on other sites

Yes, you can do that. This is what's called a 'singular' module, meaning it returns a new instance on every $modules->get("MarkupRSS"). So you can  create as many instances as you need, and they don't share data with each other.

Share this post


Link to post
Share on other sites

thanks! my guess was yes.. but didn't want to have to find out the hard way~

...oh, and someone else might be wondering too :rolleyes:

(btw, only taking 1.5hrs to reply my post is quite amazing!)

Share this post


Link to post
Share on other sites

(btw, only taking 1.5hrs to reply my post is quite amazing!)

Hold on ... , usually he's much faster! ;D

Share this post


Link to post
Share on other sites

My RSS Feeds seem to only show the title of the Item ( I used the above mentioned code to modify the title of the items). I noticed in MarkupRSS.module that the $defaultConfigData array has

itemDescriptionField => 'summary'

My pages don't have a summary field.

Is there a way to make my RSS feed show more than just a title? ie some of the $body? I tried changing the module code to

itemDescriptionField => 'body'

but it didn't work. Also, I edited the module without reloading it anyhow, could that have made it not work?

thanks,

Share this post


Link to post
Share on other sites

Hi,

I tend to use more than just the body in my pages so I code my own (http://www.smd.net.au/rss/). It outputs the body and then my project images which matches the order on my site.

Here's what it looks like:

<?php header('Content-Type: application/rss+xml; charset=utf-8'); ?>
<?php echo "<?phpxml version=\"1.0\" encoding=\"utf-8\"?>\n"; ?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom">
<channel>
<title>stillmovingdesign</title>
<link>http://www.smd.net.au/rss</link>
<description>RSS Feed</description>
<?php
$featured = $pages->get("/work/")->find("template=work, sort=sort, limit=999");
foreach($featured as $feature) {
$image = $feature->images->first();
$h = $image->height();
$w = $image->width();
$thumb = $image->size(310,0);
echo "<item>\n<title>{$feature->title}</title>\n<link>http://www.smd.net.au{$feature->url}</link><dc:creator>stillmovingdesign</dc:creator><guid isPermaLink=\"false\">http://www.smd.net.au{$feature->url}</guid>";
echo "<description><![CDATA[";
echo "<p>" . $feature->summary . "</p>";
foreach($feature->images as $featuredimages) {
$large = $featuredimages->width(650);
echo "<img src='http://www.smd.net.au{$large->url}' title='{$feature->title}' alt='{$feature->title}' />\n";
}
echo "{$feature->body}";
echo "]]></description>\n";
echo "</item>\n\n";
}
?></channel></rss>

As an aside I use twitterfeed to automatically tweet each job I publish to Twitter. It works really well.

Regards

Marty

  • Like 1

Share this post


Link to post
Share on other sites
My RSS Feeds seem to only show the title of the Item ( I used the above mentioned code to modify the title of the items). I noticed in MarkupRSS.module that the $defaultConfigData array has

The item description field is actually configurable in the module settings. In the admin, go to Modules > Markup > RSS. Click on it to configure the various settings it can use. This sets the defaults for all instances of MarkupRSS. If you prefer, you can also change the settings at runtime for any individual instance like this:

<?php
$rss = $modules->get('MarkupRSS'); 
$rss->itemDescriptionField = 'body';
echo $rss->render($yourPages); 

Also, thanks for the great example stillmovingdesign!

Share this post


Link to post
Share on other sites

I noticed that itemDescriptionField gets tags stripped. It would be great if this was a boolean option for this. I usually set up my blog(or other RSS feeds) to show the full article so anyone can view the entire thing in their reader.

Share this post


Link to post
Share on other sites

Clinton, I've updated the module to support this. You'll find it in the latest commit in the PW 2.2 source. To enable, specify '0' as the max length for the description field in the module settings. Can you confirm that this solves the issue and enables use of the full article?

Thanks,

Ryan

  • Like 1

Share this post


Link to post
Share on other sites

Thanks for reporting back. Glad to hear it's working how it should.

Share this post


Link to post
Share on other sites

I am just trying to retrieve the rss feed and I am getting this error, when opening the page, with the template containing the following rss code.

This page contains the following errors:
error on line 1 at column 2: StartTag: invalid element name
Below is a rendering of the page up to the first error.

Where as "below" nothing appears.

The Code:

<?php 
// retrieve the RSS module
$rss = $modules->get("MarkupRSS");

// configure the feed. see the actual module file for more optional config options.
$rss->title = "Latest updates";
$rss->description = "The most recent pages updated on my site";

// find the pages you want to appear in the feed.
// this can be any group of pages returned by $pages->find() or $page->children(), etc.
$items = $pages->find("tags=short");

// send the output of the RSS feed, and you are done
$rss->render($items);

Any ideas, what I am doing wrong?

Share this post


Link to post
Share on other sites

Does it work with a more standard:

$items = $pages->find("limit=10, sort=-modified");

Also, what do you see if you view the page source - it might show the start of the xml so you can see the tag problem.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...