Marco Luthe Online! » WordPress Stuff » Using WP Cron To Cache External Scripts (Via Plugin)
Using WP Cron To Cache External Scripts (Via Plugin)
Recently, Torsten had the very nice idea of caching the Flickr badge script locally with a cron job. I have also seen a similar idea for keeping a local copy of the Google Analytics script at askapache.com. Unfortunately, I am not so familiar with setting up cron jobs on Apache, so I thought it would be easier to use the WordPress built-in cron system.
Basically, you can useĀ the WordPress function wp_schedule_event() to periodically trigger a function that you define. It is not as accurate as the actual cron system that is run by the server, because people still have to visit the site to trigger it – but I found out that it is accurate enough for my purposes.
I noticed that the Flickr badge script sometimes needs ages to be loaded… as does the Friendfeed updates widget script and also the Google Analytics script every now and then. So I thought it would be nice to have a locally cached copy that is updated every now and then – because the Flickr and Friendfeed scripts are dynamic, and it might also be that the Google Analytics Script changes (also see this recommendation by Google).
I have written a little plugin called “WP-CronCacheScripts” that uses the PHP cURL library to download the scripts, using the following update frequencies:
- Flickr: download the script every hour,
- Friendfeed: download the script every fifteen minutes half hour,
- Google Analytics: download the script every twelve hours.
Of course, those could be changed to whatever frequencies you want, but up to now, you would have to do that manually within the plugin php code, because I have no admin management interface for it yet.
And here is how the plugin works:
- Before activation, make sure you fill in your Flickr and Friendfeed ID in the right place (you have to do that within the PHP script, it is commented at the top where the variables are defined).
- On activation, the plugin checks if the folder “/wp-content/ccs_cache/” exists and is writeable. You have to ensure that, otherwise the plugin will not work! It will give you an error message if this happens. If you get an error, please deactivate the plugin, create the folder, make it writeable and re-activate the plugin.
- If the folder exists and is writeable, three functions are hooked to the WP cron system using the wp_schedule_event() function (Flickr, Friendfeed, Google Analytics).
- The functions download the scripts via cURL into the /css_cache/ folder periodically. If you want to change the frequencies I mentioned above, you will have to do that before activation. See the code for more details or contact me.
- If an error occurs, an option will be updated to “fail” which means the file could not be downloaded. Without an error, that option will be set to “success”. These options are later checked by the function to integrate the scripts (see below).
- On deactivation, the cron jobs are cleared using wp_clear_scheduled_hook(). This is important, because otherwise, the cron jobs will still be triggered! For future versions, I am planning to implement a management interface where you can stop them manually.
The cached scripts can be integrated using a function that is provided by the plugin, it is called wp_ccs_include() and can have one of the following parameters (small letters):
- “flickr” for the Flickr badge,
- “friendfeed” for the Friendfeed updates widget,
- “google” for the Google Analytics script.
The cached scripts are included using <script type=”text/javascript” src=”…”></script>.
Here is an example how to include the cached Google Analytics script:
<!-- Start Google Analytics Tracker Script -->
<?php if(function_exists('wp_ccs_include')) { wp_ccs_include("google"); } else { ?>
<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<?php } ?>
<script type="text/javascript">
var pageTracker = _gat._getTracker("UA-1171939-1");
pageTracker._trackPageview();
</script>
<!-- End Google Analytics Script -->
Note that the second script starting with “var pageTracker” will not be included by that function, only the ga.js will be!
The function also checks if the scripts could be downloaded, or if an error has occured while trying to cache them. If the latter happens, the original URL to the script is provided via “src=…”, otherwise, the URL to the local copy will be included.
OK, I think it is now time to let you
- Download the plugin!
- Deactivate any older version, if you already installed it.
- Change anything you need within the script (see above).
- Upload the file to your WordPress plugin folder.
- Make sure the /wp-content/ccs_cache/ folder exists and is writeable.
- Activate the plugin via “Plugins” in the admin panel.
- Be sure to use the wp_ccs_include() function in your templates (or link to the cached files yourself).
It really seems to work, because YSlow gives me a better rating now.
Feedback is appreciated.
Additionally, the plugin WP-Crontrol is a good choice to manage WP cron jobs.
Release History
[Update from 2008-12-18]
- v1.0.2:
- Added an admin message if activation did not work using add_action(‘admin_notices’…).
2008-12-08
- v1.0.1:
- Moved add_filter(‘cron_schedules’…) from within activation hook to top to make it work (stupid mistake!).
- Changed Friendfeed frequency to every half hour (should be enough).
- Tried to cache http://digg.com/tools/diggthis.js, but that throws an error. Also, the MyBlogLog widget is dynamic and checks for cookies, so no sense in caching locally, either. Any idea of other static scripts to cache?
2008-12-07
- v1.0:
- Initial release.
Filed under: WordPress Stuff · Tags: cache, copy, cron job, cURL, javascript, local, plugin, script, wordpress











