I’ve created my Iron Ajax plugin to help me deal with processing large data sets in WordPress, . This is a tool for developers, it does not provide any value to end-users until additional code is hooked into this plugin.
WordPress Timeouts
When working with large datasets in WordPress, timeouts are often an issue that arises. The solution to avoiding timeouts is to break up the dataset into smaller subsets that can be processed in less than the maximum execution time.
WordPress Ajax to Process Large Datasets
Using Ajax in WordPress to process these smaller subsets through a single click is not a new idea. There are some terrific plugins out there processing large data sets by using Ajax calls to process each of the smaller subsets. Alex Mills (aka ViperBond007) was doing this back in 2008 with his still awesome plugin Regenerate Thumbnails.
Don’t Repeat Yourself
Since I found myself repeatedly creating code to breakup a large dataset and process it all based on Ajax calls, I decided to break out that functionality. This is my WordPress Iron Ajax plugin.
Using the Iron Ajax Plugin
On activation, a menu item is added under Tools > Iron Ajax. On this page, there is a button to start the Ajax processing however each Ajax call doesn’t actually process anything. The functionality of the plugin is created via hooks. On the admin page, you’ll see documentation specific to that instance (you can have multiple instances of the Iron Ajax settings page that do different things). The documentation for that instance includes the hooks to modify the behavior.
Bare Minimum Iron Ajax
At a minimum, you’ll want to modify the initial count that is returned for items that need to be processed and you’ll want to modify the behavior each time an item is processed.
This code should go in an executive locations like: a file in mu-plugins, a new plugin, or the theme’s functions.php.
/* Modify the entries_count | |
* i.e. the number of entries that need | |
* to be processed | |
*/ | |
add_filter( | |
'fe_ajx_iron_ajax_entries_count', | |
'examp_iajx_entries_count' | |
); | |
function examp_iajx_entries_count( $count ) { | |
// generally you'll perform some lookup | |
// to determine the total number of entries | |
// $count = my_function_to_determine_count(); | |
return $count; | |
} | |
/* Process each entry | |
*/ | |
add_filter( | |
'fe_ajx_iron_ajax_process', | |
'examp_iajx_process', | |
10, 3 | |
); | |
function examp_iajx_process( $response, $index, $persist ) { | |
// add code here to process based on $index | |
// e.g. process the row at $index in a csv | |
// my_function_to_process_one_item( $index ); | |
return $response; | |
} |
Create an Additional Instance of Iron Ajax
You can create an additional instance of the Iron Ajax plugin with the following code in your location of choice. I recommend a file in mu-plugins, a new plugin, or the theme’s functions.php.
add_action( 'plugins_loaded', 'create_example_iron_ajax_instance' );
function create_example_iron_ajax_instance() {
if ( class_exists( 'Fe_Ajx' ) ) {
new Fe_Ajx( array(
'instance_id' => 'Example',
) );
}
}
This really like something I could use – however dont quite understand it yet.
I need to be able to process large xml files, using just php wont work cause of timeouts.
Installed the plugin on a wordpress installation, and looking at the demo page, I cant figure out where in the code i would put in the xml file, and where i actually handle the data, in this case putting it into the database?
Any hints and tips much appreciated:)
In this Stack Overflow article, they discuss how to Count entries in a XML file. In the example code on this page, you’ll need to do something like this in the
my_function_to_determine_count()
function you define.Then you need to process a single entry in the XML file, based on an index in the
my_function_to_process_one_item( $index )
function you define.I hope this helps.
Hey, thanks for the reply, but that isnt my concern… not yet atleast;) I cant figure out your plugin, where and what functions I should interact with in order for it to actually process some stuff. Right now it is just the progressbar that moves, so thats my concern now, how I get it to do stuff, along with the progressbar?
Thanks a bunch:)
Hi Tommy,
If you’re still looking to make this work, here is some example code where I’ve used it.
https://gist.github.com/salcode/2baa7ee48a2b604a235b