2011-07-13, 06:24:42
RSS Generation
My clients needed an RSS Feed of their blog so they can submit it to Facebook.
I will post the 2 simple changes I made to your news plugin for automatic rss generation.
Maybe it can give you a starting point on adding the functionality or help someone else out.
I added the following function to "plugins/news_manager/inc/admin.php" , above the nm_save_post" function:
Then I executed that function right at the end of the "nm_save_post" function. That way a new rss feed will be generated after each page save. Note the RSS feed has a limit of 5 posts (you can change that by changing the limit variable above). The RSS Feed is also sorted by date of edited/created posts.
Below is a snippet of where I exectuted the "rss_feed_generate()" function:
My clients needed an RSS Feed of their blog so they can submit it to Facebook.
I will post the 2 simple changes I made to your news plugin for automatic rss generation.
Maybe it can give you a starting point on adding the functionality or help someone else out.
I added the following function to "plugins/news_manager/inc/admin.php" , above the nm_save_post" function:
Code:
function rss_feed_generate()
{
global $SITEURL;
$dir = "../data/posts";
$RSSString = "";
$RSSString .= "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
$RSSString .= "<rss version=\"2.0\">\n";
$RSSString .= "<channel>\n";
$RSSString .= "<title>$SITEURL - RSS Feed</title>\n";
$RSSString .= "<link>$SITEURL</link>\n";
$RSSString .= "<description>$SITEURL</description>\n";
$RSSString .= "<lastBuildDate>".date("D, j M Y H:i:s T")."</lastBuildDate>\n";
$RSSString .= "<language>en-us</language>\n";
$post_array = glob($dir . "/*.xml");
$limit = "5";
array_multisort(array_map('filemtime', $post_array), SORT_DESC, $post_array);
$post_array = array_slice($post_array, 0, $limit);
foreach($list as $file) {
include $file;
}
foreach ($post_array as $filename) {
$blog_post = simplexml_load_file($filename);
$RSSDate = $blog_post->date;
$RSSTitle = $blog_post->title;
$RSSBody = html_entity_decode($blog_post->content);
$RSSBody = str_replace(" ", " ", $RSSBody);
$RSSBody = substr(htmlspecialchars(strip_tags($RSSBody)),0,200)."...
";
$ID = str_replace("../data/posts/", "", $filename);
$ID = str_replace(".xml", "", $ID);
$RSSString .= "<item>\n";
$RSSString .= "\t <title>".$RSSTitle."</title>\n";
$RSSString .= "\t <link>http://$SITEURL/index.php?id=blog&post=".$ID."</link>\n";
$RSSString .= "\t <guid>http://$SITEURL/index.php?id=blog&post=".$ID."</guid>\n";
$RSSString .= "\t <description>".$RSSBody."</description>\n";
$RSSString .= "</item>\n";
}
$RSSString .= "</channel>\n";
$RSSString .= "</rss>\n";
if(!$fp = fopen("../rss.rss",'w')){
echo "Could not open the rss.rss file";
exit();
}
if(!fwrite($fp,$RSSString)){
echo "Could not write to rss.rss file";
exit();
}
fclose($fp);
}
Then I executed that function right at the end of the "nm_save_post" function. That way a new rss feed will be generated after each page save. Note the RSS feed has a limit of 5 posts (you can change that by changing the limit variable above). The RSS Feed is also sorted by date of edited/created posts.
Below is a snippet of where I exectuted the "rss_feed_generate()" function:
Code:
# write data to file
if (!@XMLsave($xml, $file))
nm_display_message(i18n_r('news_manager/ERROR_SAVE_POST'), TRUE);
else
nm_display_message(i18n_r('news_manager/SUCCESS_SAVE_POST'));
}
rss_feed_generate();
}