Combine Two WordPress RSS feed into a single Feed

by admin on November 15, 2007 · 7 comments

in php, programming, Scripts, Technology, Tips, Tutorials, Ubuntu Linux, Wordpress

If you have two blogs installed on same domain, you may want to combine two blogs feed into one blog feed. It will provide few benefits:

a) Ease of feed management

b) More readers

c) Better feed stats and much more

Required tools

=> MagpieRSS rss parser for php (download link)

=> FeedCreator lets you easily create RSS 0.91, 1.0 or 2.0, ATOM 0.3 and OPML 1.0 feeds with PHP (download link).

Download required tools

Download required tools to your desktop and upload to your server in myfeed directory using ftp / sftp client. Following instructions assumes that you have a shell access to server:
mkdir myfeed
cd myfeed
wget http://internap.dl.sourceforge.net/sourceforge/magpierss/magpierss-0.72.tar.gz
tar -zxvf magpierss-0.72.tar.gz
cp magpierss-0.72/*.inc .
mv magpierss-0.72/extlib/ .
rm -rf magpierss-0.72*
wget http://www.bitfolge.de/download/feedcreator_172.zip
unzip feedcreator_172.zip
rm *.zip *.txt

Create rss.php script

Now you have all required tools, just create rss.php script :

  1. <?php
  2. $TMP_ROOT = "/tmp";
  3. $DOMAIN_NAME = "http://theos.in/";
  4. $SITE_TITLE = "Your Site / blog Title";
  5. $SITE_DESRIPTION = "Your Site / blog Description";
  6. $SITE_AUTHOR = "Vivek";
  7. $SITE_LOG_URL = $DOMAIN_NAME."logo.jpg";
  8. $RSS_DIR = "/home/lighttpd/theos.in/http";
  9.  
  10. define('MAGPIE_DIR', $RSS_DIR.'/myfeed/');
  11. define('MAGPIE_CACHE_DIR', $TMP_ROOT.'/rsscache');
  12.  
  13. /* include required files */
  14. @require_once(MAGPIE_DIR.'rss_fetch.inc');
  15. @include(MAGPIE_DIR.'feedcreator.class.php');
  16.  
  17. /* Set RSS properties */
  18. $rss = new UniversalFeedCreator();
  19. $rss->useCached();
  20. $rss->title = $SITE_TITLE;
  21. $rss->description = $SITE_DESRIPTION;
  22. $rss->link = $DOMAIN_NAME;
  23. $rss->syndicationURL = $DOMAIN_NAME."/rss/rss.php";
  24.  
  25. /* Set Image properties */
  26. $image = new FeedImage();
  27. $image->title = $SITE_TITLE. " Logo";
  28. $image->url = $SITE_LOG_URL;
  29. $image->link = $DOMAIN_NAME;
  30. $image->description = "Feed provided by ". $SITE_TITLE. ". Click to visit.";
  31. $rss->image = $image;
  32.  
  33. /**************************************************************
  34. showSummery() - Fetch and build new feed
  35. $url = Feed url
  36. $num = Show the most recent posts (default = 10 )
  37. $showfullfeed = true or false, for each article, show full text or summary (default false / summary text)
  38. ***************************************************************/
  39. function showSummery($url,$num=10,$showfullfeed=false){
  40. global $rss, $DOMAIN_NAME, $SITE_AUTHOR, $SITE_TITLE;
  41. $num_items = $num;
  42. @$rss1 = fetch_rss( $url );
  43. if ( $rss1 )
  44. {
  45. $items = array_slice($rss1->items, 0, $num_items);
  46. foreach ($items as $item)
  47. {
  48. $href = $item['link'];
  49. $title = $item['title'];
  50. if ( ! $showfullfeed ) {
  51. $desc = $item['description'];
  52. }else{
  53. $desc = $item['content']['encoded'];
  54. }
  55. $desc .= '<p>Copyright &copy; <a href="'.$DOMAIN_NAME.'">'.$SITE_TITLE.'</a>. All Rights Reserved.</p>';
  56. $pdate = $item['pubdate'];
  57. $item = new FeedItem();
  58. $item->title = $title;
  59. $item->link = $href;
  60. $item->description = $desc;
  61. $item->date = $pdate;
  62. $item->source = $DOMAIN_NAME;
  63. $item->author = $SITE_AUTHOR;
  64. $rss->addItem($item);
  65. }
  66. }
  67. else
  68. {
  69. echo "Error: Cannot fetch feed url - ".$url;
  70. }
  71. } // end showSummery()
  72. //***************************************************************/
  73. // Add your feed below:
  74. // showSummery("http://path-to/url/feed",number-of-rss-items,false)
  75. //****************************************************************/
  76. showSummery("http://theos.in/feed/");
  77. showSummery("http://www.cyberciti.biz/tips/feed/");
  78. showSummery("http://www.cyberciti.biz/faq/feed/",5,false);
  79.  
  80. // get your news items from other feed and display back
  81. $rss->saveFeed("RSS1.0", $TMP_ROOT."/rsscache/feed.xml");
  82. ?>

Customize script

Set temporary directory to cache rss feed, web server must be able to write to this directory.
$TMP_ROOT = "/tmp";
Set path to rss directory:
$RSS_DIR = "/home/lighttpd/theos.in/http";
Set your domain name, site title, description and author name:
$DOMAIN_NAME = "http://theos.in/";
$SITE_TITLE = "Your Site / blog Title";
$SITE_DESRIPTION = "Your Site / blog Description";
$SITE_AUTHOR = "Vivek";

Finally, set logo file name (replace logo.jpg with actual logo file name):
$SITE_LOG_URL = $DOMAIN_NAME."logo.jpg";

showSummery() function

showSummery($url,$num=10,$showfullfeed=false) fetch remote feed url and build new feed. It accept following arguments:

  • $url = Feed url
  • $num = Show the most recent posts (default = 10 )
  • $showfullfeed = true or false, for each article, show full text or summary (default false )

To combine two RSS feed http://theos.in/blog1/feed/ and http://theos.in/blog2/feed/, call showSummery():
showSummery("http://theos.in/blog1/feed/");
showSummery("http://theos.in/blog2/feed/");
showSummery("http://other-my-blog.com/feed/",5,true); // full feed

Test it

Open a web browser and type url http://your-domain.com/myfeed/rss.php

Download all files and required tools

=> You can download rss.php and required tools here (all GPL licensed except FeedCreator which is under LGPL)

Other tools - Yahoo Pipes

You can use 3rd party service such as Yahoo Pipes, which is an interactive feed aggregator and manipulator. Personally, I like to keep more control on my feed, hence I wrote this script.

{ 7 comments… read them below or add one }

1 Travis January 23, 2008 at 5:44 am

Code and procedure works (there where a few things not explained well), however, I was hoping for the two or more rss feeds to be intertwined by date, not one feed first and then the other.

Reply

2 Andrew J Talcott October 23, 2009 at 6:09 pm

Hey Travis, I was looking for the same thing that you spoke of, did you ever figure this out?

Reply

3 hyearn March 31, 2008 at 2:17 am

why?

Reply

4 Technology Hacker April 30, 2008 at 6:55 am

Thanks

Reply

5 Knowtebook.com September 23, 2008 at 7:39 pm

That we were looking for. Thank you says Knowtebook.com

Reply

6 Mithun Dhali February 10, 2009 at 6:08 pm

Thanks, I was looking for something like this ..

Reply

7 Askeptykal May 31, 2009 at 1:09 am

A little confusing at first, but works like a charm. Exactly what I needed. Thanks a lot!

Reply

Leave a Comment


− 3 = three

Previous post:

Next post: