RSS Generator For Importing to Wordpress

Maybe all that MySQL command line editing in the last post was a bit more than you wanted to deal with, in which case this PHP function may be handy for you.

This function takes an array of items and creates an rss feed with a slowdrip staggering of publish dates.

rss_maker_for_wordpress()

 
function rss_maker_for_wordpress($items,
                                 $dates=.5,
                                 $back_date=1.0){
 
  /*
 
    Builds a (possibly invalid XML) RSS feed suitable for importing to
    wordpress.
 
    Author: Victory
    Release Date: 090815
    Version: 1.0
 
    For the description of the paramters see function rss_maker();
 
  */
 
  return rss_maker($items,
                   $dates,
                   $back_date,
                   "Wordpress Importable",
                   "http://example.net/",
                   "A WP Feed",
                   True,
                   False);
}

rss_maker_for_wordpress() requires the more general rss_maker() function.

rss_maker()

 
 
function rss_maker($items,
                   $dates=.5,
                   $back_date=0,
                   $title="The Feed", 
                   $link="http://example.com/",  
                   $description="A Feed",
                   $no_guid=False,
                   $escape_with_cdata=True){
 
  /*
    Builds a (possibly invalid XML) RSS feed.
 
    Author: Victory
    Release Date: 090815
    Version: 3.0
 
    @param array(array()) $items - The items to be inserted into the
    array. The inner array must set 'title','link' and 'description.'
 
    exmaple:
 
    $items=Array(Array("title"=>"Item 1",
                       "description"=>"Body of text for item 1"),
                 Array("title"=>"Second Item",
                       "description"=>"Body of text for item 2"));
 
    @param [array or float] $dates - If an array then its a list of
    Published Dates (PubDate) in date("r",mktime()) format, if its a
    float, create them randomly using choosen dates steped the number
    of days in the future.
 
    @param float $back_date - the number of days before today to start
    creating random dates (ignored if $dates is an array)
 
    @param string $title - string of the the title of the feed
    (ignored by wordpress)
 
    @param string $link - the feed's link (ignored by wordpress)
 
    @param string $description - the feed's description (ignored by
    wordpress)
 
    Basic Usage:
 
    $my_feed=
      rss_maker_for_wordpress(
       Array(
        Array("title"=>"Item 1",
              "description"=>"Body of text for item 1"),
        Array("title"=>"Second Item",
              "description"=>"Body of text for item 2")));
 
    NOTE: To get valid XML you would need to htmlentites() escape
    $items description.
 
  */
 
 
  /* start by setting the feeds channel info */
 
  // So if the dates are not set explicitly ...
  if(!is_array($dates)){
    // ... use a random time this morning as the pubdate,
    $pubDate = mktime(rand(0,5),rand(0,50),rand(0,50),
                      date("n"),date("j"),date("Y"));
  }else{// ... otherwise use the first date of $dates
    $pubDate = $dates[0];
  }
 
  // now get link, pubdate etc...
  $pubDate=date("r", $pubDate);
  $rss_link=$link;
  $rss_title=$title;
  $rss_description=$description;
  $rss_language="en-us";
 
  // and shove that into the feeds head.
  $feed[]="<" . "?xml version=\"1.0\"" . "?" . ">
<rss version=\"2.0\">
  <channel>
    <title><![CDATA[$rss_title]]></title>
    <link>$rss_link</link>
    <description><![CDATA[$rss_description]]></description>
    <language>$rss_language</language>
    <pubDate>$pubDate</pubDate>
    <docs>http://blogs.law.harvard.edu/tech/rss</docs>
  ";
 
 
  // We can set up a template for a feed item. Note that if we wanted
  // to be more confident that this will create a valid feed than we
  // can use <![CDATA[%variable%]]> here instead, but wordpress and
  // others doen't always play nice with such feeds. Also you would
  // have to set a <guid>%link%#%guid%</guid> for this to be truly
  // valid and the items in $items would need a $item['link'];
 
  $rss_item="
    <item>
      <link>%link%</link>
  ";
  if($escape_with_cdata){
    $rss_item.="
      <title><![CDATA[%title%]]></title>
      <description><![CDATA[%description%]]></description>
  ";
  }else{
    $rss_item.="
      <title>%title%</title>
      <description>%description%</description>      
  ";
  }
  if(isset($items[0]['link'])){
    $rss_item .= "
      <guid>%link%#%guid%</guid>
  ";
  }
  $rss_item .="
      <pubDate>%pubdate%</pubDate>
    </item>
  ";
 
  // if dates aren't set then make dates randomly in the future
  if(!is_array($dates)){
 
    // the max time interval in minutes (1440 minutes in a day)
    $cur_day=-intval($back_date * 1440);
 
    $max_time_interval=max(1,intval($dates * 1440));
    $min_time_interval=intval(floor($max_time_interval/2.0));
 
    // switch date back an array
    $dates=Array();
 
    // create a random date for every item in the list
    for($i=0; $i<count($items); $i++){
 
      // step the cur_day forward
      $cur_day+=rand($min_time_interval,
                     $max_time_interval);
 
      $dates[]=
        mktime(rand(12,20),
               rand(0,55),
               rand(0,55),
               date("n",strtotime("+$cur_day minutes")),
               date("j",strtotime("+$cur_day minutes")),
               date("Y",strtotime("+$cur_day minutes")));
 
 
    }
    // "blog" order
    $dates=array_reverse($dates);
  }
 
  // We need to set up the variables array for the template
  $vars=Array("%title%","%link%","%description%",
              "%pubdate%","%guid%");
 
  // now just sub in the our values and append them to the feed
  foreach($items as $i=>$item){
    $pubDate=date("r", $dates[$i]);
    $vals=Array($item["title"],$item["link"],$item["description"],
                $pubDate,rand(10000,99999));
    $feed[]=str_replace($vars,$vals,$rss_item);
  }
 
  $feed[]="
  </channel>
</rss>
";
  return implode("\n",$feed);
}

Please tell me if you found this tool and post were useful, if not maybe you could suggest a topic.

  • Share/Bookmark
This entry was posted in intermediate.programming and tagged , , , . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.
1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading ... Loading ...

2 Comments

  1. Posted October 1, 2009 at 5:56 pm | Permalink

    How I missed this one, I have no idea! Excellent

  2. Posted October 27, 2009 at 8:10 am | Permalink

    RSS maker for WP is amazing. Thank you

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">

If you are going to post code please use:
<pre lang="php" escaped="true"> YOUR_CODE_HERE </pre>

Change the lang to mysql, python, lisp, whatever. This will escape your code.