SimplePie 1.3.1 is now available. Upgrade now!

SimplePie Documentation.  Learn how to use this thing.  It's way better than going to school.

You are here: Documentation » Tips, Tricks, Tutorials, and Screencasts » Setting up a cron job for SimplePie and SimplePie Plugin for Wordpress

Setting up a cron job for SimplePie and SimplePie Plugin for Wordpress

This procedure to set up a cron job to update the SimplePie cache in the background once an hour is similar for both SimplePie and SimplePie Plugin for Wordpress so I've combined them in one tutorial.

Compatibility

  • Supported in SimplePie 1.0 or newer.
  • Requires a Linux based host that allows cron jobs.

Instructions

The sequence of events for both SimplePie classic and SimplePie Plugin for Wordpress is:

  1. Create and upload a web page that updates the SimplePie cache.
  2. Run a cron job to load that page once an hour.
  3. Change the cache duration of your normal SimplePie runs to 999999999, and timeout to -1.

SimplePie Classic

Create a file called update_simplepie_cache.php

Enter the text below.

<?php
        require 'simplepie.inc'; 
        $urls = array([enter all your feed urls here]);
        $cache_location = './cache';  //  change to your cache location 
        $feed = new SimplePie();
        $feed->set_feed_url($urls);
        $feed->set_cache_location($cache_location);
        $feed->set_cache_duration(0);   // force cache to update immediatlely 
        $feed->set_timeout(5);   // optional, if you have a lot of feeds a low timeout may be necessary 
        $feed->init();
?>

Upload that file to a web accessible directory on your website so you can access it via HTTP. For instance:

http://yoursite.com/update_simiplepie_cache.php

SimplePie Plugin for Wordpress

Create a file named update_simplepie_cache.php and enter this text:

<?php
/*
Template Name: update_simplepie_cache
*/
        SimplePieWP(array([your feed urls here]), 
        array('set_cache_duration' => 0,'set_timeout' => 5));
?>

Upload update_simplepie_cache.php to your current Wordpress theme directory.

Create a “Page” in Wordpress using the template: “update_simplepie_cache”.

If you have permalinks turned on in Wordpress, change the permalink for the page to update_simplepie_cache. If not, write down the pageid number for use later.

Create the cron job

There are two ways to create the cron job. If you have SSH and root access to your server, you create a BASH script and upload it directly to your /etc/cron.hourly directory, or the monthly directory if you only want to update your cache once a month. There is no yearly or age-of-aquarius directory.

The first way to create the cron job

SimplePie Classic

Create a file named update_simplepie_cache.sh and enter this text into it:

#!/bin/bash
wget -O /dev/null http://yoursite.com/update_simplepie_cache.php

SimplePie Plugin for Wordpress

Create a file named update_simplepie_cache.sh and enter this text into it:

With permalinks on:

#!/bin/bash
wget -O /dev/null http://yoursite.com/update_simplepie_cache/

With permalinks off:

#!/bin/bash
wget -O /dev/null http://yoursite.com/?pageid=[page number]

Enter the pageid number of Page you created in Wordpress.

When your ready to start the cron job, upload update_simplepie_cache.sh to your /etc/cron.hourly directory. It will run automatically once an hour, usually at minute 56.

The second way to create the cron job

Use your server control panel. Here's a screenshot of the CPANEL CRON job manager:

cpanel

Enter the “wget” line into the CPANEL CRON manager's “command to run” text box. Select minute 0, or whichever you want, Every Hour, Every Day, Every Month, Every Weekday.

Or

php -f /sitedirectory/update_simplepie_cache.php

Set your cache duration

Once your cron job is running, go back to your original SimplePie code and set the cache duration to a number greater than the cron job time period which is usually 1 hour. I use 999999999 just to be sure but if you like living on the edge you could just set it to 3601, 1 second more than an hour. Why are you doing this you say? Doing this effectively tells SimplePie to never update its cache during a normal operations since it's already being updated by the cron job. Also, just in case, set the timeout to -1 to prevent SimplePie from retrying previous failed feeds if there were any. You might also want to check your server temp periodically because SimplePie will now be running at the speed of wheat and could break the pie barrier.

Set cache duration and timeout in SimplePie Plugin for Wordpress

You can do this in the configuration webpage, or in the call to SimplePieWP, like this:

<?php echo SimplePieWP(array('feed1.com',feed2.com'),
                       array('set_cache_duration'=>999999999,'set_timeout'=>-1));

Set cache duration and timeout in SimplePie Classic

$feed->set_cache_duration(999999999); 
$feed->set_timeout(-1);

That's it!


tutorial/setting_up_a_cron_job_with_simplepie.txt · Last modified: 2011/03/05 19:56 (external edit)