I am attempting to update a large array of feeds into WordPress, using fetch_feeds. As can be expected, when going over 50 feeds the server is starting to time out. I would still like to update the feeds in bulk but without crashing the server.
Can you suggest a method through which I can break things down? Maybe using AJAX? Is that the best technique? Any examples I can look at?
You can try using wp cron functions, programing a feed import every 10 minutes or so, until you have nothing left in queue.
Example code:
<?php
register_activation_hook( __FILE__, 'wp1903_schedule_cron' );
function wp1903_schedule_cron() {
$counter = 0;
$feeds = array(
'http://example.com/feed1',
'http://example.com/feed2',
'http://example.com/feed3',
'http://example.com/feed4'
);
foreach ($feeds as $feed) {
$counter++;
// schedule every 10 mins
$time = time() + (600 * $counter);
wp_schedule_single_event( $time, 'wp1903_process_feed', $feed );
}
}
add_action('wp1903_process_feed', 'wp1903_import_feed');
function wp1903_import_feed($feed_url="") {
// do the import
}
It is essential only to run the scheduler on a activation hook, or else at every page load the crons will be rescheduled.